Skip to content

Commit

Permalink
Merge pull request #444 from open-contracting/443-collection-notes
Browse files Browse the repository at this point in the history
feat: add collection notes endpoint
  • Loading branch information
yolile authored Dec 18, 2024
2 parents 88ceeba + 01ad267 commit 6382098
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
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 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
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
20 changes: 20 additions & 0 deletions process/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
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_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)
Expand Down

0 comments on commit 6382098

Please sign in to comment.