feat: initial implementation

This commit is contained in:
jfmcarreira committed 2026-10-05 22:36:27 +01:00
commit e29da874ee
39 files changed
+2309

No files matched your search

+123
View File
@@ -0,0 +1,123 @@
from __future__ import annotations
import sys
from datetime import date, timedelta
from sqlalchemy import select
from sqlalchemy.orm import selectinload
from .bootstrap import ensure_initial_data
from .config import settings
from .database import Base, SessionLocal, engine
from .models import Assignment, NotificationLog, RotationMember, Schedule, User
from .notifications import send_email_notification
from .scheduling import ensure_month_assignments
from .security import hash_password
def init_db() -> None:
Base.metadata.create_all(bind=engine)
with SessionLocal() as db:
ensure_initial_data(db)
print("Database initialized.")
def seed_demo() -> None:
init_db()
demo_people = [
("Alice", "alice", "alice@example.com"),
("Bruno", "bruno", "bruno@example.com"),
("Carla", "carla", None),
("Diogo", "diogo", "diogo@example.com"),
]
with SessionLocal() as db:
for name, username, email in demo_people:
if not db.scalar(select(User.id).where(User.username == username)):
db.add(
User(
name=name,
username=username,
email=email,
phone=None,
password_hash=hash_password("demo-password"),
is_active=True,
is_admin=False,
notify_email=True,
)
)
db.commit()
people = db.scalars(select(User).where(User.username.in_([x[1] for x in demo_people])).order_by(User.name)).all()
schedules = db.scalars(select(Schedule).where(Schedule.schedule_type == "rotation").order_by(Schedule.id)).all()
for schedule in schedules:
if not db.scalar(select(RotationMember.id).where(RotationMember.schedule_id == schedule.id)):
for pos, person in enumerate(people, start=1):
db.add(RotationMember(schedule_id=schedule.id, user_id=person.id, position=pos))
db.commit()
today = date.today()
assignments = ensure_month_assignments(db, today.year, today.month)
night_people = people or []
if night_people:
idx = 0
for assignment in assignments:
if assignment.schedule.slug == "night" and assignment.user_id is None:
assignment.user_id = night_people[idx % len(night_people)].id
assignment.source = "manual"
idx += 1
db.commit()
print("Demo data seeded. Demo member password: demo-password")
def send_reminders() -> None:
Base.metadata.create_all(bind=engine)
target = date.today() + timedelta(days=settings.reminder_days_ahead)
with SessionLocal() as db:
ensure_initial_data(db)
ensure_month_assignments(db, target.year, target.month)
assignments = db.scalars(
select(Assignment)
.where(Assignment.date == target, Assignment.user_id.is_not(None))
.options(selectinload(Assignment.user), selectinload(Assignment.schedule))
).all()
count = 0
for assignment in assignments:
if not assignment.user:
continue
subject = f"Rota reminder: {assignment.schedule.name} on {assignment.date:%d %b}"
already_sent = db.scalar(
select(NotificationLog.id).where(
NotificationLog.user_id == assignment.user.id,
NotificationLog.event_type == "shift_reminder",
NotificationLog.subject == subject,
NotificationLog.status == "sent",
)
)
if already_sent:
continue
send_email_notification(
db,
assignment.user,
"shift_reminder",
subject,
f"Reminder: you are assigned to {assignment.schedule.name} on "
f"{assignment.date:%A, %d %B %Y}.\n\nView the rota: {settings.normalized_base_url}/",
)
count += 1
print(f"Processed {count} reminder(s) for {target.isoformat()}.")
def main() -> None:
command = sys.argv[1] if len(sys.argv) > 1 else ""
commands = {
"init-db": init_db,
"seed-demo": seed_demo,
"send-reminders": send_reminders,
}
if command not in commands:
print("Usage: python -m app.cli [init-db|seed-demo|send-reminders]")
raise SystemExit(2)
commands[command]()
if __name__ == "__main__":
main()