2020-07-06 10:53:40 +01:00
|
|
|
import itertools
|
|
|
|
|
|
|
|
|
|
from notifications_utils.serialised_model import SerialisedModelCollection
|
2020-08-10 10:46:31 +01:00
|
|
|
from werkzeug.utils import cached_property
|
2020-07-06 10:53:40 +01:00
|
|
|
|
2020-07-24 16:45:41 +01:00
|
|
|
from .repo import BroadcastAreasRepository
|
2020-07-24 12:54:22 +01:00
|
|
|
|
|
|
|
|
|
2020-07-31 13:21:21 +01:00
|
|
|
class SortableMixin:
|
2020-07-06 10:53:40 +01:00
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
return f'{self.__class__.__name__}(<{self.id}>)'
|
|
|
|
|
|
|
|
|
|
def __lt__(self, other):
|
|
|
|
|
# Implementing __lt__ means any classes inheriting from this
|
|
|
|
|
# method are sortable
|
2020-07-31 13:21:21 +01:00
|
|
|
return self.name < other.name
|
2020-07-24 16:47:12 +01:00
|
|
|
|
|
|
|
|
|
2020-07-06 10:53:40 +01:00
|
|
|
class GetItemByIdMixin:
|
|
|
|
|
def get(self, id):
|
|
|
|
|
for item in self:
|
|
|
|
|
if item.id == id:
|
|
|
|
|
return item
|
|
|
|
|
raise KeyError(id)
|
|
|
|
|
|
|
|
|
|
|
2020-07-31 13:21:21 +01:00
|
|
|
class BroadcastArea(SortableMixin):
|
2020-07-06 10:53:40 +01:00
|
|
|
|
2020-07-24 16:47:12 +01:00
|
|
|
def __init__(self, row):
|
2020-08-12 09:34:13 +01:00
|
|
|
self.id, self.name = row
|
2020-07-06 10:53:40 +01:00
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
|
return self.id == other.id
|
|
|
|
|
|
2020-08-24 14:43:28 +01:00
|
|
|
@cached_property
|
2020-07-28 18:38:40 +01:00
|
|
|
def polygons(self):
|
2020-08-24 14:43:28 +01:00
|
|
|
return BroadcastAreasRepository().get_polygons_for_area(self.id)
|
2020-07-28 18:38:40 +01:00
|
|
|
|
2020-08-24 14:43:28 +01:00
|
|
|
@cached_property
|
2020-07-28 18:38:40 +01:00
|
|
|
def simple_polygons(self):
|
2020-08-24 14:43:28 +01:00
|
|
|
return BroadcastAreasRepository().get_simple_polygons_for_area(self.id)
|
2020-07-30 17:24:20 +01:00
|
|
|
|
2020-07-31 13:21:21 +01:00
|
|
|
@property
|
|
|
|
|
def sub_areas(self):
|
|
|
|
|
return [
|
|
|
|
|
BroadcastArea(row)
|
|
|
|
|
for row in BroadcastAreasRepository().get_all_areas_for_group(self.id)
|
|
|
|
|
]
|
2020-07-06 10:53:40 +01:00
|
|
|
|
2020-07-31 13:21:21 +01:00
|
|
|
|
|
|
|
|
class BroadcastAreaLibrary(SerialisedModelCollection, SortableMixin, GetItemByIdMixin):
|
2020-07-06 10:53:40 +01:00
|
|
|
|
|
|
|
|
model = BroadcastArea
|
|
|
|
|
|
2020-07-31 13:21:21 +01:00
|
|
|
def __init__(self, row):
|
2020-08-13 17:33:58 +01:00
|
|
|
id, name, name_singular, is_group = row
|
2020-07-31 13:21:21 +01:00
|
|
|
self.id = id
|
|
|
|
|
self.name = name
|
2020-08-13 17:33:58 +01:00
|
|
|
self.name_singular = name_singular
|
2020-07-31 13:21:21 +01:00
|
|
|
self.is_group = bool(is_group)
|
2020-08-12 10:17:45 +01:00
|
|
|
self.items = BroadcastAreasRepository().get_all_areas_for_library(self.id)
|
2020-07-06 10:53:40 +01:00
|
|
|
|
2020-07-24 16:47:12 +01:00
|
|
|
def get_examples(self):
|
|
|
|
|
return BroadcastAreasRepository().get_library_description(self.id)
|
2020-07-06 10:53:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class BroadcastAreaLibraries(SerialisedModelCollection, GetItemByIdMixin):
|
|
|
|
|
|
|
|
|
|
model = BroadcastAreaLibrary
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
2020-08-12 10:13:16 +01:00
|
|
|
self.items = BroadcastAreasRepository().get_libraries()
|
2020-07-06 10:53:40 +01:00
|
|
|
|
|
|
|
|
def get_areas(self, *area_ids):
|
2020-07-09 19:10:51 +01:00
|
|
|
# allow people to call `get_areas('a', 'b') or get_areas(['a', 'b'])`
|
|
|
|
|
if len(area_ids) == 1 and isinstance(area_ids[0], list):
|
|
|
|
|
area_ids = area_ids[0]
|
|
|
|
|
|
2020-07-24 16:47:12 +01:00
|
|
|
areas = BroadcastAreasRepository().get_areas(area_ids)
|
|
|
|
|
return [BroadcastArea(area) for area in areas]
|
2020-07-06 10:53:40 +01:00
|
|
|
|
|
|
|
|
def get_polygons_for_areas_long_lat(self, *area_ids):
|
|
|
|
|
return list(itertools.chain(*(
|
|
|
|
|
area.polygons
|
|
|
|
|
for area in self.get_areas(*area_ids)
|
|
|
|
|
)))
|
|
|
|
|
|
|
|
|
|
def get_polygons_for_areas_lat_long(self, *area_ids):
|
|
|
|
|
return [
|
|
|
|
|
[[long, lat] for lat, long in polygon]
|
|
|
|
|
for polygon in self.get_polygons_for_areas_long_lat(*area_ids)
|
|
|
|
|
]
|
|
|
|
|
|
2020-08-06 13:38:15 +01:00
|
|
|
def get_simple_polygons_for_areas_long_lat(self, *area_ids):
|
|
|
|
|
return list(itertools.chain(*(
|
|
|
|
|
area.simple_polygons
|
|
|
|
|
for area in self.get_areas(*area_ids)
|
|
|
|
|
)))
|
|
|
|
|
|
|
|
|
|
def get_simple_polygons_for_areas_lat_long(self, *area_ids):
|
|
|
|
|
return [
|
|
|
|
|
[[long, lat] for lat, long in polygon]
|
|
|
|
|
for polygon in self.get_simple_polygons_for_areas_long_lat(*area_ids)
|
|
|
|
|
]
|
|
|
|
|
|
2020-07-06 10:53:40 +01:00
|
|
|
|
|
|
|
|
broadcast_area_libraries = BroadcastAreaLibraries()
|