|
1 | | -import json |
2 | 1 | import os |
| 2 | +import re |
3 | 3 |
|
4 | 4 | import pandas as pd |
| 5 | +from datetime import datetime |
5 | 6 | from flask import Flask, render_template, request, send_file, session |
6 | 7 |
|
7 | | -from core import get_flight, make_ics_from_selected_df_index |
| 8 | +from core import get_flight, make_ics_from_selected_df_index, make_ics_from_manual_data |
8 | 9 |
|
9 | 10 | app = Flask(__name__) |
10 | 11 |
|
@@ -42,11 +43,83 @@ def create_ical_from_selected(index): |
42 | 43 | return "No flight data found", 400 |
43 | 44 |
|
44 | 45 | df = pd.read_json(df_json, orient="split") |
| 46 | + |
| 47 | + # Check if custom times were provided |
| 48 | + custom_departure = request.form.get("custom_departure") |
| 49 | + custom_arrival = request.form.get("custom_arrival") |
| 50 | + |
| 51 | + if custom_departure and custom_arrival: |
| 52 | + # Update the DataFrame with custom times |
| 53 | + df.at[index, "scheduled_departure"] = custom_departure |
| 54 | + df.at[index, "scheduled_arrival"] = custom_arrival |
| 55 | + |
45 | 56 | ics_data = make_ics_from_selected_df_index(df, index) |
46 | 57 | flight = df.iloc[index]["flight_number"] |
47 | 58 |
|
48 | 59 | return send_file(ics_data, as_attachment=True, download_name=f"{flight}.ics") |
49 | 60 |
|
50 | 61 |
|
| 62 | +@app.route("/manual_entry") |
| 63 | +def manual_entry(): |
| 64 | + return render_template("manual_entry.html") |
| 65 | + |
| 66 | + |
| 67 | +@app.route("/create_manual_event", methods=["POST"]) |
| 68 | +def create_manual_event(): |
| 69 | + try: |
| 70 | + # Get all form data |
| 71 | + flight_data = { |
| 72 | + "flight_number": request.form.get("flight_number"), |
| 73 | + "airline_name": request.form.get("airline_name"), |
| 74 | + "origin_airport": request.form.get("origin_airport"), |
| 75 | + "origin_airport_code": request.form.get("origin_airport_code"), |
| 76 | + "destination_airport": request.form.get("destination_airport"), |
| 77 | + "destination_airport_code": request.form.get("destination_airport_code"), |
| 78 | + "scheduled_departure": request.form.get("scheduled_departure"), |
| 79 | + "scheduled_arrival": request.form.get("scheduled_arrival"), |
| 80 | + "origin_timezone": request.form.get("origin_timezone"), |
| 81 | + "destination_timezone": request.form.get("destination_timezone"), |
| 82 | + } |
| 83 | + |
| 84 | + # Validate required fields |
| 85 | + required_fields = [ |
| 86 | + "flight_number", |
| 87 | + "airline_name", |
| 88 | + "origin_airport", |
| 89 | + "origin_airport_code", |
| 90 | + "destination_airport", |
| 91 | + "destination_airport_code", |
| 92 | + "scheduled_departure", |
| 93 | + "scheduled_arrival", |
| 94 | + "origin_timezone", |
| 95 | + "destination_timezone", |
| 96 | + ] |
| 97 | + for field in required_fields: |
| 98 | + if not flight_data.get(field): |
| 99 | + raise ValueError(f"Missing required field: {field}") |
| 100 | + |
| 101 | + # Validate airport codes (should be 3 uppercase letters) |
| 102 | + if not re.match(r"^[A-Z]{3}$", flight_data["origin_airport_code"]): |
| 103 | + raise ValueError("Origin airport code must be 3 uppercase letters") |
| 104 | + if not re.match(r"^[A-Z]{3}$", flight_data["destination_airport_code"]): |
| 105 | + raise ValueError("Destination airport code must be 3 uppercase letters") |
| 106 | + |
| 107 | + # Validate datetime format (YYYY-MM-DD HH:MM) |
| 108 | + try: |
| 109 | + datetime.strptime(flight_data["scheduled_departure"], "%Y-%m-%d %H:%M") |
| 110 | + datetime.strptime(flight_data["scheduled_arrival"], "%Y-%m-%d %H:%M") |
| 111 | + except ValueError: |
| 112 | + raise ValueError("Invalid datetime format. Use: yyyy-mm-dd hh:mm") |
| 113 | + |
| 114 | + # Create iCal file from manual data |
| 115 | + ics_data = make_ics_from_manual_data(flight_data) |
| 116 | + flight = flight_data["flight_number"] |
| 117 | + |
| 118 | + return send_file(ics_data, as_attachment=True, download_name=f"{flight}.ics") |
| 119 | + except Exception as e: |
| 120 | + error_message = str(e) |
| 121 | + return render_template("manual_entry.html", error=error_message) |
| 122 | + |
| 123 | + |
51 | 124 | if __name__ == "__main__": |
52 | 125 | app.run() |
0 commit comments