Files
putzliga/app/cli.py

87 lines
2.9 KiB
Python

from __future__ import annotations
import click
from .extensions import db
from .models import BadgeDefinition, User
from .services.monthly import archive_months_missing_up_to_previous
from .services.notifications import send_due_notifications, send_monthly_winner_notifications
DEFAULT_BADGES = [
{
"key": "early_bird",
"name": "Frühstarter",
"description": "Erledige 3 Aufgaben vor ihrem Fälligkeitsdatum.",
"icon_name": "bell",
"trigger_type": "early_finisher_count",
"threshold": 3,
"bonus_points": 10,
},
{
"key": "on_time_streak",
"name": "Sauberer Lauf",
"description": "Erledige Aufgaben an 3 Tagen in Folge.",
"icon_name": "check",
"trigger_type": "streak_days",
"threshold": 3,
"bonus_points": 15,
},
{
"key": "task_sprinter",
"name": "Putz-Sprinter",
"description": "Schließe 8 Aufgaben in einem Monat ab.",
"icon_name": "trophy",
"trigger_type": "monthly_task_count",
"threshold": 8,
"bonus_points": 20,
},
]
def seed_badges() -> None:
for payload in DEFAULT_BADGES:
badge = BadgeDefinition.query.filter_by(key=payload["key"]).first()
if not badge:
db.session.add(BadgeDefinition(**payload))
db.session.commit()
def register_cli(app) -> None:
@app.cli.command("init-db")
def init_db_command():
db.create_all()
seed_badges()
click.echo("Datenbank und Standard-Badges sind bereit.")
@app.cli.command("create-user")
@click.option("--name", prompt=True, help="Anzeigename")
@click.option("--email", prompt=True, help="E-Mail")
@click.option("--password", prompt=True, hide_input=True, confirmation_prompt=True, help="Passwort")
def create_user_command(name: str, email: str, password: str):
existing = User.query.filter_by(email=email.lower().strip()).first()
if existing:
click.echo("Es existiert bereits ein Nutzer mit dieser E-Mail.")
raise SystemExit(1)
user = User(name=name.strip(), email=email.lower().strip())
user.set_password(password)
db.session.add(user)
db.session.commit()
click.echo(f"Nutzer {user.email} wurde angelegt.")
@app.cli.command("archive-months")
def archive_months_command():
archive_months_missing_up_to_previous()
click.echo("Monatsarchiv wurde geprüft.")
@app.cli.command("notify-due")
def notify_due_command():
result = send_due_notifications()
click.echo(f"Due-Push: sent={result.sent} skipped={result.skipped} failed={result.failed}")
@app.cli.command("notify-monthly-winner")
def notify_monthly_winner_command():
result = send_monthly_winner_notifications()
click.echo(f"Winner-Push: sent={result.sent} skipped={result.skipped} failed={result.failed}")