-
Notifications
You must be signed in to change notification settings - Fork 19
upgrade uk-election-timetables, add notice_of_election_deadline and sopn_publish_deadline fields #2613
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
Open
chris48s
wants to merge
7
commits into
master
Choose a base branch
from
timetables5
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
upgrade uk-election-timetables, add notice_of_election_deadline and sopn_publish_deadline fields #2613
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
92ad019
update timetables library
chris48s d7e63e6
add notice_of_election_deadline and sopn_publish_deadline fields
chris48s 9cdffab
handle ref/NoE edge case in one place
chris48s 9b82687
add test for NI timetable
chris48s 6d97c5c
update test assertions
chris48s 8765ce0
add tests for clean() method
chris48s 6886766
improve migration
chris48s File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
every_election/apps/elections/migrations/0089_more_timetable_fields.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # Generated by Django 5.2.15 on 2026-07-08 09:13 | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("elections", "0088_fix_ni_timetable_fields"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name="election", | ||
| name="notice_of_election_deadline", | ||
| field=models.DateField( | ||
| blank=True, | ||
| help_text="Deadline to publish Notice of Election document", | ||
| null=True, | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name="election", | ||
| name="sopn_publish_deadline", | ||
| field=models.DateField( | ||
| blank=True, | ||
| help_text="Deadline to publish SOPN document", | ||
| null=True, | ||
| ), | ||
| ), | ||
| ] |
105 changes: 105 additions & 0 deletions
105
every_election/apps/elections/migrations/0090_backfill_new_timetable_fields.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| # Generated by Django 5.2.13 on 2026-06-16 10:30 | ||
|
|
||
| from django.db import migrations | ||
| from django.db.models import F | ||
| from uk_election_timetables.calendars import Country | ||
| from uk_election_timetables.election_ids import from_election_id | ||
|
|
||
| country_map = { | ||
| "WLS": Country.WALES, | ||
| "ENG": Country.ENGLAND, | ||
| "NIR": Country.NORTHERN_IRELAND, | ||
| "SCT": Country.SCOTLAND, | ||
| "GBN": None, | ||
| } | ||
|
|
||
|
|
||
| def backfill_new_timetable_fields(apps, schema_editor): | ||
| Election = apps.get_model("elections", "Election") | ||
| qs = Election.private_objects.using(schema_editor.connection.alias) | ||
|
|
||
| elections_to_update = [] | ||
| city_of_london_to_update = [] | ||
|
|
||
| for election in qs.iterator(): | ||
| # we can't calculate a timetable for en election with | ||
| # no polling day | ||
| if election.poll_open_date is None: | ||
| continue | ||
|
|
||
| # only calculate a timetable for ballots | ||
| if election.group_type is not None: | ||
| continue | ||
|
|
||
| # We don't have logic for computing timetable for | ||
| # EU Parliament elections but some do exist in the DB | ||
| # from back in the day | ||
| if election.election_id.startswith("europarl."): | ||
| continue | ||
|
|
||
| # There is no nominations or SOPN for refenda | ||
| # Notice of election deadline is applicable | ||
| # but not implemented | ||
| if election.election_id.startswith("ref."): | ||
| continue | ||
|
|
||
| area = election.division or election.organisation | ||
| territory_code = ( | ||
| area.territory_code or election.organisation.territory_code | ||
| ) | ||
|
|
||
| timetable = from_election_id( | ||
| election.election_id, country=country_map[territory_code] | ||
| ) | ||
|
|
||
| # populate the new fields we just added in 0089 | ||
| election.notice_of_election_deadline = ( | ||
| timetable.notice_of_election_deadline | ||
| ) | ||
| election.sopn_publish_deadline = timetable.sopn_publish_deadline | ||
| elections_to_update.append(election) | ||
|
|
||
| # We used to make a slightly different assumption about | ||
| # City of London elections. This changed in 5.0.0 so | ||
| # update close_of_nominations for City of London only | ||
| if election.election_id.startswith("local.city-of-london."): | ||
| election.close_of_nominations = timetable.close_of_nominations | ||
| city_of_london_to_update.append(election) | ||
|
|
||
| batch_size = 2000 | ||
| for i in range(0, len(elections_to_update), batch_size): | ||
| qs.bulk_update( | ||
| elections_to_update[i : i + batch_size], | ||
| ["notice_of_election_deadline", "sopn_publish_deadline"], | ||
| ) | ||
| for i in range(0, len(city_of_london_to_update), batch_size): | ||
| qs.bulk_update( | ||
| city_of_london_to_update[i : i + batch_size], | ||
| ["close_of_nominations"], | ||
| ) | ||
|
|
||
|
|
||
| def clear_new_timetable_fields(apps, schema_editor): | ||
| Election = apps.get_model("elections", "Election") | ||
| qs = Election.private_objects.using(schema_editor.connection.alias) | ||
|
|
||
| qs.filter(election_id__startswith="local.city-of-london.").update( | ||
| close_of_nominations=F("sopn_publish_deadline") | ||
| ) | ||
|
|
||
| qs.update( | ||
| notice_of_election_deadline=None, | ||
| sopn_publish_deadline=None, | ||
| ) | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("elections", "0089_more_timetable_fields"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RunPython( | ||
| backfill_new_timetable_fields, clear_new_timetable_fields | ||
| ) | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is another migration that is going to touch the modified date on basically every ballot object in EE
I do at least know this time round that it will break the EE --> YNR sync and what to do about it when it does