def process_create_or_update_request(request, create_or_update_function, request_top_level_key, identifier=None, name=None): try: request_body = json.loads(request.body)[request_top_level_key] # Create the Activity object activity = Activity(username=request.user.username, requestPath=settings.IIIF_BASE_URL + request.get_full_path(), requestBody=request_body, requestMethod=request.method, remoteAddress=request.META['REMOTE_ADDR']).save() user = request.user.to_mongo() del user["_id"] # Mongo ObjectID is not serializable request_body["identifier"], request_body[ "name"] = identifier, name # Include these url fields with the requestBody if (request.method == "POST" and settings.QUEUE_POST_ENABLED) or ( request.method == "PUT" and settings.QUEUE_PUT_ENABLED): queue = Queue( status="Pending", activity=activity.to_mongo()).save() # Create the Queue object queue_activity = "{0}_{1}".format( queue.id, activity.id) # link the Queue and Activity object queue_status_url = "{0}/queue/{1}".format( settings.IIIF_BASE_URL, queue.id) # Get the Queue status url if settings.QUEUE_RUNNER != "CELERY": # Either 'PROCESS' or 'THREAD' imported as 'Runner' Runner(target=create_or_update_function, args=(user, request_body, False, queue_activity, initialize_new_bulk_actions())).start() else: create_or_update_function.delay(user, request_body, False, queue_activity, initialize_new_bulk_actions()) return Response(status=status.HTTP_202_ACCEPTED, data={ 'message': "Request Accepted", "status": queue_status_url }) else: result = create_or_update_function(user, request_body, False, "_" + str(activity.id), initialize_new_bulk_actions()) return Response(status=result["status"], data={ "responseBody": result["data"], "responseCode": result["status"] }) except Exception as e: # pragma: no cover return Response(status=status.HTTP_400_BAD_REQUEST, data={'error': e.message})
def updateBackground(self, request, identifier=None, name=None, format=None): try: requestBody = json.loads(request.body)["annotationList"] activity = Activity(username=request.user.username, requestPath=request.get_full_path(), requestMethod=request.method, remoteAddress=request.META['REMOTE_ADDR'], startTime=datetime.now()) user = request.user.to_mongo() del user["_id"] if settings.QUEUE_PUT_ENABLED: queue = Queue(status="Pending", activity=activity.to_mongo()).save() activity.requestBody = requestBody activity.save() if settings.QUEUE_RUNNER != "CELERY": Runner(target=updateAnnotationList, args=(user, identifier, name, requestBody, False, str(queue.id), str(activity.id), initializeNewBulkActions())).start() else: updateAnnotationList.delay(user, identifier, name, requestBody, False, str(queue.id), str(activity.id), initializeNewBulkActions()) return Response(status=status.HTTP_202_ACCEPTED, data={ 'message': "Request Accepted", "status": settings.IIIF_BASE_URL + '/queue/' + str(queue.id) }) else: activity.requestBody = requestBody activity.save() result = updateAnnotationList(user, identifier, name, requestBody, False, None, str(activity.id), initializeNewBulkActions()) return Response(status=result["status"], data={ "responseBody": result["data"], "responseCode": result["status"] }) except Exception as e: # pragma: no cover return Response(status=status.HTTP_400_BAD_REQUEST, data={'error': e.message})
def process_delete_request(request, delete_function, identifier="", name=""): try: activity = Activity(username=request.user.username, requestPath=settings.IIIF_BASE_URL + request.get_full_path(), requestMethod=request.method, remoteAddress=request.META['REMOTE_ADDR']).save() user = request.user.to_mongo() del user["_id"] # Mongo ObjectID is not serializable if settings.QUEUE_DELETE_ENABLED: queue = Queue( status="Pending", activity=activity.to_mongo()).save() # Create the Queue object queue_activity = "{0}_{1}".format( queue.id, activity.id) # link the Queue and Activity object if settings.QUEUE_RUNNER != "CELERY": # Either 'PROCESS' or 'THREAD' imported as 'Runner' Runner(target=delete_function, args=(user, identifier + "__" + name, queue_activity)).start() else: delete_function.delay(user, identifier + "__" + name, queue_activity) return Response(status=status.HTTP_202_ACCEPTED, data={ 'message': "Request Accepted", "status": settings.IIIF_BASE_URL + '/queue/' + str(queue.id) }) else: result = delete_function(user, identifier + "__" + name, "_" + str(activity.id)) return Response(status=result["status"], data={ "responseBody": result["data"], "responseCode": result["status"] }) except Exception as e: # pragma: no cover return Response(status=status.HTTP_400_BAD_REQUEST, data={'error': e.message})
def destroyBackground(self, request, name=None, format=None): try: activity = Activity(username=request.user.username, requestPath=request.get_full_path(), requestMethod=request.method, remoteAddress=request.META['REMOTE_ADDR'], startTime=datetime.now()) user = request.user.to_mongo() del user["_id"] if settings.QUEUE_DELETE_ENABLED: queue = Queue(status="Pending", activity=activity.to_mongo()).save() activity.save() if settings.QUEUE_RUNNER != "CELERY": Runner(target=destroyCollection, args=(user, name, False, str(queue.id), str(activity.id))).start() else: destroyCollection.delay(user, name, False, str(queue.id), str(activity.id)) return Response(status=status.HTTP_202_ACCEPTED, data={ 'message': "Request Accepted", "status": settings.IIIF_BASE_URL + '/queue/' + str(queue.id) }) else: activity.save() result = destroyCollection(user, name, False, None, str(activity.id)) return Response(status=result["status"], data={ "responseBody": result["data"], "responseCode": result["status"] }) except Exception as e: # pragma: no cover return Response(status=status.HTTP_400_BAD_REQUEST, data={'error': e.message})