Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add collection notes endpoint #444

Merged
merged 5 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/_static/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 compiled collection and its parent collection.
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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions process/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,21 @@ def metadata(self, request, pk=None):

return Response(metadata)

@action(detail=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other views use @extend_schema to define the output format. This is only to improve the OpenAPI docs. You can create a follow-up issue instead.

def notes(self, request, pk=None):
"""Return the notes for the compiled collection and its parent collection."""
compiled_collection = get_object_or_404(Collection, pk=pk)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why request the compiled collection? I think it'd be more generic to request the root collection, and then get notes from all child collections.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was following the same approach as the metadata endpoint, as I thought the registry would probably ask for the compiled collection. But you are right and I will change it to the root collection instead

collection = compiled_collection.get_root_parent()

if compiled_collection.transform_type != Collection.Transform.COMPILE_RELEASES:
return Response("The collection must be a compiled collection", status=status.HTTP_400_BAD_REQUEST)

notes_db = CollectionNote.objects.filter(collection_id__in=[compiled_collection.id, collection.id])
notes = {level: [] for level in CollectionNote.Level.values} # noqa: PD011
for note in notes_db:
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):
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/complete_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
"fields": {
"collection": 1,
"note": "adsfsdfsdsadf",
"code": "INFO",
"data": {},
"stored_at": "2020-12-29T13:50:28.589"
}
},
Expand All @@ -89,6 +91,8 @@
"fields": {
"collection": 2,
"note": "adsfsdfsdsadf",
"code": "WARNING",
"data": {},
"stored_at": "2020-12-29T13:50:28.597"
}
},
Expand Down
20 changes: 20 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_not_compiled(self):
response = self.client.get(f"{base_url}/1/notes/?format=json")
self.assertEqual(response.status_code, 400)

def test_notes_ok(self):
response = self.client.get(f"{base_url}/3/notes/?format=json")
self.assertEqual(response.status_code, 200)
self.assertJSONEqual(
response.content,
{
"INFO": [["adsfsdfsdsadf", {}]],
"ERROR": [],
"WARNING": [],
},
)

def test_retrieve_404(self):
response = self.client.get(f"{base_url}/2/tree/")
self.assertEqual(response.status_code, 404)
Expand Down
Loading