|
23 | 23 | Date, |
24 | 24 | select, |
25 | 25 | DateTime, |
| 26 | + delete |
26 | 27 | ) |
27 | 28 |
|
28 | 29 | AEROAPI_BASE_URL = "https://aeroapi.flightaware.com/aeroapi" |
|
70 | 71 | "aeroapi_alerts", |
71 | 72 | metadata_obj, |
72 | 73 | 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()), |
76 | 76 | Column("long_description", Text), |
77 | 77 | Column("short_description", Text), |
78 | 78 | Column("summary", Text), |
@@ -124,6 +124,67 @@ def insert_into_table(data_to_insert: Dict[str, Any], table: Table) -> int: |
124 | 124 | return 0 |
125 | 125 |
|
126 | 126 |
|
| 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 | + |
127 | 188 | @app.route("/posted_alerts") |
128 | 189 | def get_posted_alerts() -> Response: |
129 | 190 | """ |
@@ -220,7 +281,7 @@ def create_alert() -> Response: |
220 | 281 | # initialize response headers |
221 | 282 | r_alert_id: int = -1 |
222 | 283 | r_success: bool = False |
223 | | - r_description: str = "" |
| 284 | + r_description: str |
224 | 285 | # Process json |
225 | 286 | content_type = request.headers.get("Content-Type") |
226 | 287 | data: Dict[str, Any] |
|
0 commit comments