mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-15 15:28:50 -04:00
DRY-up getting and setting area_ids
Previously we just held the new area_ids in memory. Setting them on the object means we can reuse its functionality to get polygons and also avoids confusion if in future we try to continue using the object after calling "add_areas" or "remove_areas".
This commit is contained in:
@@ -88,23 +88,30 @@ class BroadcastMessage(JSONModel):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def areas(self):
|
def areas(self):
|
||||||
area_ids = self._dict['areas_2']['ids']
|
|
||||||
polygons = self._dict['areas_2']['simple_polygons']
|
polygons = self._dict['areas_2']['simple_polygons']
|
||||||
library_areas = self.get_areas(area_ids)
|
library_areas = self.get_areas(self.area_ids)
|
||||||
|
|
||||||
if library_areas:
|
if library_areas:
|
||||||
if len(library_areas) != len(area_ids):
|
if len(library_areas) != len(self.area_ids):
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
f'BroadcastMessage has {len(area_ids)} areas '
|
f'BroadcastMessage has {len(self.area_ids)} areas '
|
||||||
f'but {len(library_areas)} found in the library'
|
f'but {len(library_areas)} found in the library'
|
||||||
)
|
)
|
||||||
return library_areas
|
return library_areas
|
||||||
|
|
||||||
return CustomBroadcastAreas(
|
return CustomBroadcastAreas(
|
||||||
area_ids=area_ids,
|
area_ids=self.area_ids,
|
||||||
polygons=polygons,
|
polygons=polygons,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def area_ids(self):
|
||||||
|
return self._dict['areas_2']['ids']
|
||||||
|
|
||||||
|
@area_ids.setter
|
||||||
|
def area_ids(self, value):
|
||||||
|
self._dict['areas_2']['ids'] = value
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def ancestor_areas(self):
|
def ancestor_areas(self):
|
||||||
return sorted(set(self._ancestor_areas_iterator))
|
return sorted(set(self._ancestor_areas_iterator))
|
||||||
@@ -219,23 +226,19 @@ class BroadcastMessage(JSONModel):
|
|||||||
return polygons.smooth.simplify if len(areas) > 1 else polygons
|
return polygons.smooth.simplify if len(areas) > 1 else polygons
|
||||||
|
|
||||||
def add_areas(self, *new_area_ids):
|
def add_areas(self, *new_area_ids):
|
||||||
area_ids = list(OrderedSet(
|
self.area_ids = list(OrderedSet(self.area_ids + list(new_area_ids)))
|
||||||
self._dict['areas_2']['ids'] + list(new_area_ids)
|
|
||||||
))
|
|
||||||
simple_polygons = self.get_simple_polygons(areas=self.get_areas(area_ids))
|
|
||||||
|
|
||||||
self._update(areas_2={
|
self._update(areas_2={
|
||||||
'ids': area_ids,
|
'ids': self.area_ids,
|
||||||
'simple_polygons': simple_polygons.as_coordinate_pairs_lat_long
|
'simple_polygons': self.simple_polygons.as_coordinate_pairs_lat_long
|
||||||
})
|
})
|
||||||
|
|
||||||
def remove_area(self, area_id):
|
def remove_area(self, area_id):
|
||||||
area_ids = list(set(self._dict['areas_2']['ids']) - {area_id})
|
self.area_ids = list(set(self._dict['areas_2']['ids']) - {area_id})
|
||||||
simple_polygons = self.get_simple_polygons(areas=self.get_areas(area_ids))
|
|
||||||
|
|
||||||
self._update(areas_2={
|
self._update(areas_2={
|
||||||
'ids': area_ids,
|
'ids': self.area_ids,
|
||||||
'simple_polygons': simple_polygons.as_coordinate_pairs_lat_long
|
'simple_polygons': self.simple_polygons.as_coordinate_pairs_lat_long
|
||||||
})
|
})
|
||||||
|
|
||||||
def _set_status_to(self, status):
|
def _set_status_to(self, status):
|
||||||
|
|||||||
Reference in New Issue
Block a user