Introduce "areas_2" so we can repurpose "areas"

Currently we have:

- An "areas" column in the DB that stores a JSON blob.
- An "areas" field inside the "areas" JSON that stores area IDs.
- Each field has to be manually copied into the JSON column.

We want to move to:

- An "areas" column in the DB (unchanged).
- An "ids" field inside the "areas" JSON (to replace "areas").
- The Admin app sending other data inside an "areas" JSON field.

The API design for areas is confusing and difficult to extend.
Here we duplicate the current API functionality using an "areas_2"
field. Once the Admin app is using this field, we'll be able to
rename it to just "areas", which is where we want to get to.

In the next commits we'll build on this to support the migration
from "areas"."areas" to "areas"."ids".
This commit is contained in:
Ben Thorner
2021-08-25 13:49:18 +01:00
parent 277db4e6c7
commit fd7ebbebb0
5 changed files with 119 additions and 18 deletions

View File

@@ -133,12 +133,25 @@ def create_broadcast_message(service_id):
content = str(temporary_template)
reference = data['reference']
# TEMPORARY: while we repurpose "areas"
areas = {}
areas_2 = data.get("areas_2", {})
if "areas" in data:
areas["areas"] = data["areas"]
if "simple_polygons" in data:
areas["simple_polygons"] = data["simple_polygons"]
if "areas" in areas_2:
areas["areas"] = areas_2["areas"]
if "simple_polygons" in areas_2:
areas["simple_polygons"] = areas_2["simple_polygons"]
broadcast_message = BroadcastMessage(
service_id=service.id,
template_id=template_id,
template_version=template.version if template else None,
personalisation=personalisation,
areas={"areas": data.get("areas", []), "simple_polygons": data.get("simple_polygons", [])},
areas=areas,
status=BroadcastStatusType.DRAFT,
starts_at=_parse_nullable_datetime(data.get('starts_at')),
finishes_at=_parse_nullable_datetime(data.get('finishes_at')),
@@ -170,7 +183,20 @@ def update_broadcast_message(service_id, broadcast_message_id):
status_code=400
)
if ('areas' in data and 'simple_polygons' not in data) or ('areas' not in data and 'simple_polygons' in data):
# TEMPORARY: while we repurpose "areas"
areas = {}
areas_2 = data.get("areas_2", {})
if "areas" in data:
areas["areas"] = data["areas"]
if "simple_polygons" in data:
areas["simple_polygons"] = data["simple_polygons"]
if "areas" in areas_2:
areas["areas"] = areas_2["areas"]
if "simple_polygons" in areas_2:
areas["simple_polygons"] = areas_2["simple_polygons"]
if ('areas' in areas and 'simple_polygons' not in areas) or ('areas' not in areas and 'simple_polygons' in areas):
raise InvalidRequest(
f'Cannot update broadcast_message {broadcast_message.id}, areas or polygons are missing.',
status_code=400
@@ -182,8 +208,8 @@ def update_broadcast_message(service_id, broadcast_message_id):
broadcast_message.starts_at = _parse_nullable_datetime(data['starts_at'])
if 'finishes_at' in data:
broadcast_message.finishes_at = _parse_nullable_datetime(data['finishes_at'])
if 'areas' in data and 'simple_polygons' in data:
broadcast_message.areas = {"areas": data["areas"], "simple_polygons": data["simple_polygons"]}
if 'areas' in areas and 'simple_polygons' in areas:
broadcast_message.areas = areas
dao_save_object(broadcast_message)