below is my patch API to update fields in a model. I need to run the try except block which is patching permissions through external API's to run async with this patch api.
@staticmethod
def patch(request, pk):
"""
Update specific content request fields
Patch Tenant and Customer Permissions in License
@param request:
@param pk: content request id
@return:
"""
content_request = get_object_or_404(ContentRequest, pk=pk)
serializer = content_request_serializers.ContentRequestPatchSerializer(
content_request, data=request.data, context={"request": request}, partial=True
)
if serializer.is_valid():
serializer.save()
try:
if content_request.status == "purchased":
logger.info("Patching Permissions in License for Tenant and Customer")
response_1 = (
applisting_helper.FetchServiceLicenseInfo()
.send_request(params={"service_id": content_request.content_service_id})
.json()
)
module = response_1[0].get("module_name")
submodule_id = response_1[0].get("submodule_id")
response_2 = (
lic_helper.FetchSubmoduleData(submodule_id)
.send_request()
.json()
.get("sub_module_data")
)
submodule = response_2.get("sub_module_name")
permissions = response_1[0].get("permissions")
data = []
for permission in permissions:
data.append(module + "." + submodule + "." + permission)
tenant_id = content_request.tenant
lic_helper.PatchTenantPermissions(tenant_id).send_request(
payload={"permissions": data}
)
logger.info("Successfully patched Tenant Permissions in License")
response_3 = (
lic_helper.FetchTenantData(tenant_id)
.send_request()
.json()
.get("tenant_data")
)
customer_id = response_3.get("customer_id")
lic_helper.PatchCustomerPermission(customer_id).send_request(
payload={"permissions": data}
)
logger.info("Successfully patched Customer Permissions in License")
else:
logger.warning("No new permissions patched")
except requests.exceptions.ConnectionError:
logger.warning(
"Failed to connect to the License host, skipping permission patching"
)
return
return Response(serializer.data, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)