mirror of
https://github.com/skoelle/calender_sync.git
synced 2026-09-17 18:20:24 +00:00
feat: REST API + Web-Frontend für Kalenderübersicht
- FastAPI Backend mit /api/events, /api/events/{id}, /api/health Endpoints
- Optionale Suche nach Event-Titel (Query Parameter ?search=...)
- Jinja2 Web-Frontend auf / mit Suchfeld
- Shared DB Connection Module (api/database.py)
- Docker Compose: calendar-api Service hinzugefügt
- Sync.py refactored: nutzt shared database.py
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
import os
|
||||
import mysql.connector
|
||||
|
||||
DB_HOST = os.environ.get("DB_HOST", "mariadb.fritz.box")
|
||||
DB_PORT = int(os.environ.get("DB_PORT", "3306"))
|
||||
DB_NAME = os.environ.get("DB_NAME", "calendar_sync")
|
||||
DB_USER = os.environ["DB_USER"]
|
||||
DB_PASSWORD = os.environ["DB_PASSWORD"]
|
||||
|
||||
|
||||
def get_connection(autocommit: bool = True):
|
||||
return mysql.connector.connect(
|
||||
host=DB_HOST,
|
||||
port=DB_PORT,
|
||||
user=DB_USER,
|
||||
password=DB_PASSWORD,
|
||||
database=DB_NAME,
|
||||
autocommit=autocommit,
|
||||
)
|
||||
+171
@@ -0,0 +1,171 @@
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import FastAPI, Query
|
||||
from fastapi.responses import HTMLResponse
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from pydantic import BaseModel
|
||||
|
||||
from api.database import get_connection
|
||||
|
||||
app = FastAPI(title="Calendar Sync API")
|
||||
|
||||
templates = Jinja2Templates(directory=Path(__file__).parent / "templates")
|
||||
|
||||
|
||||
class EventResponse(BaseModel):
|
||||
id: int
|
||||
summary: str | None
|
||||
description: str | None
|
||||
location: str | None
|
||||
start_at: str
|
||||
end_at: str | None
|
||||
all_day: bool
|
||||
status: str
|
||||
|
||||
|
||||
class EventsListResponse(BaseModel):
|
||||
events: list[EventResponse]
|
||||
count: int
|
||||
query_time: str
|
||||
|
||||
|
||||
def row_to_event(row) -> EventResponse:
|
||||
return EventResponse(
|
||||
id=row[0],
|
||||
summary=row[1],
|
||||
description=row[2],
|
||||
location=row[3],
|
||||
start_at=row[4].isoformat() if row[4] else None,
|
||||
end_at=row[5].isoformat() if row[5] else None,
|
||||
all_day=bool(row[6]),
|
||||
status=row[7],
|
||||
)
|
||||
|
||||
|
||||
@app.get("/api/health")
|
||||
def health():
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
@app.get("/api/events", response_model=EventsListResponse)
|
||||
def get_events(
|
||||
limit: int = Query(default=10, ge=1, le=50),
|
||||
search: str | None = Query(default=None),
|
||||
):
|
||||
conn = get_connection()
|
||||
cur = conn.cursor()
|
||||
|
||||
if search:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT id, summary, description, location, start_at, end_at, all_day, status
|
||||
FROM calendar_events
|
||||
WHERE deleted = 0 AND start_at >= NOW() AND summary LIKE %s
|
||||
ORDER BY start_at ASC
|
||||
LIMIT %s
|
||||
""",
|
||||
(f"%{search}%", limit),
|
||||
)
|
||||
else:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT id, summary, description, location, start_at, end_at, all_day, status
|
||||
FROM calendar_events
|
||||
WHERE deleted = 0 AND start_at >= NOW()
|
||||
ORDER BY start_at ASC
|
||||
LIMIT %s
|
||||
""",
|
||||
(limit,),
|
||||
)
|
||||
|
||||
rows = cur.fetchall()
|
||||
cur.close()
|
||||
conn.close()
|
||||
|
||||
events = [row_to_event(row) for row in rows]
|
||||
|
||||
return EventsListResponse(
|
||||
events=events,
|
||||
count=len(events),
|
||||
query_time=datetime.utcnow().isoformat() + "Z",
|
||||
)
|
||||
|
||||
|
||||
@app.get("/api/events/{event_id}", response_model=EventResponse)
|
||||
def get_event(event_id: int):
|
||||
conn = get_connection()
|
||||
cur = conn.cursor()
|
||||
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT id, summary, description, location, start_at, end_at, all_day, status
|
||||
FROM calendar_events
|
||||
WHERE id = %s AND deleted = 0
|
||||
""",
|
||||
(event_id,),
|
||||
)
|
||||
|
||||
row = cur.fetchone()
|
||||
cur.close()
|
||||
conn.close()
|
||||
|
||||
if not row:
|
||||
from fastapi import HTTPException
|
||||
raise HTTPException(status_code=404, detail="Event not found")
|
||||
|
||||
return row_to_event(row)
|
||||
|
||||
|
||||
@app.get("/", response_class=HTMLResponse)
|
||||
def index(
|
||||
search: str | None = Query(default=None),
|
||||
limit: int = Query(default=10, ge=1, le=50),
|
||||
):
|
||||
conn = get_connection()
|
||||
cur = conn.cursor()
|
||||
|
||||
if search:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT id, summary, description, location, start_at, end_at, all_day, status
|
||||
FROM calendar_events
|
||||
WHERE deleted = 0 AND start_at >= NOW() AND summary LIKE %s
|
||||
ORDER BY start_at ASC
|
||||
LIMIT %s
|
||||
""",
|
||||
(f"%{search}%", limit),
|
||||
)
|
||||
else:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT id, summary, description, location, start_at, end_at, all_day, status
|
||||
FROM calendar_events
|
||||
WHERE deleted = 0 AND start_at >= NOW()
|
||||
ORDER BY start_at ASC
|
||||
LIMIT %s
|
||||
""",
|
||||
(limit,),
|
||||
)
|
||||
|
||||
rows = cur.fetchall()
|
||||
cur.close()
|
||||
conn.close()
|
||||
|
||||
events = []
|
||||
for row in rows:
|
||||
events.append({
|
||||
"id": row[0],
|
||||
"summary": row[1],
|
||||
"description": row[2],
|
||||
"location": row[3],
|
||||
"start_at": row[4],
|
||||
"end_at": row[5],
|
||||
"all_day": bool(row[6]),
|
||||
"status": row[7],
|
||||
})
|
||||
|
||||
return templates.TemplateResponse(
|
||||
"index.html",
|
||||
{"request": {}, "events": events, "search": search or ""},
|
||||
)
|
||||
@@ -0,0 +1,214 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Calendar Sync</title>
|
||||
<style>
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
|
||||
background: #f5f5f5;
|
||||
color: #333;
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
.container {
|
||||
max-width: 800px;
|
||||
margin: 0 auto;
|
||||
padding: 2rem 1rem;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 1.5rem;
|
||||
margin-bottom: 1.5rem;
|
||||
color: #222;
|
||||
}
|
||||
|
||||
.search-form {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
margin-bottom: 1.5rem;
|
||||
}
|
||||
|
||||
.search-form input[type="text"] {
|
||||
flex: 1;
|
||||
padding: 0.75rem 1rem;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 6px;
|
||||
font-size: 1rem;
|
||||
background: #fff;
|
||||
}
|
||||
|
||||
.search-form input[type="text"]:focus {
|
||||
outline: none;
|
||||
border-color: #0066cc;
|
||||
}
|
||||
|
||||
.search-form button {
|
||||
padding: 0.75rem 1.5rem;
|
||||
background: #0066cc;
|
||||
color: #fff;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
font-size: 1rem;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.search-form button:hover {
|
||||
background: #0055aa;
|
||||
}
|
||||
|
||||
.search-form a {
|
||||
padding: 0.75rem 1rem;
|
||||
background: #e0e0e0;
|
||||
color: #333;
|
||||
text-decoration: none;
|
||||
border-radius: 6px;
|
||||
font-size: 1rem;
|
||||
}
|
||||
|
||||
.search-form a:hover {
|
||||
background: #d0d0d0;
|
||||
}
|
||||
|
||||
.events-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.event-card {
|
||||
background: #fff;
|
||||
border-radius: 8px;
|
||||
padding: 1rem 1.25rem;
|
||||
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
|
||||
}
|
||||
|
||||
.event-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: flex-start;
|
||||
gap: 1rem;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.event-date {
|
||||
font-size: 0.875rem;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
.event-summary {
|
||||
font-size: 1.125rem;
|
||||
font-weight: 600;
|
||||
color: #222;
|
||||
}
|
||||
|
||||
.event-location {
|
||||
font-size: 0.875rem;
|
||||
color: #888;
|
||||
margin-top: 0.25rem;
|
||||
}
|
||||
|
||||
.event-location::before {
|
||||
content: "\1F4CD ";
|
||||
}
|
||||
|
||||
.badge {
|
||||
display: inline-block;
|
||||
padding: 0.2rem 0.6rem;
|
||||
border-radius: 4px;
|
||||
font-size: 0.75rem;
|
||||
font-weight: 500;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.5px;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.badge-confirmed {
|
||||
background: #d4edda;
|
||||
color: #155724;
|
||||
}
|
||||
|
||||
.badge-tentative {
|
||||
background: #fff3cd;
|
||||
color: #856404;
|
||||
}
|
||||
|
||||
.badge-cancelled {
|
||||
background: #f8d7da;
|
||||
color: #721c24;
|
||||
}
|
||||
|
||||
.no-events {
|
||||
text-align: center;
|
||||
color: #888;
|
||||
padding: 3rem 1rem;
|
||||
background: #fff;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
|
||||
}
|
||||
|
||||
.all-day {
|
||||
font-size: 0.8rem;
|
||||
color: #0066cc;
|
||||
font-weight: 500;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>Termine</h1>
|
||||
|
||||
<form class="search-form" method="get" action="/">
|
||||
<input type="text" name="search" value="{{ search }}" placeholder="Suche nach Titel...">
|
||||
<button type="submit">Suchen</button>
|
||||
{% if search %}
|
||||
<a href="/">Zurücksetzen</a>
|
||||
{% endif %}
|
||||
</form>
|
||||
|
||||
<div class="events-list">
|
||||
{% if events %}
|
||||
{% for event in events %}
|
||||
<div class="event-card">
|
||||
<div class="event-header">
|
||||
<div>
|
||||
<div class="event-date">
|
||||
{% if event.all_day %}
|
||||
{{ event.start_at.strftime('%d.%m.%Y') }} <span class="all-day">Ganztägig</span>
|
||||
{% elif event.end_at %}
|
||||
{{ event.start_at.strftime('%d.%m.%Y %H:%M') }} - {{ event.end_at.strftime('%H:%M') }}
|
||||
{% else %}
|
||||
{{ event.start_at.strftime('%d.%m.%Y %H:%M') }}
|
||||
{% endif %}
|
||||
</div>
|
||||
<div class="event-summary">{{ event.summary or '(Kein Titel)' }}</div>
|
||||
{% if event.location %}
|
||||
<div class="event-location">{{ event.location }}</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
<span class="badge badge-{{ event.status|lower }}">
|
||||
{{ event.status }}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
<div class="no-events">
|
||||
{% if search %}
|
||||
Keine Termine für "{{ search }}" gefunden.
|
||||
{% else %}
|
||||
Keine anstehenden Termine.
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user