<s> from __future__ import absolute_import , unicode_literals <EOL> from django . conf import settings <EOL> from django . db . models . signals import post_save , post_delete <EOL> from django . dispatch import receiver <EOL> from kombu import BrokerConnection <EOL> from kombu . common import maybe_declare <EOL> from kombu . pools import producers <EOL> from . models import Incident , AreaOfInterest <EOL> from . queues import notifications_exchange <EOL> def send_notification ( notification ) : <EOL> with BrokerConnection ( settings . AMPQ_URL ) as connection : <EOL> with producers [ connection ] . acquire ( block = True ) as producer : <EOL> maybe_declare ( notifications_exchange , producer . channel ) <EOL> producer . publish ( <EOL> notification , <EOL> exchange = notifications_exchange , <EOL> routing_key = '<STR_LIT>' <EOL> ) <EOL> @ receiver ( post_save , sender = Incident ) <EOL> def incident_post_save ( sender , ** kwargs ) : <EOL> send_notification ( dict ( <EOL> type = '<STR_LIT>' , <EOL> created = kwargs [ '<STR_LIT>' ] , <EOL> feature = kwargs [ '<STR_LIT>' ] . geojson_feature <EOL> ) ) <EOL> if not kwargs [ '<STR_LIT>' ] . closed : <EOL> areas_of_interest = [ <EOL> area_of_interest . geojson_feature for area_of_interest in AreaOfInterest . objects . filter ( <EOL> polygon__contains = kwargs [ '<STR_LIT>' ] . location , <EOL> severity__in = kwargs [ '<STR_LIT>' ] . alert_severities , <EOL> ) <EOL> ] <EOL> if areas_of_interest : <EOL> send_notification ( dict ( <EOL> type = '<STR_LIT>' , <EOL> feature = kwargs [ '<STR_LIT>' ] . geojson_feature , <EOL> areas_of_interest = [ <EOL> { <EOL> '<STR_LIT:id>' : area_of_interest [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT:name>' : area_of_interest [ '<STR_LIT>' ] [ '<STR_LIT:name>' ] , <EOL> '<STR_LIT>' : area_of_interest [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> '<STR_LIT:url>' : area_of_interest [ '<STR_LIT>' ] [ '<STR_LIT:url>' ] , <EOL> } <EOL> for area_of_interest in areas_of_interest <EOL> ] <EOL> ) ) <EOL> @ receiver ( post_save , sender = AreaOfInterest ) <EOL> def area_of_interest_post_save ( sender , ** kwargs ) : <EOL> send_notification ( dict ( <EOL> type = '<STR_LIT>' , <EOL> created = kwargs [ '<STR_LIT>' ] , <EOL> feature = kwargs [ '<STR_LIT>' ] . geojson_feature <EOL> ) ) <EOL> @ receiver ( post_delete , sender = Incident ) <EOL> @ receiver ( post_delete , sender = AreaOfInterest ) <EOL> def post_delete ( sender , ** kwargs ) : <EOL> send_notification ( dict ( <EOL> type = '<STR_LIT>' , <EOL> feature = kwargs [ '<STR_LIT>' ] . geojson_feature <EOL> ) ) </s>
