diff --git a/docs/_static/openapi.yaml b/docs/_static/openapi.yaml index 4ad73f81..45d8ef0f 100644 --- a/docs/_static/openapi.yaml +++ b/docs/_static/openapi.yaml @@ -123,6 +123,25 @@ paths: publication_policy: type: string description: '' + /api/collections/{id}/notes/: + get: + operationId: collections_notes_retrieve + description: Return the notes for the collection and its child collections. + parameters: + - in: path + name: id + schema: + type: integer + required: true + tags: + - collections + security: + - cookieAuth: [] + - basicAuth: [] + - {} + responses: + '200': + description: No response body /api/collections/{id}/tree/: get: operationId: collections_tree_list @@ -357,6 +376,7 @@ components: source_id: type: string description: If sourced from Scrapy, this should be the name of the spider. + pattern: ^([a-z]+_)*[a-z]+$ data_version: type: string format: date-time diff --git a/process/views.py b/process/views.py index 1a8e5df0..3a5a8783 100644 --- a/process/views.py +++ b/process/views.py @@ -232,6 +232,26 @@ def metadata(self, request, pk=None): return Response(metadata) + @action(detail=True) + def notes(self, request, pk=None): + """Return the notes for the collection and its child collections.""" + root_collection = get_object_or_404(Collection, pk=pk) + if root_collection.transform_type: + return Response("The collection must be a root collection", status=status.HTTP_400_BAD_REQUEST) + compiled_collection = root_collection.get_compiled_collection() + upgraded_collection = root_collection.get_upgraded_collection() + + ids = [ + collection.id + for collection in [root_collection, compiled_collection, upgraded_collection] + if collection is not None + ] + + notes = {level: [] for level in CollectionNote.Level.values} # noqa: PD011 # false positive + for note in CollectionNote.objects.filter(collection_id__in=ids): + notes[note.code].append([note.note, note.data]) + return Response(notes) + @extend_schema(responses=TreeSerializer(many=True)) @action(detail=True) def tree(self, request, pk=None): diff --git a/tests/fixtures/complete_db.json b/tests/fixtures/complete_db.json index 7efc1a6f..13414d6a 100644 --- a/tests/fixtures/complete_db.json +++ b/tests/fixtures/complete_db.json @@ -80,6 +80,8 @@ "fields": { "collection": 1, "note": "adsfsdfsdsadf", + "code": "INFO", + "data": {}, "stored_at": "2020-12-29T13:50:28.589" } }, @@ -89,6 +91,8 @@ "fields": { "collection": 2, "note": "adsfsdfsdsadf", + "code": "WARNING", + "data": {}, "stored_at": "2020-12-29T13:50:28.597" } }, diff --git a/tests/test_views.py b/tests/test_views.py index 5817fb7f..b5452379 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -108,6 +108,26 @@ def test_destroy_ok(self): response = self.client.delete(f"{base_url}/1/") self.assertEqual(response.status_code, 202) + def test_notes_404(self): + response = self.client.get(f"{base_url}/900/notes/?format=json") + self.assertEqual(response.status_code, 404) + + def test_notes_ok(self): + response = self.client.get(f"{base_url}/1/notes/?format=json") + self.assertEqual(response.status_code, 200) + self.assertJSONEqual( + response.content, + { + "INFO": [["adsfsdfsdsadf", {}]], + "ERROR": [], + "WARNING": [["adsfsdfsdsadf", {}]], + }, + ) + + def test_notes_compiled(self): + response = self.client.get(f"{base_url}/3/notes/?format=json") + self.assertEqual(response.status_code, 400) + def test_retrieve_404(self): response = self.client.get(f"{base_url}/2/tree/") self.assertEqual(response.status_code, 404)