def test_to_delete_an_invalid_activity(self): activity2 = Activity(responseCode=200).save() activity2.delete() response = self.client.delete('/activity/' + str(activity2.id)) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) self.assertEqual( response.data['error'], "Activity with id '" + str(activity2.id) + "' does not exist.")
def test_to_delete_all_activities(self): activity = Activity(responseCode=200).save() activity = Activity(responseCode=200).save() activity = Activity(responseCode=200).save() activity = Activity(responseCode=200).save() self.assertEqual(len(Activity.objects), 4) response = self.client.delete('/activity') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['message'], "Successfully deleted all Activities.") self.assertEqual(len(Activity.objects), 0)
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 test_to_delete_a_specific_activity(self): activity = Activity(responseCode=200).save() self.assertEqual(len(Activity.objects), 1) response = self.client.delete('/activity/' + str(activity.id)) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual( response.data['message'], "Successfully deleted the Activity with id '" + str(activity.id) + "'.") self.assertEqual(len(Activity.objects), 0)
def test_to_delete_all_activities_with_staff(self): self.user = User.create_user('teststaff', '*****@*****.**', 'teststaffpass') jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER payload = jwt_payload_handler(self.user) token = jwt_encode_handler(payload) self.client.credentials(HTTP_AUTHORIZATION='JWT ' + token) activity = Activity(responseCode=200).save() activity = Activity(responseCode=200).save() activity = Activity(responseCode=200).save() activity = Activity(responseCode=200).save() self.assertEqual(len(Activity.objects), 4) response = self.client.delete('/activity') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.assertEqual( response.data['detail'], "You don't have the necessary permission to perform this action. Please contact your admin." ) self.assertEqual(len(Activity.objects), 4)
def view_all(self, request, format=None): try: query = process_search_query(request.GET) serializer = ActivityEmbeddedSerializer( Activity.objects(**query).order_by('-id'), context={'request': request}, many=True) return Response(status=status.HTTP_200_OK, data=serializer.data) except Exception as e: # pragma: no cover return Response(status=status.HTTP_400_BAD_REQUEST, data={'error': str(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})
def setUp(self): self.user = User.create_user('staff', '*****@*****.**', 'staffpass', True) jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER payload = jwt_payload_handler(self.user) token = jwt_encode_handler(payload) self.client.credentials(HTTP_AUTHORIZATION='JWT ' + token) data = {"manifest": json.loads(open(MANIFEST_FULL).read())} Activity(username="******", requestMethod="POST", responseBody={ "@id": "http://*****:*****@type": "someType" }, responseCode=201, endTime=datetime.now()).save() Activity(username="******", requestMethod="PUT", responseBody={ "@id": "http://*****:*****@type": "someType" }, responseCode=200, endTime=datetime.now()).save() Activity(username="******", requestMethod="DELETE", responseBody={ "@id": "http://*****:*****@type": "someType" }, responseCode=204, endTime=datetime.now()).save() Activity(username="******", requestMethod="POST", responseBody={ "@id": "http://*****:*****@type": "someType" }, responseCode=201, endTime=datetime.now()).save() Activity(username="******", requestMethod="PUT", responseBody={ "@id": "http://*****:*****@type": "someType" }, responseCode=200, endTime=datetime.now()).save() Activity(username="******", requestMethod="DELETE", responseBody={ "@id": "http://*****:*****@type": "someType" }, responseCode=204, endTime=datetime.now()).save() Activity(username="******", requestMethod="POST", responseBody={ "@id": "http://*****:*****@type": "someType" }, responseCode=201, endTime=datetime.now()).save() Activity(username="******", requestMethod="PUT", responseBody={ "@id": "http://*****:*****@type": "someType" }, responseCode=200, endTime=datetime.now()).save() Activity(username="******", requestMethod="DELETE", responseBody={ "@id": "http://*****:*****@type": "someType" }, responseCode=204, endTime=datetime.now()).save() Activity(username="******", requestMethod="POST", responseBody={ "@id": "http://*****:*****@type": "someType" }, responseCode=201, endTime=datetime.now()).save() Activity(username="******", requestMethod="PUT", responseBody={ "@id": "http://*****:*****@type": "someType" }, responseCode=200, endTime=datetime.now()).save() Activity(username="******", requestMethod="DELETE", responseBody={ "@id": "http://*****:*****@type": "someType" }, responseCode=204, endTime=datetime.now()).save() Activity(username="******", requestMethod="DELETE", responseBody={ "@id": "http://*****:*****@type": "someType" }, responseCode=422, endTime=datetime.now()).save() Activity(username="******", requestMethod="POST", responseBody={ "@id": "http://*****:*****@type": "someType" }, responseCode=201, endTime=datetime.now()).save() Activity(username="******", requestMethod="DELETE", responseBody={ "@id": "http://*****:*****@type": "someType" }, responseCode=204, endTime=datetime.now()).save() Activity(username="******", requestMethod="PUT", responseBody={ "@id": "http://*****:*****@type": "someType" }, responseCode=404, endTime=datetime.now()).save() Activity(username="******", requestMethod="DELETE", responseBody={ "@id": "http://*****:*****@type": "someType" }, responseCode=404, endTime=datetime.now()).save() Activity(username="******", requestMethod="POST", responseBody={ "@id": "http://*****:*****@type": "someType" }, responseCode=201, endTime=datetime.now()).save() Activity(username="******", requestMethod="PUT", responseBody={ "@id": "http://*****:*****@type": "someType" }, responseCode=200, endTime=datetime.now()).save() Activity(username="******", requestMethod="DELETE", responseBody={ "@id": "http://*****:*****@type": "someType" }, responseCode=204, endTime=datetime.now()).save() Activity(username="******", requestMethod="POST", responseBody={ "@id": "http://*****:*****@type": "someType" }, responseCode=201, endTime=datetime.now()).save() Activity(username="******", requestMethod="PUT", responseBody={ "@id": "http://*****:*****@type": "someType" }, responseCode=200, endTime=datetime.now()).save() Activity(username="******", requestMethod="DELETE", responseBody={ "@id": "http://*****:*****@type": "someType" }, responseCode=204, endTime=datetime.now()).save() Activity(username="******", requestMethod="POST", responseBody={ "@id": "http://*****:*****@type": "someType" }, responseCode=201, endTime=datetime.now()).save() Activity(username="******", requestMethod="PUT", responseBody={ "@id": "http://*****:*****@type": "someType" }, responseCode=200, endTime=datetime.now()).save() Activity(username="******", requestMethod="DELETE", responseBody={ "@id": "http://*****:*****@type": "someType" }, responseCode=204, endTime=datetime.now()).save() Activity(username="******", requestMethod="POST", responseBody={ "@id": "http://*****:*****@type": "someType" }, responseCode=201, endTime=datetime.now()).save() Activity(username="******", requestMethod="PUT", responseBody={ "@id": "http://*****:*****@type": "someType" }, responseCode=200, endTime=datetime.now()).save()
def discovery(self, request, page=None, format=None, date_range=None): try: if date_range: try: data_range_query_text = "/?from=" + \ date_range[0] + "&to=" + date_range[1] from_date = datetime.strptime(date_range[0], '%Y-%m-%d') to_date = datetime.strptime(date_range[1], '%Y-%m-%d') + timedelta(days=1) activities = Activity.objects( endTime__lte=to_date, endTime__gte=from_date, responseCode__lt=300).order_by('-id') except Exception: return Response( status=status.HTTP_422_UNPROCESSABLE_ENTITY, data={ 'error': "The date range query format is invalid." }) else: activities = Activity.objects( responseCode__lt=300).order_by('-id') context = [ "http://iiif.io/api/presentation/3/context.json", "https://www.w3.org/ns/activitystreams" ] total = activities.count() last_page = (total + 20 - 1) // 20 if page: # Construct a specific Collection Page if int(page[1:]) == 0: # Page number must be grater than 0 return Response( status=status.HTTP_404_NOT_FOUND, data={ 'error': "Discovery with page '0' does not exist." }) if int(page[1:]) > last_page: # Requested page is out of range return Response(status=status.HTTP_404_NOT_FOUND, data={ 'error': "Discovery with page '" + page[1:] + "' does not exist." }) type = "CollectionPage" if date_range: id = settings.IIIF_BASE_URL + "/discovery" + page + data_range_query_text part_of = { "id": settings.IIIF_BASE_URL + "/discovery" + data_range_query_text, "type": "Collection" } else: id = settings.IIIF_BASE_URL + "/discovery" + page part_of = { "id": settings.IIIF_BASE_URL + "/discovery", "type": "Collection" } label = "{0} IIIF Discovery Collection: Page{1}".format( settings.TOP_LEVEL_COLLECTION_LABEL, page) first = None last = None if date_range: next = { "id": settings.IIIF_BASE_URL + "/discovery-" + str(int(page[1:]) + 1) + data_range_query_text, "type": "CollectionPage" } if int(page[1:]) + 1 <= last_page else None else: next = { "id": settings.IIIF_BASE_URL + "/discovery-" + str(int(page[1:]) + 1), "type": "CollectionPage" } if int(page[1:]) + 1 <= last_page else None items = [] method_mapping = { "POST": "Create", "PUT": "Update", "DELETE": "Delete" } # Get only the required (20 or less) activities that belong to this page sorted by endTime. start_index = (int(page[1:]) - 1) * 20 end_index = start_index + 20 if activities: activities = activities[start_index:end_index] else: activities = Activity.objects(responseCode__lt=300)[ start_index:end_index] # pragma: no cover for activity in activities: items.append({ "id": settings.IIIF_BASE_URL + "/activity/" + str(activity.id), "type": method_mapping[activity.requestMethod], "object": { "id": activity.responseBody["@id"], "type": activity.responseBody["@type"] }, "actor": activity.username, "startTime": activity.startTime, "endTime": activity.endTime }) count = len(items) else: # Construct the Top Level Discovery Collection type = "Collection" if date_range: id = settings.IIIF_BASE_URL + "/discovery" + data_range_query_text else: id = settings.IIIF_BASE_URL + "/discovery" part_of = None label = "{0} IIIF Discovery Collection".format( settings.TOP_LEVEL_COLLECTION_LABEL) count = None if date_range: first = { "id": settings.IIIF_BASE_URL + "/discovery-1" + data_range_query_text, "type": "CollectionPage" } last = { "id": settings.IIIF_BASE_URL + "/discovery-" + str(last_page) + data_range_query_text, "type": "CollectionPage" } else: first = { "id": settings.IIIF_BASE_URL + "/discovery-1", "type": "CollectionPage" } last = { "id": settings.IIIF_BASE_URL + "/discovery-" + str(last_page), "type": "CollectionPage" } next = None items = None if last_page == 0: last = first if (first["id"] == last["id"]): last = {} if (total == 0): first = {} # Render the response discovery = { "@context": context, "id": id, "type": type, "partOf": part_of, "label": label, "total": total, "count": count, "first": first, "last": last, "next": next, "items": items } return Response(status=status.HTTP_200_OK, data=discovery) except Exception as e: # pragma: no cover return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR, data={'error': e.message})