Add command to migrate custom broadcast areas

Unlike broadcasts created in the Admin app, these are only expected
to have "names" and "simple_polygons" in their areas column [1].

The migration command in the Admin app [2] isn't suitable for these
broadcasts as it would try to aggregate their areas, etc.

I've put a conditional on "areas" being present (in the areas column)
so this command doesn't pick up any new custom broadcasts.

[1]: 023a06d5fbo
[2]: https://github.com/alphagov/notifications-admin/pull/4011
This commit is contained in:
Ben Thorner
2021-09-06 17:35:42 +01:00
parent 3779146cc5
commit b639031b9f

View File

@@ -914,3 +914,26 @@ def populate_annual_billing_with_defaults(year, missing_services_only):
for service in active_services:
set_default_free_allowance_for_service(service, year)
@notify_command(name='tmp-backfill-custom-broadcast-areas')
def tmp_backfill_custom_broadcast_areas():
from app import db
from app.models import BroadcastMessage
custom_broadcasts = BroadcastMessage.query \
.filter(BroadcastMessage.api_key_id != None) \
.filter(BroadcastMessage.areas.has_key('areas')) # noqa
for broadcast in custom_broadcasts:
old_areas = broadcast.areas
new_areas = {
'names': old_areas['areas'],
'simple_polygons': old_areas['simple_polygons']
}
broadcast.areas = new_areas
print(f'Migrating {broadcast.id}')
db.session.commit()