Skip to content

Commit 0726b9d

Browse files
authored
Merge pull request #11 from flightaware/BCK-7007_delete_alert_config
Enable Deleting Alert Configurations by adding endpoint to delete them.
2 parents b835b38 + 4bb185d commit 0726b9d

File tree

1 file changed

+65
-4
lines changed

1 file changed

+65
-4
lines changed

alerts_backend/python/app.py

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
Date,
2424
select,
2525
DateTime,
26+
delete
2627
)
2728

2829
AEROAPI_BASE_URL = "https://aeroapi.flightaware.com/aeroapi"
@@ -70,9 +71,8 @@
7071
"aeroapi_alerts",
7172
metadata_obj,
7273
Column("id", Integer, primary_key=True, autoincrement=True),
73-
Column(
74-
"time_alert_received", DateTime(timezone=True), server_default=func.now()
75-
), # Store time in UTC that the alert was received
74+
# Store time in UTC that the alert was received
75+
Column("time_alert_received", DateTime(timezone=True), server_default=func.now()),
7676
Column("long_description", Text),
7777
Column("short_description", Text),
7878
Column("summary", Text),
@@ -124,6 +124,67 @@ def insert_into_table(data_to_insert: Dict[str, Any], table: Table) -> int:
124124
return 0
125125

126126

127+
def delete_from_table(fa_alert_id: int) -> int:
128+
"""
129+
Delete alert config from SQL Alert Configurations table based on FA Alert ID.
130+
Returns 0 on success, -1 otherwise.
131+
"""
132+
try:
133+
with engine.connect() as conn:
134+
stmt = delete(aeroapi_alert_configurations).where(aeroapi_alert_configurations.c.fa_alert_id == fa_alert_id)
135+
conn.execute(stmt)
136+
conn.commit()
137+
logger.info(f"Data successfully deleted from {aeroapi_alert_configurations.name}")
138+
except exc.SQLAlchemyError as e:
139+
logger.error(f"SQL error occurred during deletion from table {aeroapi_alert_configurations.name}: {e}")
140+
return -1
141+
return 0
142+
143+
144+
@app.route("/delete", methods=["POST"])
145+
def delete_alert() -> Response:
146+
"""
147+
Function to delete the alert given (with key "fa_alert_id" in the payload).
148+
Deletes the given alert via AeroAPI DELETE call and then deletes it from the
149+
SQLite database. Returns JSON Response in form {"Success": True/False,
150+
"Description": <A detailed description of the response>}
151+
"""
152+
r_success: bool = False
153+
r_description: str
154+
# Process json
155+
content_type = request.headers.get("Content-Type")
156+
data: Dict[str, Any]
157+
158+
if content_type != "application/json":
159+
r_description = "Invalid content sent"
160+
else:
161+
data = request.json
162+
fa_alert_id = data['fa_alert_id']
163+
api_resource = f"/alerts/{fa_alert_id}"
164+
logger.info(f"Making AeroAPI request to DELETE {api_resource}")
165+
result = AEROAPI.delete(f"{AEROAPI_BASE_URL}{api_resource}", json=data)
166+
if result.status_code != 204:
167+
# return to front end the error, decode and clean the response
168+
try:
169+
processed_json = result.json()
170+
r_description = f"Error code {result.status_code} with the following description for alert configuration {fa_alert_id}: {processed_json['detail']}"
171+
except json.decoder.JSONDecodeError:
172+
r_description = f"Error code {result.status_code} for the alert configuration {fa_alert_id} could not be parsed into JSON. The following is the HTML response given: {result.text}"
173+
else:
174+
# Check if data was inserted into database properly
175+
if delete_from_table(fa_alert_id) == -1:
176+
r_description = (
177+
"Error deleting the alert configuration from the SQL Database - since it was deleted "
178+
"on AeroAPI but not locally, this means the alert will still be shown on the table - in order to "
179+
"properly delete the alert please look in your local Sqlite database."
180+
)
181+
else:
182+
r_success = True
183+
r_description = f"Request sent successfully, alert configuration {fa_alert_id} has been deleted"
184+
185+
return jsonify({"Success": r_success, "Description": r_description})
186+
187+
127188
@app.route("/posted_alerts")
128189
def get_posted_alerts() -> Response:
129190
"""
@@ -220,7 +281,7 @@ def create_alert() -> Response:
220281
# initialize response headers
221282
r_alert_id: int = -1
222283
r_success: bool = False
223-
r_description: str = ""
284+
r_description: str
224285
# Process json
225286
content_type = request.headers.get("Content-Type")
226287
data: Dict[str, Any]

0 commit comments

Comments
 (0)