From ad3041ec564f769ea9b954064c49ddcb6052d1b3 Mon Sep 17 00:00:00 2001 From: Joao Carreira Date: Tue, 6 Oct 2026 00:59:48 +0100 Subject: [PATCH] feat: separate schedules --- AGENTS.md | 2 +- app/static/app.css | 2 + app/templates/admin_schedules.html | 73 ++++++++++++++++------------- app/templates/schedules.html | 40 +++++++++------- app/web.py | 75 +++++++++++++++++++++++++----- docker-compose.dev.yml | 4 ++ tests/test_public_schedules.py | 63 ++++++++++++++++++++++++- 7 files changed, 198 insertions(+), 61 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index de22563..fc73d2e 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -66,7 +66,7 @@ make test-docker docker compose -f docker-compose.dev.yml --profile test run --rm tests ``` -The `dev` target of the `Dockerfile` installs `requirements-dev.txt` on top of the app image; the `tests` service in `docker-compose.dev.yml` runs `pytest` with throwaway settings (`SECRET_KEY`, `DATABASE_URL`, `INITIAL_ADMIN_PASSWORD`). It mounts no host volumes and does not read `.env`, so it is safe on a fresh clone. +The `dev` target of the `Dockerfile` installs `requirements-dev.txt` on top of the app image; the `tests` service in `docker-compose.dev.yml` runs `pytest` with throwaway settings (`SECRET_KEY`, `DATABASE_URL`, `INITIAL_ADMIN_PASSWORD`). It bind-mounts `app/`, `tests/` and `pyproject.toml` so the suite always runs the working tree, keeps `data/` inside the container, and does not read `.env`, so it is safe on a fresh clone. `docker-compose.yml` is the service stack only. Anything development-related (auto-reload, bind mounts, test runner) belongs in `docker-compose.dev.yml`. diff --git a/app/static/app.css b/app/static/app.css index 88e542b..ac2909f 100644 --- a/app/static/app.css +++ b/app/static/app.css @@ -86,6 +86,8 @@ details summary{cursor:pointer;color:var(--brand-dark);font-weight:600} .rotation-list{max-width:620px;padding-left:24px} .rotation-list li{padding:8px 0;border-bottom:1px solid var(--line);display:list-item} .rotation-list li>span{display:inline-block;min-width:220px} +.group-title{margin:0} +.date-list{margin:0;font-weight:600} .table-wrap{overflow-x:auto;background:#fff;border:1px solid var(--line);border-radius:12px} table{width:100%;border-collapse:collapse;min-width:650px} th,td{text-align:left;padding:10px 12px;border-bottom:1px solid var(--line)} diff --git a/app/templates/admin_schedules.html b/app/templates/admin_schedules.html index de11371..4038ee8 100644 --- a/app/templates/admin_schedules.html +++ b/app/templates/admin_schedules.html @@ -1,39 +1,48 @@ {% extends "base.html" %} {% block content %} -

Schedules

Saturday lunch and Sunday lunch rotate independently. Existing generated assignments are not rewritten when the rotation changes.

+

Schedules

The night schedule is fixed and repeats every month. Saturday and Sunday lunch are independent rotations. Existing generated assignments are not rewritten when a rotation changes.

-{% for schedule in schedules %} -
-
-

{{ schedule.name }}

{{ schedule.schedule_type }} · weekdays {{ schedule.weekdays }} · {{ schedule.label_time }}

-
+{% for group in groups %} +
+

{{ group.title }}

+

{{ group.muted }}

+ {% for card in group.cards %} + {% set schedule = card.schedule %} +
+
+

{{ schedule.name }}

{{ card.kind_label }}{% if schedule.label_time %} · {{ schedule.label_time }}{% endif %}

+ + + + + + +
+ {% if card.kind == 'rotation' %} +

Next dates

+

{{ card.dates | join(' · ') }}

+

Rotation order

+
    + {% for member in schedule.rotation_members %} +
  1. + {{ member.user.name }} +
    +
    +
    +
    +
    +
  2. + {% else %}
  3. No members yet.
  4. {% endfor %} +
+
- - - + +
-
- {% if schedule.schedule_type == 'rotation' %} -

Rotation order

-
    - {% for member in schedule.rotation_members %} -
  1. - {{ member.user.name }} -
    -
    -
    -
    -
    -
  2. - {% else %}
  3. No members yet.
  4. {% endfor %} -
-
- - - -
- {% endif %} -
+ {% endif %} + + {% endfor %} +
{% endfor %} -{% endblock %} +{% endblock %} \ No newline at end of file diff --git a/app/templates/schedules.html b/app/templates/schedules.html index e33964f..90520ac 100644 --- a/app/templates/schedules.html +++ b/app/templates/schedules.html @@ -1,22 +1,30 @@ {% extends "base.html" %} {% block title %}Schedules · {{ settings.app_name }}{% endblock %} {% block content %} -

Schedules

Saturday lunch and Sunday lunch rotate independently. Sign in to see who is on duty for a given month.

+

Schedules

The night schedule repeats every month. Saturday lunch and Sunday lunch are separate rotations that never share an order.

-{% for card in cards %} -
-

{{ card.schedule.name }}

-

{{ card.schedule.schedule_type }}{% if card.schedule.label_time %} · {{ card.schedule.label_time }}{% endif %}

-

{{ card.weekday_label }}

- {% if card.schedule.schedule_type == 'rotation' %} -

Rotation order

-
    - {% for name in card.rotation_names %} -
  1. {{ name }}
  2. - {% else %}
  3. No members yet.
  4. {% endfor %} -
- {% endif %} -
+{% for group in groups %} +
+

{{ group.title }}

+

{{ group.muted }}

+ {% for card in group.cards %} +
+
+

{{ card.schedule.name }}

{{ card.kind_label }}{% if card.schedule.label_time %} · {{ card.schedule.label_time }}{% endif %}

+
+ {% if card.kind == 'rotation' %} +

Next dates

+

{{ card.dates | join(' · ') }}

+

Rotation order

+
    + {% for name in card.rotation_names %} +
  1. {{ name }}
  2. + {% else %}
  3. No members yet.
  4. {% endfor %} +
+ {% endif %} +
+ {% endfor %} +
{% else %}
No schedules are published yet.
{% endfor %} @@ -24,4 +32,4 @@ {% if not current_user %} {% endif %} -{% endblock %} +{% endblock %} \ No newline at end of file diff --git a/app/web.py b/app/web.py index 59836ff..b6c5493 100644 --- a/app/web.py +++ b/app/web.py @@ -1,7 +1,8 @@ from __future__ import annotations import calendar -from datetime import date, datetime +from collections.abc import Sequence +from datetime import date, datetime, timedelta from fastapi import APIRouter, Depends, Form, HTTPException, Request from fastapi.responses import HTMLResponse, RedirectResponse @@ -85,6 +86,65 @@ def _weekday_labels(weekdays: set[int]) -> str: return ", ".join(calendar.day_name[d] for d in sorted(weekdays)) +ALL_WEEKDAYS = frozenset(range(7)) + + +def _next_dates(weekdays: set[int], count: int = 6) -> list[str]: + today = date.today() + labels: list[str] = [] + day = today + while len(labels) < count: + if day.weekday() in weekdays: + labels.append(f"{day.strftime('%a')} {day.day} {day.strftime('%b')}") + day += timedelta(days=1) + return labels + + +def _schedule_card(schedule: Schedule) -> dict: + weekdays = schedule.weekday_set + fixed = schedule.schedule_type != "rotation" and weekdays == ALL_WEEKDAYS + if fixed: + kind_label = "Every day · repeats every month" + weekday_label = "Every day" + dates: list[str] = [] + else: + weekday_label = _weekday_labels(weekdays) + kind_label = f"Rotating · {weekday_label}" + dates = _next_dates(weekdays) + return { + "schedule": schedule, + "kind": "fixed" if fixed else "rotation", + "kind_label": kind_label, + "weekday_label": weekday_label, + "dates": dates, + "rotation_names": [member.user.name for member in schedule.rotation_members], + } + + +def _schedule_groups(schedules: Sequence[Schedule]) -> list[dict]: + cards = [_schedule_card(schedule) for schedule in schedules] + groups = [ + { + "key": "fixed", + "title": "Repeats every month", + "muted": "One fixed pattern; the same days come round again every month.", + "cards": [card for card in cards if card["kind"] == "fixed"], + } + ] + # Every rotation is independent: Saturday and Sunday lunch never share an order. + for card in cards: + if card["kind"] == "rotation": + groups.append( + { + "key": f"rotation-{card['schedule'].id}", + "title": f"{card['weekday_label']} rotation", + "muted": "Independent rotation, filled in order.", + "cards": [card], + } + ) + return [group for group in groups if group["cards"]] + + def _month_nav(year: int, month: int) -> tuple[tuple[int, int], tuple[int, int]]: if month == 1: prev = (year - 1, 12) @@ -146,18 +206,11 @@ def public_schedules(request: Request, db: Session = Depends(get_db)): .options(selectinload(Schedule.rotation_members).selectinload(RotationMember.user)) .order_by(Schedule.id) ).all() - cards = [ - { - "schedule": schedule, - "weekday_label": _weekday_labels(schedule.weekday_set), - "rotation_names": [member.user.name for member in schedule.rotation_members], - } - for schedule in schedules - ] + groups = _schedule_groups(schedules) return request.app.state.templates.TemplateResponse( request=request, name="schedules.html", - context=_context(request, db, cards=cards), + context=_context(request, db, groups=groups), ) @@ -614,7 +667,7 @@ def admin_schedules(request: Request, db: Session = Depends(get_db)): return request.app.state.templates.TemplateResponse( request=request, name="admin_schedules.html", - context=_context(request, db, schedules=schedules, users=users), + context=_context(request, db, groups=_schedule_groups(schedules), users=users), ) diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 4fe6717..ad13c37 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -29,4 +29,8 @@ services: DATABASE_URL: sqlite:///./data/test.db INITIAL_ADMIN_PASSWORD: test-only-password PYTHONPATH: /app + volumes: + - ./app:/app/app + - ./tests:/app/tests + - ./pyproject.toml:/app/pyproject.toml:ro command: ["pytest", "-ra"] \ No newline at end of file diff --git a/tests/test_public_schedules.py b/tests/test_public_schedules.py index bd45715..ef95788 100644 --- a/tests/test_public_schedules.py +++ b/tests/test_public_schedules.py @@ -1,5 +1,5 @@ import re -from datetime import date +from datetime import date, timedelta import pytest from fastapi.testclient import TestClient @@ -120,6 +120,45 @@ def test_public_schedules_show_details_and_rotation_order(client, db): assert "Bruno" in page +def test_fixed_schedule_is_described_as_repeating_every_month(client, db): + seed_schedules(db) + + page = client.get("/schedules").text + + assert "Repeats every month" in page + assert "Every day" in page + + +def test_raw_weekday_numbers_are_never_shown(client, db): + seed_schedules(db) + + page = client.get("/schedules").text + + assert "0,1,2,3,4,5,6" not in page + assert "weekdays" not in page + + +def test_saturday_and_sunday_rotations_are_separate_groups(client, db): + seed_schedules(db) + + page = client.get("/schedules").text + + assert "Saturday rotation" in page + assert "Sunday rotation" in page + + +def test_rotations_show_upcoming_dates_instead_of_weekdays(client, db): + _, sat, _ = seed_schedules(db) + + page = client.get("/schedules").text + + first_saturday = date.today() + while first_saturday.weekday() != 5: + first_saturday += timedelta(days=1) + expected = f"{first_saturday.strftime('%a')} {first_saturday.day} {first_saturday.strftime('%b')}" + assert expected in page + + def test_public_schedules_omit_inactive_schedules(client, db): add_schedule(db, name="Retired Shift", slug="retired", is_active=False) db.commit() @@ -167,3 +206,25 @@ def test_rota_stays_private(client, db): seed_schedules(db) assert client.get("/", follow_redirects=False).status_code == 401 + + +def test_admin_schedules_page_separates_the_three_schedule_types(client, db): + seed_schedules(db) + db.add( + User( + name="Admin", + username="admin", + password_hash=hash_password("admin-password"), + is_active=True, + is_admin=True, + ) + ) + db.commit() + login(client, "admin", "admin-password") + + page = client.get("/admin/schedules").text + + assert "Repeats every month" in page + assert "Saturday rotation" in page + assert "Sunday rotation" in page + assert "weekdays" not in page