mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-20 22:40:31 -04:00
Support broadcasts with unidentifiable areas
The original code to raise the exception was flawed: if a broadcast
only had a single area that was invalid, we would assume it was a
custom broadcast [1]. Since the recent changes [2] fixed the flaw
we're now getting exceptions for broadcasts of this kind.
It's not practical to go and manually fix the invalid broadcasts,
and the likelihood is there will be more in future as the set of
areas we support changes. This takes a pragmatic approach of simply
logging the issue and pretending such broadcasts are custom.
[1]: 926ada2f21
[2]: https://github.com/alphagov/notifications-admin/pull/4014/files#diff-2dd8f77d6df281e7674b20263cdf27a3d58b839dc5930c0087ac8b9749b313e4R92
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import itertools
|
import itertools
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
|
from flask import current_app
|
||||||
from notifications_utils.polygons import Polygons
|
from notifications_utils.polygons import Polygons
|
||||||
from notifications_utils.template import BroadcastPreviewTemplate
|
from notifications_utils.template import BroadcastPreviewTemplate
|
||||||
from orderedset import OrderedSet
|
from orderedset import OrderedSet
|
||||||
@@ -92,12 +93,19 @@ class BroadcastMessage(JSONModel):
|
|||||||
if 'ids' in self._dict['areas']:
|
if 'ids' in self._dict['areas']:
|
||||||
library_areas = self.get_areas(self.area_ids)
|
library_areas = self.get_areas(self.area_ids)
|
||||||
|
|
||||||
if len(library_areas) != len(self.area_ids):
|
if len(library_areas) == len(self.area_ids):
|
||||||
raise RuntimeError(
|
return library_areas
|
||||||
f'BroadcastMessage has {len(self.area_ids)} areas '
|
else:
|
||||||
f'but {len(library_areas)} found in the library'
|
# it's possible an old broadcast may refer to areas that
|
||||||
|
# are no longer part of our area libraries; in this case
|
||||||
|
# we should just treat the whole thing as a custom broadcast,
|
||||||
|
# which isn't great as our code doesn't support editing its
|
||||||
|
# areas, but we don't expect this to happen often
|
||||||
|
current_app.logger.warn(
|
||||||
|
f'BroadcastMessage has {len(self._dict["areas"])} areas '
|
||||||
|
f'but {len(library_areas)} found in the library. Treating '
|
||||||
|
f'{self.id} as a custom broadcast.'
|
||||||
)
|
)
|
||||||
return library_areas
|
|
||||||
|
|
||||||
polygons = self._dict['areas'].get('simple_polygons', [])
|
polygons = self._dict['areas'].get('simple_polygons', [])
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from app.broadcast_areas.models import CustomBroadcastAreas
|
||||||
from app.models.broadcast_message import BroadcastMessage
|
from app.models.broadcast_message import BroadcastMessage
|
||||||
from tests import broadcast_message_json
|
from tests import broadcast_message_json
|
||||||
|
|
||||||
@@ -70,17 +71,23 @@ def test_areas(
|
|||||||
assert len(list(broadcast_message.areas)) == expected_length
|
assert len(list(broadcast_message.areas)) == expected_length
|
||||||
|
|
||||||
|
|
||||||
def test_areas_raises_for_missing_areas():
|
def test_areas_treats_missing_ids_as_custom_broadcast(notify_admin):
|
||||||
broadcast_message = BroadcastMessage(broadcast_message_json(
|
broadcast_message = BroadcastMessage(broadcast_message_json(
|
||||||
area_ids=[
|
areas={
|
||||||
'wd20-E05009372',
|
'ids': [
|
||||||
'something else',
|
'wd20-E05009372',
|
||||||
],
|
'something else',
|
||||||
|
],
|
||||||
|
# although the IDs may no longer be usable, we can
|
||||||
|
# expect the broadcast to have names and polygons,
|
||||||
|
# which is enough to show the user something
|
||||||
|
'names': [
|
||||||
|
'wd20 name',
|
||||||
|
'something else name'
|
||||||
|
],
|
||||||
|
'simple_polygons': [[[1, 2]]]
|
||||||
|
}
|
||||||
))
|
))
|
||||||
|
|
||||||
with pytest.raises(RuntimeError) as exception:
|
assert len(list(broadcast_message.areas)) == 2
|
||||||
broadcast_message.areas
|
assert type(broadcast_message.areas) == CustomBroadcastAreas
|
||||||
|
|
||||||
assert str(exception.value) == (
|
|
||||||
'BroadcastMessage has 2 areas but 1 found in the library'
|
|
||||||
)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user