-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(email_alerting_code): Alert users when restrictions change (#32)
- Loading branch information
Showing
4 changed files
with
140 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from datetime import datetime | ||
from typing import Optional, Any | ||
from enum import Enum | ||
|
||
class Event(Enum): | ||
DECREE_CREATION = 'decree_creation' | ||
DECREE_REPEAL = 'decree_repeal' | ||
|
||
|
||
class Decree: | ||
def __init__(self, id: Optional[int], external_id: str, geozone_id: int, alert_level: str, start_date: datetime, end_date: datetime, document: str): | ||
self.id = id | ||
self.external_id = external_id | ||
self.geozone_id = geozone_id | ||
self.alert_level = alert_level | ||
self.start_date = start_date | ||
self.end_date = end_date | ||
self.document = document | ||
|
||
def get_geozone_id(self): | ||
return self.geozone_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from typing import List, Optional | ||
from models import Decree, Event | ||
|
||
|
||
def find_all_decree_events(cursor) -> List[Event]: | ||
cursor.execute("SELECT * FROM event_store WHERE type IN ('decree_creation', 'decree_repeal');") | ||
results = cursor.fetchall() | ||
return [] if not results else results | ||
|
||
def get_decree_by_id(cursor, id: str) -> Optional[Decree]: | ||
query = 'SELECT * FROM decree WHERE id = %s' | ||
parameters = (id,) | ||
cursor.execute(query, parameters) | ||
|
||
result = cursor.fetchone() | ||
if not result: | ||
return None | ||
|
||
return Decree( | ||
id=result.get('id'), | ||
external_id=result.get('external_id'), | ||
geozone_id=result.get('geozone_id'), | ||
alert_level=result.get('alert_level'), | ||
start_date=result.get('start_date'), | ||
end_date=result.get('end_date'), | ||
document=result.get('document') | ||
) | ||
|
||
|
||
def find_user_emails_by_geozone_id(cursor, geozone_id: str) -> List[str]: | ||
query = """ | ||
SELECT DISTINCT email | ||
FROM alert_subscription | ||
CROSS JOIN geozone | ||
WHERE geozone.id = %s | ||
AND ST_Contains( | ||
geozone.geom, | ||
ST_SetSRID( | ||
ST_MakePoint(alert_subscription.longitude, alert_subscription.latitude), | ||
4326 | ||
) | ||
); | ||
""" | ||
params = (geozone_id,) | ||
cursor.execute(query, params) | ||
emails = cursor.fetchall() | ||
return emails | ||
|
||
def delete_event(cursor, event_id: int): | ||
query = 'DELETE FROM event_store WHERE id = %s' | ||
parameters = (event_id,) | ||
cursor.execute(query, parameters) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ psycopg2 | |
requests | ||
email-validator | ||
geopandas | ||
mailjet-rest |