Skip to content

Commit

Permalink
Merge pull request #21 from wmo-raf/dev
Browse files Browse the repository at this point in the history
Initial CAP alert import implementation
  • Loading branch information
erick-otenyo authored Apr 5, 2024
2 parents 82d758a + 02f1b98 commit 3c5d425
Show file tree
Hide file tree
Showing 21 changed files with 1,613 additions and 62 deletions.
63 changes: 45 additions & 18 deletions capeditor/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from wagtail.models import Site
from wagtailmodelchooser.blocks import ModelChooserBlock

from .forms.fields import PolygonField, MultiPolygonField, BoundaryMultiPolygonField
from .forms.fields import PolygonField, MultiPolygonField, BoundaryMultiPolygonField, PolygonOrMultiPolygonField
from .forms.widgets import CircleWidget
from .utils import file_path_mime

Expand Down Expand Up @@ -58,6 +58,26 @@ def value_from_form(self, value):
return value


class PolygonOrMultiPolygonFieldBlock(FieldBlock):
def __init__(self, required=True, help_text=None, srid=4326, **kwargs):
self.field_options = {
"required": required,
"help_text": help_text,
"srid": srid
}

super().__init__(**kwargs)

@cached_property
def field(self):
return PolygonOrMultiPolygonField(**self.field_options)

def value_from_form(self, value):
if isinstance(value, GEOSGeometry):
value = value.json
return value


class PolygonFieldBlock(FieldBlock):
def __init__(self, required=True, help_text=None, srid=4326, **kwargs):
self.field_options = {
Expand Down Expand Up @@ -174,11 +194,6 @@ def geojson(self):
polygon = self.get("boundary")
return json.loads(polygon)

# @cached_property
# def aread_desc(self):
# area_desc = self.get("areaDesc")
# return json.loads(polygon)


class AlertAreaBoundaryBlock(blocks.StructBlock):
class Meta:
Expand Down Expand Up @@ -209,15 +224,25 @@ class Meta:
class AlertAreaPolygonStructValue(StructValue):
@cached_property
def area(self):
polygon_geojson_str = self.get("polygon")
polygon_geojson_dict = json.loads(polygon_geojson_str)
geom_geojson_str = self.get("polygon")
geom_geojson_dict = json.loads(geom_geojson_str)
geom_shape = shape(geom_geojson_dict)

polygons = []

if isinstance(geom_shape, Polygon):
polygons.append(geom_shape)
else:
polygons = list(geom_shape.geoms)

polygon = shape(polygon_geojson_dict)
coords = " ".join(["{},{}".format(y, x) for x, y in list(polygon.exterior.coords)])
polygons_data = []
for polygon in polygons:
coords = " ".join(["{},{}".format(y, x) for x, y in list(polygon.exterior.reverse().coords)])
polygons_data.append(coords)

area_data = {
"areaDesc": self.get("areaDesc"),
"polygon": coords,
"polygons": polygons_data
}

if self.get("altitude"):
Expand All @@ -239,9 +264,10 @@ class Meta:

areaDesc = blocks.TextBlock(label=_("Affected areas / Regions"),
help_text=_("The text describing the affected area of the alert message"))
polygon = PolygonFieldBlock(label=_("Polygon"),
help_text=_("The paired values of points defining a polygon that delineates "
"the affected area of the alert message"))
polygon = PolygonOrMultiPolygonFieldBlock(label=_("Polygon"),
help_text=_(
"The paired values of points defining a polygon that delineates "
"the affected area of the alert message"))
altitude = blocks.CharBlock(max_length=100, required=False, label=_("Altitude"),
help_text=_("The specific or minimum altitude of the affected "
"area of the alert message"))
Expand Down Expand Up @@ -576,7 +602,7 @@ class AlertInfo(blocks.StructBlock):
('Env', _("Pollution and other environmental")),
('Transport', _("Public and private transportation")),
('Infra', _("Utility, telecommunication, other non-transport infrastructure")),
('Cbrne', _("Chemical, Biological, Radiological, Nuclear or High-Yield Explosive threat or attack")),
('CBRNE', _("Chemical, Biological, Radiological, Nuclear or High-Yield Explosive threat or attack")),
('Other', _("Other events")),
)

Expand Down Expand Up @@ -607,9 +633,10 @@ class AlertInfo(blocks.StructBlock):
help_text=_("The text denoting the type of the subject event of the alert message. You "
"can define hazards events monitored by your institution from CAP settings"))

category = blocks.ChoiceBlock(choices=CATEGORY_CHOICES, default="Met", label=_("Category"),
help_text=_("The code denoting the category of the subject"
" event of the alert message"))
category = blocks.MultipleChoiceBlock(choices=CATEGORY_CHOICES, default="Met", label=_("Category"),
help_text=_("The code denoting the category of the subject"
" event of the alert message"),
widget=forms.CheckboxSelectMultiple)
language = blocks.ChoiceBlock(choices=LANGUAGE_CHOICES, default="en", required=False, label=_("Language"),
help_text=_("The code denoting the language of the alert message"), )

Expand Down
30 changes: 30 additions & 0 deletions capeditor/cap_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ class Meta:
], heading=_("Predefined Areas"), classname="map-resize-trigger"),
])

@property
def contact_list(self):
contacts = []
for contact_block in self.contacts:
contact = contact_block.value.get("contact")
if contact:
contacts.append(contact)
return contacts

@property
def audience_list(self):
audiences = []
for audience_block in self.audience_types:
audience = audience_block.value.get("audience")
if audience:
audiences.append(audience)
return audiences


class HazardEventTypes(Orderable):
setting = ParentalKey(CapSetting, on_delete=models.PROTECT, related_name="hazard_event_types")
Expand Down Expand Up @@ -119,3 +137,15 @@ def get_default_sender():
if cap_setting and cap_setting.sender:
return cap_setting.sender
return None


def get_cap_contact_list(request):
cap_settings = CapSetting.for_request(request)
contacts_list = cap_settings.contact_list
return contacts_list


def get_cap_audience_list(request):
cap_settings = CapSetting.for_request(request)
audience_list = cap_settings.audience_list
return audience_list
Loading

0 comments on commit 3c5d425

Please sign in to comment.