169 lines
5.3 KiB
Python
169 lines
5.3 KiB
Python
import logging
|
|
from math import fabs
|
|
|
|
logger = logging.getLogger(__name__)
|
|
# dashboard/callbacks/appointments_callbacks.py
|
|
import requests
|
|
import json
|
|
from flask import session
|
|
from dash import Input, Output, State, callback, ctx, dash, no_update
|
|
import os
|
|
import sys
|
|
from datetime import datetime, timedelta
|
|
|
|
# This message will now appear in the terminal during startup
|
|
logger.debug("Registering appointments page...")
|
|
|
|
|
|
# --- Modalbox öffnen: jetzt auch auf Kalenderklick reagieren ---
|
|
|
|
sys.path.append('/workspace')
|
|
|
|
print("appointments_callbacks.py geladen")
|
|
|
|
API_BASE_URL = os.getenv("API_BASE_URL", "http://192.168.43.100")
|
|
ENV = os.getenv("ENV", "development")
|
|
|
|
|
|
@callback(
|
|
dash.Output('output', 'children'),
|
|
dash.Input('calendar', 'lastDateClick')
|
|
)
|
|
def display_date(date_str):
|
|
if date_str:
|
|
return f"Letzter Klick auf: {date_str}"
|
|
return "Klicke auf ein Datum im Kalender!"
|
|
|
|
|
|
@callback(
|
|
dash.Output('event-output', 'children'),
|
|
dash.Input('calendar', 'lastEventClick')
|
|
)
|
|
def display_event(event_id):
|
|
if event_id:
|
|
return f"Letztes Event geklickt: {event_id}"
|
|
return "Klicke auf ein Event im Kalender!"
|
|
|
|
|
|
@callback(
|
|
dash.Output('select-output', 'children'),
|
|
dash.Input('calendar', 'lastSelect')
|
|
)
|
|
def display_select(select_info):
|
|
if select_info:
|
|
return f"Markiert: {select_info['start']} bis {select_info['end']} (ganztägig: {select_info['allDay']})"
|
|
return "Markiere einen Bereich im Kalender!"
|
|
|
|
|
|
@callback(
|
|
dash.Output('calendar', 'events'),
|
|
dash.Input('calendar', 'lastNavClick'),
|
|
)
|
|
def load_events(view_dates):
|
|
logger.info(f"Lade Events für Zeitraum: {view_dates}")
|
|
if not view_dates or "start" not in view_dates or "end" not in view_dates:
|
|
return []
|
|
start = view_dates["start"]
|
|
end = view_dates["end"]
|
|
try:
|
|
verify_ssl = True if ENV == "production" else False
|
|
resp = requests.get(
|
|
f"{API_BASE_URL}/api/events",
|
|
params={"start": start, "end": end},
|
|
verify=verify_ssl
|
|
)
|
|
resp.raise_for_status()
|
|
events = resp.json()
|
|
return events
|
|
except Exception as e:
|
|
logger.info(f"Fehler beim Laden der Events: {e}")
|
|
return []
|
|
|
|
# --- Modalbox öffnen ---
|
|
|
|
|
|
@callback(
|
|
[
|
|
Output("appointment-modal", "opened"),
|
|
Output("start-date-input", "value", allow_duplicate=True),
|
|
Output("time-start", "value", allow_duplicate=True),
|
|
Output("time-end", "value", allow_duplicate=True),
|
|
],
|
|
[
|
|
Input("calendar", "lastDateClick"),
|
|
Input("calendar", "lastSelect"),
|
|
Input("open-appointment-modal-btn", "n_clicks"),
|
|
Input("close-appointment-modal-btn", "n_clicks"),
|
|
],
|
|
State("appointment-modal", "opened"),
|
|
prevent_initial_call=True
|
|
)
|
|
def open_modal(date_click, select, open_click, close_click, is_open):
|
|
trigger = ctx.triggered_id
|
|
|
|
# Bereichsauswahl (lastSelect)
|
|
if trigger == "calendar" and select:
|
|
try:
|
|
start_dt = datetime.fromisoformat(select["start"])
|
|
end_dt = datetime.fromisoformat(select["end"])
|
|
|
|
return (
|
|
True,
|
|
start_dt.date().isoformat(),
|
|
start_dt.strftime("%H:%M"),
|
|
end_dt.strftime("%H:%M"),
|
|
)
|
|
except Exception as e:
|
|
print("Fehler beim Parsen von select:", e)
|
|
return no_update, no_update, no_update, no_update
|
|
|
|
# Einzelklick (lastDateClick)
|
|
if trigger == "calendar" and date_click:
|
|
try:
|
|
dt = datetime.fromisoformat(date_click)
|
|
# Versuche, die Slotlänge aus dem Kalender zu übernehmen (optional)
|
|
# Hier als Beispiel 30 Minuten aufaddieren, falls keine Endzeit vorhanden
|
|
end_dt = dt + timedelta(minutes=30)
|
|
return (
|
|
True,
|
|
dt.date().isoformat(),
|
|
dt.strftime("%H:%M"),
|
|
end_dt.strftime("%H:%M"),
|
|
)
|
|
except Exception as e:
|
|
print("Fehler beim Parsen von date_click:", e)
|
|
return no_update, no_update, no_update, no_update
|
|
|
|
# Modal öffnen per Button
|
|
if trigger == "open-appointment-modal-btn" and open_click:
|
|
now = datetime.now()
|
|
end_dt = now + timedelta(minutes=30)
|
|
return True, now.date().isoformat(), now.strftime("%H:%M"), end_dt.strftime("%H:%M")
|
|
|
|
# Modal schließen
|
|
if trigger == "close-appointment-modal-btn" and close_click:
|
|
return False, no_update, no_update, no_update
|
|
|
|
return is_open, no_update, no_update, no_update
|
|
|
|
|
|
# @callback(
|
|
# Output("time-end", "value", allow_duplicate=True),
|
|
# Input("time-start", "value"),
|
|
# prevent_initial_call=True
|
|
# )
|
|
# def handle_end_time(start_time, duration="00:30"):
|
|
# trigger = ctx.triggered_id
|
|
# if trigger == "time-start" and start_time and duration:
|
|
# try:
|
|
# # Beispiel für start_time: "09:00"
|
|
# start_dt = datetime.strptime(start_time, "%H:%M")
|
|
# # Dauer in Stunden und Minuten, z.B. "01:30"
|
|
# hours, minutes = map(int, duration.split(":"))
|
|
# # Endzeit berechnen: Dauer addieren!
|
|
# end_dt = start_dt + timedelta(hours=hours, minutes=minutes)
|
|
# return end_dt.strftime("%H:%M")
|
|
# except Exception as e:
|
|
# print("Fehler bei der Berechnung der Endzeit:", e)
|
|
# return no_update
|