diff --git a/requirements/base.txt b/requirements/base.txt index b27341445..619431681 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -31,6 +31,7 @@ djangorestframework-jsonp==1.0.2 djangorestframework-csv==2.1.1 djangorestframework==3.14 drf-yasg==1.21.5 +uk-election-timetables==2.4.1 factory-boy==3.2.1 Faker==19.2.0 freezegun==1.2.2 diff --git a/ynr/apps/candidates/helpers/helpers.py b/ynr/apps/candidates/helpers/helpers.py new file mode 100644 index 000000000..09934b2bb --- /dev/null +++ b/ynr/apps/candidates/helpers/helpers.py @@ -0,0 +1,20 @@ +from uk_election_timetables.calendars import Country +from uk_election_timetables.election_ids import from_election_id + + +def get_election_timetable(slug, territory): + country = { + "ENG": Country.ENGLAND, + "WLS": Country.WALES, + "SCT": Country.SCOTLAND, + "NIR": Country.NORTHERN_IRELAND, + } + + if slug.startswith("local") and territory not in country: + return None + + try: + return from_election_id(slug, country[territory]) + + except BaseException: + return None diff --git a/ynr/apps/candidates/models/popolo_extra.py b/ynr/apps/candidates/models/popolo_extra.py index d0bbeb225..04631bfc0 100644 --- a/ynr/apps/candidates/models/popolo_extra.py +++ b/ynr/apps/candidates/models/popolo_extra.py @@ -1,6 +1,7 @@ import datetime import hashlib +from candidates.helpers.helpers import get_election_timetable from candidates.models import LoggedAction from candidates.models.auth import TRUSTED_TO_LOCK_GROUP_NAME from candidates.models.db import ActionType @@ -558,6 +559,16 @@ def is_welsh_run(self): return self.tags.get("NUTS1", {}).get("key") == "UKL" + @property + def expected_sopn_date(self): + try: + return get_election_timetable( + self.ballot_paper_id, self.post.territory_code + ).sopn_publish_date + + except AttributeError: + return None + class PartySet(models.Model): slug = models.CharField(max_length=256, unique=True)