first kanban-view integration for client groups
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
"""Add client_groups table and group_id to clients
|
||||
|
||||
Revision ID: 8a45ec34f84d
|
||||
Revises: c1178d5fa549
|
||||
Create Date: 2025-06-26 18:40:10.988281
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '8a45ec34f84d'
|
||||
down_revision: Union[str, None] = 'c1178d5fa549'
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema."""
|
||||
# 1. Neue Tabelle anlegen
|
||||
op.create_table(
|
||||
'client_groups',
|
||||
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
|
||||
sa.Column('name', sa.String(length=100), nullable=False),
|
||||
sa.Column('created_at', sa.TIMESTAMP(timezone=True),
|
||||
server_default=sa.text('CURRENT_TIMESTAMP'), nullable=True),
|
||||
sa.Column('is_active', sa.Boolean(), nullable=False),
|
||||
sa.PrimaryKeyConstraint('id'),
|
||||
sa.UniqueConstraint('name')
|
||||
)
|
||||
# 2. Gruppe "Nicht zugeordnet" mit id=0 anlegen
|
||||
op.execute(
|
||||
"INSERT INTO client_groups (id, name, is_active) VALUES (0, 'Nicht zugeordnet', true)")
|
||||
|
||||
# 3. Spalte group_id mit Default 0 hinzufügen
|
||||
op.add_column('clients', sa.Column('group_id', sa.Integer(),
|
||||
nullable=False, server_default='0'))
|
||||
|
||||
# 4. Für alle bestehenden Clients group_id auf 0 setzen (optional, falls Daten vorhanden)
|
||||
op.execute("UPDATE clients SET group_id = 0 WHERE group_id IS NULL")
|
||||
|
||||
# 5. Foreign Key Constraint setzen
|
||||
op.create_foreign_key(None, 'clients', 'client_groups', [
|
||||
'group_id'], ['id'])
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_constraint(None, 'clients', type_='foreignkey')
|
||||
op.drop_column('clients', 'group_id')
|
||||
op.drop_table('client_groups')
|
||||
# ### end Alembic commands ###
|
||||
45
server/dummy_clients.py
Normal file
45
server/dummy_clients.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from models import Client
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
from datetime import datetime, timedelta
|
||||
import random
|
||||
import uuid
|
||||
|
||||
# .env laden
|
||||
load_dotenv()
|
||||
|
||||
DB_USER = os.getenv("DB_USER")
|
||||
DB_PASSWORD = os.getenv("DB_PASSWORD")
|
||||
DB_HOST = os.getenv("DB_HOST")
|
||||
DB_NAME = os.getenv("DB_NAME")
|
||||
|
||||
db_conn_str = f"mysql+pymysql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}/{DB_NAME}"
|
||||
engine = create_engine(db_conn_str)
|
||||
Session = sessionmaker(bind=engine)
|
||||
session = Session()
|
||||
|
||||
# Dummy-Clients erzeugen
|
||||
locations = [
|
||||
"Raum 101",
|
||||
"Raum 102",
|
||||
"Lehrerzimmer",
|
||||
"Aula",
|
||||
"Bibliothek"
|
||||
]
|
||||
|
||||
for i in range(5):
|
||||
client = Client(
|
||||
uuid=str(uuid.uuid4()),
|
||||
hardware_hash=f"dummyhash{i:02d}",
|
||||
location=locations[i],
|
||||
ip_address=f"192.168.0.{100+i}",
|
||||
registration_time=datetime.now() - timedelta(days=random.randint(1, 30)),
|
||||
last_alive=datetime.now(),
|
||||
is_active=True
|
||||
)
|
||||
session.add(client)
|
||||
|
||||
session.commit()
|
||||
print("5 Dummy-Clients wurden angelegt.")
|
||||
@@ -1,6 +1,6 @@
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from models import Event, EventMedia, EventType
|
||||
from models import Event, EventMedia, EventType, Client
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
from datetime import datetime, timedelta
|
||||
@@ -19,22 +19,6 @@ engine = create_engine(db_conn_str)
|
||||
Session = sessionmaker(bind=engine)
|
||||
session = Session()
|
||||
|
||||
# Beispiel-Events (client_uuid und created_by müssen existieren!)
|
||||
client_uuid = "36671351-7a90-44af-bfef-a192c541373b"
|
||||
created_by = 1 # ID eines existierenden Users
|
||||
|
||||
# --- Alte Testdaten löschen ---
|
||||
# Zuerst alle EventMedia zu den Events dieses Clients löschen
|
||||
event_ids = [e.id for e in session.query(
|
||||
Event.id).filter_by(client_uuid=client_uuid).all()]
|
||||
if event_ids:
|
||||
session.query(EventMedia).filter(EventMedia.event_id.in_(
|
||||
event_ids)).delete(synchronize_session=False)
|
||||
# Dann alle Events dieses Clients löschen
|
||||
session.query(Event).filter_by(client_uuid=client_uuid).delete()
|
||||
session.commit()
|
||||
# --- Ende Löschungen ---
|
||||
|
||||
now = datetime.now()
|
||||
|
||||
|
||||
@@ -47,189 +31,33 @@ def random_time_on_day(day_offset: int, duration_hours: int = 1):
|
||||
return start, end
|
||||
|
||||
|
||||
events = [
|
||||
Event(
|
||||
client_uuid=client_uuid,
|
||||
title="Mathe Präsentation",
|
||||
description="Präsentation zum Thema Algebra.",
|
||||
start=random_time_on_day(1)[0],
|
||||
end=random_time_on_day(1)[1],
|
||||
event_type=EventType.presentation,
|
||||
created_by=created_by,
|
||||
updated_by=None,
|
||||
is_active=True
|
||||
),
|
||||
Event(
|
||||
client_uuid=client_uuid,
|
||||
title="Schulwebsite Update",
|
||||
description="Neue Inhalte auf der Schulwebsite.",
|
||||
start=random_time_on_day(2)[0],
|
||||
end=random_time_on_day(2)[1],
|
||||
event_type=EventType.website,
|
||||
created_by=created_by,
|
||||
updated_by=None,
|
||||
is_active=True
|
||||
),
|
||||
Event(
|
||||
client_uuid=client_uuid,
|
||||
title="Lehrvideo ansehen",
|
||||
description="Video zum Thema Photosynthese.",
|
||||
start=random_time_on_day(3)[0],
|
||||
end=random_time_on_day(3)[1],
|
||||
event_type=EventType.video,
|
||||
created_by=created_by,
|
||||
updated_by=None,
|
||||
is_active=True
|
||||
),
|
||||
Event(
|
||||
client_uuid=client_uuid,
|
||||
title="Nachricht vom Lehrer",
|
||||
description="Wichtige Mitteilung zum Unterricht.",
|
||||
start=random_time_on_day(4)[0],
|
||||
end=random_time_on_day(4)[1],
|
||||
event_type=EventType.message,
|
||||
created_by=created_by,
|
||||
updated_by=None,
|
||||
is_active=True
|
||||
),
|
||||
Event(
|
||||
client_uuid=client_uuid,
|
||||
title="Sonstiges Event",
|
||||
description="Allgemeines Event ohne Kategorie.",
|
||||
start=random_time_on_day(5)[0],
|
||||
end=random_time_on_day(5)[1],
|
||||
event_type=EventType.other,
|
||||
created_by=created_by,
|
||||
updated_by=None,
|
||||
is_active=True
|
||||
),
|
||||
Event(
|
||||
client_uuid=client_uuid,
|
||||
title="WebUntis Termin",
|
||||
description="Termin aus WebUntis importiert.",
|
||||
start=random_time_on_day(6)[0],
|
||||
end=random_time_on_day(6)[1],
|
||||
event_type=EventType.webuntis,
|
||||
created_by=created_by,
|
||||
updated_by=None,
|
||||
is_active=True
|
||||
),
|
||||
Event(
|
||||
client_uuid=client_uuid,
|
||||
title="Englisch Präsentation",
|
||||
description="Präsentation zu Shakespeare.",
|
||||
start=random_time_on_day(8)[0],
|
||||
end=random_time_on_day(8)[1],
|
||||
event_type=EventType.presentation,
|
||||
created_by=created_by,
|
||||
updated_by=None,
|
||||
is_active=True
|
||||
),
|
||||
Event(
|
||||
client_uuid=client_uuid,
|
||||
title="Website Relaunch",
|
||||
description="Vorstellung der neuen Schulwebsite.",
|
||||
start=random_time_on_day(10)[0],
|
||||
end=random_time_on_day(10)[1],
|
||||
event_type=EventType.website,
|
||||
created_by=created_by,
|
||||
updated_by=None,
|
||||
is_active=True
|
||||
),
|
||||
Event(
|
||||
client_uuid=client_uuid,
|
||||
title="Videoanalyse",
|
||||
description="Analyse eines Lehrvideos.",
|
||||
start=random_time_on_day(12)[0],
|
||||
end=random_time_on_day(12)[1],
|
||||
event_type=EventType.video,
|
||||
created_by=created_by,
|
||||
updated_by=None,
|
||||
is_active=True
|
||||
),
|
||||
Event(
|
||||
client_uuid=client_uuid,
|
||||
title="WebUntis Info",
|
||||
description="Weitere Termine aus WebUntis.",
|
||||
start=random_time_on_day(14)[0],
|
||||
end=random_time_on_day(14)[1],
|
||||
event_type=EventType.webuntis,
|
||||
created_by=created_by,
|
||||
updated_by=None,
|
||||
is_active=True
|
||||
),
|
||||
]
|
||||
# Hole alle Clients aus der Datenbank
|
||||
clients = session.query(Client).all()
|
||||
created_by = 1 # Passe ggf. an
|
||||
|
||||
# Events anlegen und speichern
|
||||
for event in events:
|
||||
all_events = []
|
||||
|
||||
for client in clients:
|
||||
for i in range(10):
|
||||
day_offset = random.randint(0, 13) # Termine in den nächsten 14 Tagen
|
||||
duration = random.choice([1, 2]) # 1 oder 2 Stunden
|
||||
start, end = random_time_on_day(day_offset, duration)
|
||||
event = Event(
|
||||
client_uuid=client.uuid,
|
||||
title=f"Termin {i+1} für {client.location or client.uuid[:8]}",
|
||||
description=f"Automatisch generierter Termin {i+1} für Client {client.uuid}",
|
||||
start=start,
|
||||
end=end,
|
||||
event_type=random.choice(list(EventType)),
|
||||
created_by=created_by,
|
||||
updated_by=None,
|
||||
is_active=True
|
||||
)
|
||||
all_events.append(event)
|
||||
|
||||
# Events speichern
|
||||
for event in all_events:
|
||||
session.add(event)
|
||||
session.commit() # Jetzt haben alle Events eine ID
|
||||
|
||||
# EventMedia mit den richtigen event_id-Werten anlegen
|
||||
event_media = [
|
||||
EventMedia(
|
||||
event_id=events[0].id,
|
||||
media_type=EventType.presentation,
|
||||
url="https://example.com/praesentation-mathe.pdf",
|
||||
order=1
|
||||
),
|
||||
EventMedia(
|
||||
event_id=events[1].id,
|
||||
media_type=EventType.website,
|
||||
url="https://schule.example.com/update",
|
||||
order=1
|
||||
),
|
||||
EventMedia(
|
||||
event_id=events[2].id,
|
||||
media_type=EventType.video,
|
||||
url="https://example.com/photosynthese-video.mp4",
|
||||
order=1
|
||||
),
|
||||
EventMedia(
|
||||
event_id=events[3].id,
|
||||
media_type=EventType.message,
|
||||
url="https://example.com/lehrer-nachricht",
|
||||
order=1
|
||||
),
|
||||
EventMedia(
|
||||
event_id=events[4].id,
|
||||
media_type=EventType.other,
|
||||
url="https://example.com/sonstiges-event.pdf",
|
||||
order=1
|
||||
),
|
||||
EventMedia(
|
||||
event_id=events[5].id,
|
||||
media_type=EventType.webuntis,
|
||||
url="https://webuntis.example.com/termin",
|
||||
order=1
|
||||
),
|
||||
EventMedia(
|
||||
event_id=events[6].id,
|
||||
media_type=EventType.presentation,
|
||||
url="https://example.com/praesentation-englisch.pdf",
|
||||
order=1
|
||||
),
|
||||
EventMedia(
|
||||
event_id=events[7].id,
|
||||
media_type=EventType.website,
|
||||
url="https://schule.example.com/relaunch",
|
||||
order=1
|
||||
),
|
||||
EventMedia(
|
||||
event_id=events[8].id,
|
||||
media_type=EventType.video,
|
||||
url="https://example.com/videoanalyse.mp4",
|
||||
order=1
|
||||
),
|
||||
EventMedia(
|
||||
event_id=events[9].id,
|
||||
media_type=EventType.webuntis,
|
||||
url="https://webuntis.example.com/info",
|
||||
order=1
|
||||
),
|
||||
]
|
||||
|
||||
for media in event_media:
|
||||
session.add(media)
|
||||
session.commit()
|
||||
print("Test-Events und EventMedia wurden angelegt.")
|
||||
|
||||
print(f"{len(all_events)} Termine für {len(clients)} Clients wurden angelegt.")
|
||||
|
||||
@@ -6,11 +6,13 @@ import enum
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
|
||||
class UserRole(enum.Enum):
|
||||
user = "user"
|
||||
admin = "admin"
|
||||
superadmin = "superadmin"
|
||||
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = 'users'
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
@@ -18,8 +20,20 @@ class User(Base):
|
||||
password_hash = Column(String(128), nullable=False)
|
||||
role = Column(Enum(UserRole), nullable=False, default=UserRole.user)
|
||||
is_active = Column(Boolean, default=True, nullable=False)
|
||||
created_at = Column(TIMESTAMP(timezone=True), server_default=func.current_timestamp())
|
||||
updated_at = Column(TIMESTAMP(timezone=True), server_default=func.current_timestamp(), onupdate=func.current_timestamp())
|
||||
created_at = Column(TIMESTAMP(timezone=True),
|
||||
server_default=func.current_timestamp())
|
||||
updated_at = Column(TIMESTAMP(timezone=True), server_default=func.current_timestamp(
|
||||
), onupdate=func.current_timestamp())
|
||||
|
||||
|
||||
class ClientGroup(Base):
|
||||
__tablename__ = 'client_groups'
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
name = Column(String(100), unique=True, nullable=False)
|
||||
created_at = Column(TIMESTAMP(timezone=True),
|
||||
server_default=func.current_timestamp())
|
||||
is_active = Column(Boolean, default=True, nullable=False)
|
||||
|
||||
|
||||
class Client(Base):
|
||||
__tablename__ = 'clients'
|
||||
@@ -27,9 +41,14 @@ class Client(Base):
|
||||
hardware_hash = Column(String(64), nullable=False, index=True)
|
||||
location = Column(String(100), nullable=True)
|
||||
ip_address = Column(String(45), nullable=True, index=True)
|
||||
registration_time = Column(TIMESTAMP(timezone=True), server_default=func.current_timestamp(), nullable=False)
|
||||
last_alive = Column(TIMESTAMP(timezone=True), server_default=func.current_timestamp(), onupdate=func.current_timestamp(), nullable=False)
|
||||
registration_time = Column(TIMESTAMP(
|
||||
timezone=True), server_default=func.current_timestamp(), nullable=False)
|
||||
last_alive = Column(TIMESTAMP(timezone=True), server_default=func.current_timestamp(
|
||||
), onupdate=func.current_timestamp(), nullable=False)
|
||||
is_active = Column(Boolean, default=True, nullable=False)
|
||||
group_id = Column(Integer, ForeignKey(
|
||||
'client_groups.id'), nullable=False, default=1)
|
||||
|
||||
|
||||
class EventType(enum.Enum):
|
||||
presentation = "presentation"
|
||||
@@ -39,21 +58,26 @@ class EventType(enum.Enum):
|
||||
other = "other"
|
||||
webuntis = "webuntis"
|
||||
|
||||
|
||||
class Event(Base):
|
||||
__tablename__ = 'events'
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
client_uuid = Column(String(36), ForeignKey('clients.uuid'), nullable=False, index=True)
|
||||
client_uuid = Column(String(36), ForeignKey(
|
||||
'clients.uuid'), nullable=False, index=True)
|
||||
title = Column(String(100), nullable=False)
|
||||
description = Column(Text, nullable=True)
|
||||
start = Column(TIMESTAMP(timezone=True), nullable=False, index=True)
|
||||
end = Column(TIMESTAMP(timezone=True), nullable=False, index=True)
|
||||
event_type = Column(Enum(EventType), nullable=False)
|
||||
created_at = Column(TIMESTAMP(timezone=True), server_default=func.current_timestamp())
|
||||
updated_at = Column(TIMESTAMP(timezone=True), server_default=func.current_timestamp(), onupdate=func.current_timestamp())
|
||||
created_at = Column(TIMESTAMP(timezone=True),
|
||||
server_default=func.current_timestamp())
|
||||
updated_at = Column(TIMESTAMP(timezone=True), server_default=func.current_timestamp(
|
||||
), onupdate=func.current_timestamp())
|
||||
created_by = Column(Integer, ForeignKey('users.id'), nullable=False)
|
||||
updated_by = Column(Integer, ForeignKey('users.id'), nullable=True)
|
||||
is_active = Column(Boolean, default=True, nullable=False)
|
||||
|
||||
|
||||
class EventMedia(Base):
|
||||
__tablename__ = 'event_media'
|
||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||
|
||||
108
server/wsgi.py
108
server/wsgi.py
@@ -1,10 +1,12 @@
|
||||
# server/wsgi.py
|
||||
from models import Client, ClientGroup
|
||||
from flask import request, jsonify
|
||||
import glob
|
||||
import os
|
||||
from flask import Flask, jsonify, send_from_directory, request
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy import create_engine, and_
|
||||
from models import Client, Base, Event
|
||||
from models import Client, Base, Event, ClientGroup
|
||||
|
||||
DB_USER = os.getenv("DB_USER")
|
||||
DB_PASSWORD = os.getenv("DB_PASSWORD")
|
||||
@@ -62,7 +64,8 @@ def get_clients():
|
||||
"location": c.location,
|
||||
"hardware_hash": c.hardware_hash,
|
||||
"ip_address": c.ip_address,
|
||||
"last_alive": c.last_alive.isoformat() if c.last_alive else None
|
||||
"last_alive": c.last_alive.isoformat() if c.last_alive else None,
|
||||
"group_id": c.group_id,
|
||||
}
|
||||
for c in clients
|
||||
]
|
||||
@@ -96,5 +99,106 @@ def get_events():
|
||||
return jsonify(result)
|
||||
|
||||
|
||||
@app.route("/api/groups", methods=["POST"])
|
||||
def create_group():
|
||||
data = request.get_json()
|
||||
name = data.get("name")
|
||||
if not name or not name.strip():
|
||||
return jsonify({"error": "Gruppenname erforderlich"}), 400
|
||||
|
||||
session = Session()
|
||||
# Prüfen, ob der Name schon existiert
|
||||
if session.query(ClientGroup).filter_by(name=name).first():
|
||||
session.close()
|
||||
return jsonify({"error": "Gruppe existiert bereits"}), 409
|
||||
|
||||
group = ClientGroup(name=name, is_active=True)
|
||||
session.add(group)
|
||||
session.commit()
|
||||
result = {
|
||||
"id": group.id,
|
||||
"name": group.name,
|
||||
"created_at": group.created_at.isoformat() if group.created_at else None,
|
||||
"is_active": group.is_active,
|
||||
}
|
||||
session.close()
|
||||
return jsonify(result), 201
|
||||
|
||||
|
||||
@app.route("/api/groups", methods=["GET"])
|
||||
def get_groups():
|
||||
"""Liefert alle Raumgruppen als Liste zurück."""
|
||||
session = Session()
|
||||
groups = session.query(ClientGroup).all()
|
||||
result = [
|
||||
{
|
||||
"id": g.id,
|
||||
"name": g.name,
|
||||
"created_at": g.created_at.isoformat() if g.created_at else None,
|
||||
"is_active": g.is_active,
|
||||
}
|
||||
for g in groups
|
||||
]
|
||||
session.close()
|
||||
return jsonify(result)
|
||||
|
||||
|
||||
@app.route("/api/groups/<int:group_id>", methods=["PUT"])
|
||||
def update_group(group_id):
|
||||
"""Ändert den Namen oder Status einer Raumgruppe."""
|
||||
data = request.get_json()
|
||||
session = Session()
|
||||
group = session.query(ClientGroup).filter_by(id=group_id).first()
|
||||
if not group:
|
||||
session.close()
|
||||
return jsonify({"error": "Gruppe nicht gefunden"}), 404
|
||||
if "name" in data:
|
||||
group.name = data["name"]
|
||||
if "is_active" in data:
|
||||
group.is_active = bool(data["is_active"])
|
||||
session.commit()
|
||||
result = {
|
||||
"id": group.id,
|
||||
"name": group.name,
|
||||
"created_at": group.created_at.isoformat() if group.created_at else None,
|
||||
"is_active": group.is_active,
|
||||
}
|
||||
session.close()
|
||||
return jsonify(result)
|
||||
|
||||
|
||||
@app.route("/api/groups/<int:group_id>", methods=["DELETE"])
|
||||
def delete_group(group_id):
|
||||
"""Löscht eine Raumgruppe."""
|
||||
session = Session()
|
||||
group = session.query(ClientGroup).filter_by(id=group_id).first()
|
||||
if not group:
|
||||
session.close()
|
||||
return jsonify({"error": "Gruppe nicht gefunden"}), 404
|
||||
session.delete(group)
|
||||
session.commit()
|
||||
session.close()
|
||||
return jsonify({"success": True})
|
||||
|
||||
|
||||
@app.route("/api/clients/group", methods=["PUT"])
|
||||
def update_clients_group():
|
||||
data = request.get_json()
|
||||
client_ids = data.get("client_ids", [])
|
||||
group_name = data.get("group_name")
|
||||
print(f"Update Clients Group: {group_name}, IDs: {client_ids}")
|
||||
session = Session()
|
||||
group = session.query(ClientGroup).filter_by(name=group_name).first()
|
||||
if not group:
|
||||
session.close()
|
||||
return jsonify({"error": "Gruppe nicht gefunden"}), 404
|
||||
session.query(Client).filter(Client.uuid.in_(client_ids)).update(
|
||||
{Client.group_id: group.id}, synchronize_session=False
|
||||
)
|
||||
session.commit()
|
||||
session.close()
|
||||
return jsonify({"success": True})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host="0.0.0.0", port=8000, debug=True)
|
||||
|
||||
Reference in New Issue
Block a user