Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -59,7 +59,7 @@ def generate_google_calendar_link(name, email, date_str, time_str, timezone):
|
|
| 59 |
return f"https://www.google.com/calendar/render?{urlencode(event)}"
|
| 60 |
except Exception as e:
|
| 61 |
logging.error(f"Google Calendar link error: {e}")
|
| 62 |
-
|
| 63 |
|
| 64 |
# === Email Utility ===
|
| 65 |
def send_email(to_email, subject, body):
|
|
@@ -77,7 +77,7 @@ def send_email(to_email, subject, body):
|
|
| 77 |
return True
|
| 78 |
except Exception as e:
|
| 79 |
logging.error(f"Email error: {e}")
|
| 80 |
-
|
| 81 |
|
| 82 |
# === Save to Excel ===
|
| 83 |
def save_to_excel(name, email, date_str, time_str, timezone, message):
|
|
@@ -100,27 +100,27 @@ def save_to_excel(name, email, date_str, time_str, timezone, message):
|
|
| 100 |
return True
|
| 101 |
except Exception as e:
|
| 102 |
logging.error(f"Excel save error: {e}")
|
| 103 |
-
|
| 104 |
|
| 105 |
# === Appointment Handler ===
|
| 106 |
def book_appointment(name, email, date, time, timezone, message):
|
| 107 |
-
if not all([name, email, date, time, timezone]):
|
| 108 |
-
return "Please fill out all required fields.", []
|
| 109 |
-
|
| 110 |
try:
|
|
|
|
|
|
|
|
|
|
| 111 |
date_clean = date.split(" ")[0] # Extract YYYY-MM-DD
|
| 112 |
datetime.strptime(date_clean, "%Y-%m-%d")
|
| 113 |
tz = pytz.timezone(timezone)
|
| 114 |
appointment_time = datetime.strptime(f"{date_clean} {time}", "%Y-%m-%d %H:%M").replace(tzinfo=tz)
|
| 115 |
if appointment_time < datetime.now(tz):
|
| 116 |
-
|
| 117 |
|
| 118 |
if not save_to_excel(name, email, date_clean, time, timezone, message):
|
| 119 |
-
|
| 120 |
|
| 121 |
calendar_link = generate_google_calendar_link(name, email, date_clean, time, timezone)
|
| 122 |
if not calendar_link:
|
| 123 |
-
|
| 124 |
|
| 125 |
admin_msg = f"New Appointment:\nName: {name}\nEmail: {email}\nDate: {date_clean}\nTime: {time}\nTimezone: {timezone}\nMessage: {message or 'No message'}"
|
| 126 |
send_email("admin@mubashirdev.com", "📅 New Appointment", admin_msg)
|
|
@@ -157,10 +157,9 @@ def book_appointment(name, email, date, time, timezone, message):
|
|
| 157 |
[chart_data]
|
| 158 |
)
|
| 159 |
|
| 160 |
-
except
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
return "Invalid date format.", []
|
| 164 |
|
| 165 |
# === JavaScript for Date Dropdown Enhancement ===
|
| 166 |
JS_ENHANCEMENT = """
|
|
@@ -303,4 +302,4 @@ with gr.Blocks(theme=gr.themes.Soft(), css="""
|
|
| 303 |
)
|
| 304 |
|
| 305 |
# Launch the app (no port specification for Hugging Face Spaces)
|
| 306 |
-
demo.launch()
|
|
|
|
| 59 |
return f"https://www.google.com/calendar/render?{urlencode(event)}"
|
| 60 |
except Exception as e:
|
| 61 |
logging.error(f"Google Calendar link error: {e}")
|
| 62 |
+
raise Exception("Failed to generate calendar link.")
|
| 63 |
|
| 64 |
# === Email Utility ===
|
| 65 |
def send_email(to_email, subject, body):
|
|
|
|
| 77 |
return True
|
| 78 |
except Exception as e:
|
| 79 |
logging.error(f"Email error: {e}")
|
| 80 |
+
raise Exception("Failed to send email.")
|
| 81 |
|
| 82 |
# === Save to Excel ===
|
| 83 |
def save_to_excel(name, email, date_str, time_str, timezone, message):
|
|
|
|
| 100 |
return True
|
| 101 |
except Exception as e:
|
| 102 |
logging.error(f"Excel save error: {e}")
|
| 103 |
+
raise Exception("Failed to save appointment.")
|
| 104 |
|
| 105 |
# === Appointment Handler ===
|
| 106 |
def book_appointment(name, email, date, time, timezone, message):
|
|
|
|
|
|
|
|
|
|
| 107 |
try:
|
| 108 |
+
if not all([name, email, date, time, timezone]):
|
| 109 |
+
raise Exception("Please fill out all required fields.")
|
| 110 |
+
|
| 111 |
date_clean = date.split(" ")[0] # Extract YYYY-MM-DD
|
| 112 |
datetime.strptime(date_clean, "%Y-%m-%d")
|
| 113 |
tz = pytz.timezone(timezone)
|
| 114 |
appointment_time = datetime.strptime(f"{date_clean} {time}", "%Y-%m-%d %H:%M").replace(tzinfo=tz)
|
| 115 |
if appointment_time < datetime.now(tz):
|
| 116 |
+
raise Exception("Appointments must be in the future.")
|
| 117 |
|
| 118 |
if not save_to_excel(name, email, date_clean, time, timezone, message):
|
| 119 |
+
raise Exception("Failed to save appointment.")
|
| 120 |
|
| 121 |
calendar_link = generate_google_calendar_link(name, email, date_clean, time, timezone)
|
| 122 |
if not calendar_link:
|
| 123 |
+
raise Exception("Failed to generate calendar link.")
|
| 124 |
|
| 125 |
admin_msg = f"New Appointment:\nName: {name}\nEmail: {email}\nDate: {date_clean}\nTime: {time}\nTimezone: {timezone}\nMessage: {message or 'No message'}"
|
| 126 |
send_email("admin@mubashirdev.com", "📅 New Appointment", admin_msg)
|
|
|
|
| 157 |
[chart_data]
|
| 158 |
)
|
| 159 |
|
| 160 |
+
except Exception as e:
|
| 161 |
+
logging.error(f"Booking error: {str(e)}")
|
| 162 |
+
return str(e), []
|
|
|
|
| 163 |
|
| 164 |
# === JavaScript for Date Dropdown Enhancement ===
|
| 165 |
JS_ENHANCEMENT = """
|
|
|
|
| 302 |
)
|
| 303 |
|
| 304 |
# Launch the app (no port specification for Hugging Face Spaces)
|
| 305 |
+
demo.launch(share=False)
|