Skip to content

Commit

Permalink
Merge pull request #2481 from DemocracyClub/expose-tags-in-api
Browse files Browse the repository at this point in the history
Expose tags in API
  • Loading branch information
symroe authored Dec 18, 2024
2 parents 8d480b4 + 428dc7b commit 85f95ef
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
17 changes: 17 additions & 0 deletions ynr/apps/api/tests/test_api_next.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,23 @@ def test_sopn_on_ballot(self):
},
)

def test_tags_on_ballot(self):
self.edinburgh_east_post_ballot.tags = {
"NUTS1": {"key": "UKM", "value": "Scotland"}
}
self.edinburgh_east_post_ballot.save()

response = self.app.get(
"/api/next/ballots/{}/".format(
self.edinburgh_east_post_ballot.ballot_paper_id
)
)
result = response.json
self.assertEqual(
result["tags"],
{"NUTS1": {"key": "UKM", "value": "Scotland"}},
)

def test_no_results_on_ballot(self):
response = self.app.get(
"/api/next/ballots/{}/".format(
Expand Down
36 changes: 36 additions & 0 deletions ynr/apps/elections/api/next/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from api.next.serializers import OrganizationSerializer
from candidates import models as candidates_models
from drf_yasg import openapi
from drf_yasg.utils import swagger_serializer_method
from elections import models as election_models
from official_documents.api.next.serializers import BallotSOPNSerializer
Expand Down Expand Up @@ -83,6 +84,39 @@ def get_ballots(self, obj):
).data


class TagsField(serializers.JSONField):
class Meta:
swagger_schema_fields = {
"type": openapi.TYPE_OBJECT,
"title": "Tags",
"description": """Freeform tags, but typically contain NUTS1 tags
for the ballot. Don't rely on this data existing, defaults to an
empty object""",
"properties": {
"NUTS1": {
"type": openapi.TYPE_OBJECT,
"properties": {
"key": {
"type": openapi.TYPE_STRING,
"example": "UKM",
"description": "The key representing the NUTS1 code (e.g., UKM for Scotland).",
},
"value": {
"type": openapi.TYPE_STRING,
"example": "Scotland",
"description": "The human-readable name associated with the NUTS1 code.",
},
},
"description": "An object containing NUTS1 tag details.",
"example": {"key": "UKM", "value": "Scotland"},
}
},
"example": {
"NUTS1": {"key": "UKM", "value": "Scotland"},
},
}


class BallotSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = candidates_models.Ballot
Expand All @@ -105,6 +139,7 @@ class Meta:
"uncontested",
"results",
"voting_system",
"tags",
)

replaces = serializers.SlugRelatedField(
Expand Down Expand Up @@ -138,6 +173,7 @@ class Meta:
)
last_updated = serializers.SerializerMethodField()
cancelled = serializers.SerializerMethodField()
tags = TagsField()

def get_last_updated(self, instance):
"""
Expand Down

0 comments on commit 85f95ef

Please sign in to comment.