feat: separate schedules

This commit is contained in:
jfmcarreira committed 2026-10-06 00:59:48 +01:00
1 parent c3b263eafd
commit ad3041ec56
7 files changed
+198 -61

No files matched your search

+1 -1
View File
@@ -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`.
+2
View File
@@ -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)}
+41 -32
View File
@@ -1,39 +1,48 @@
{% extends "base.html" %}
{% block content %}
<div class="page-head"><div><h1>Schedules</h1><p class="muted">Saturday lunch and Sunday lunch rotate independently. Existing generated assignments are not rewritten when the rotation changes.</p></div></div>
<div class="page-head"><div><h1>Schedules</h1><p class="muted">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.</p></div></div>
<div class="stack gap-lg">
{% for schedule in schedules %}
<section class="panel">
<div class="row between wrap gap">
<div><h2>{{ schedule.name }}</h2><p class="muted">{{ schedule.schedule_type }} · weekdays {{ schedule.weekdays }} · {{ schedule.label_time }}</p></div>
<form method="post" action="/admin/schedules/{{ schedule.id }}/settings" class="inline-form">
{% for group in groups %}
<div class="stack gap">
<h2 class="group-title">{{ group.title }}</h2>
<p class="muted">{{ group.muted }}</p>
{% for card in group.cards %}
{% set schedule = card.schedule %}
<section class="panel">
<div class="row between wrap gap">
<div><h3>{{ schedule.name }}</h3><p class="muted">{{ card.kind_label }}{% if schedule.label_time %} · {{ schedule.label_time }}{% endif %}</p></div>
<form method="post" action="/admin/schedules/{{ schedule.id }}/settings" class="inline-form">
<input type="hidden" name="csrf_token" value="{{ csrf_token }}">
<label class="check"><input type="checkbox" name="requires_manager_approval" {% if schedule.requires_manager_approval %}checked{% endif %}> Manager approval</label>
<label class="check"><input type="checkbox" name="is_active" {% if schedule.is_active %}checked{% endif %}> Active</label>
<button class="secondary" type="submit">Save</button>
</form>
</div>
{% if card.kind == 'rotation' %}
<p class="muted">Next dates</p>
<p class="date-list">{{ card.dates | join(' · ') }}</p>
<h4>Rotation order</h4>
<ol class="rotation-list">
{% for member in schedule.rotation_members %}
<li>
<span>{{ member.user.name }}</span>
<div class="actions compact">
<form method="post" action="/admin/rotation/{{ member.id }}/move"><input type="hidden" name="csrf_token" value="{{ csrf_token }}"><input type="hidden" name="direction" value="up"><button class="secondary small" type="submit">↑</button></form>
<form method="post" action="/admin/rotation/{{ member.id }}/move"><input type="hidden" name="csrf_token" value="{{ csrf_token }}"><input type="hidden" name="direction" value="down"><button class="secondary small" type="submit">↓</button></form>
<form method="post" action="/admin/rotation/{{ member.id }}/remove"><input type="hidden" name="csrf_token" value="{{ csrf_token }}"><button class="danger small" type="submit">Remove</button></form>
</div>
</li>
{% else %}<li class="muted">No members yet.</li>{% endfor %}
</ol>
<form method="post" action="/admin/schedules/{{ schedule.id }}/rotation" class="inline-form">
<input type="hidden" name="csrf_token" value="{{ csrf_token }}">
<label class="check"><input type="checkbox" name="requires_manager_approval" {% if schedule.requires_manager_approval %}checked{% endif %}> Manager approval</label>
<label class="check"><input type="checkbox" name="is_active" {% if schedule.is_active %}checked{% endif %}> Active</label>
<button class="secondary" type="submit">Save</button>
<select name="user_id" required><option value="">Add person…</option>{% for person in users %}<option value="{{ person.id }}">{{ person.name }}</option>{% endfor %}</select>
<button class="primary" type="submit">Add to rotation</button>
</form>
</div>
{% if schedule.schedule_type == 'rotation' %}
<h3>Rotation order</h3>
<ol class="rotation-list">
{% for member in schedule.rotation_members %}
<li>
<span>{{ member.user.name }}</span>
<div class="actions compact">
<form method="post" action="/admin/rotation/{{ member.id }}/move"><input type="hidden" name="csrf_token" value="{{ csrf_token }}"><input type="hidden" name="direction" value="up"><button class="secondary small" type="submit">↑</button></form>
<form method="post" action="/admin/rotation/{{ member.id }}/move"><input type="hidden" name="csrf_token" value="{{ csrf_token }}"><input type="hidden" name="direction" value="down"><button class="secondary small" type="submit">↓</button></form>
<form method="post" action="/admin/rotation/{{ member.id }}/remove"><input type="hidden" name="csrf_token" value="{{ csrf_token }}"><button class="danger small" type="submit">Remove</button></form>
</div>
</li>
{% else %}<li class="muted">No members yet.</li>{% endfor %}
</ol>
<form method="post" action="/admin/schedules/{{ schedule.id }}/rotation" class="inline-form">
<input type="hidden" name="csrf_token" value="{{ csrf_token }}">
<select name="user_id" required><option value="">Add person…</option>{% for person in users %}<option value="{{ person.id }}">{{ person.name }}</option>{% endfor %}</select>
<button class="primary" type="submit">Add to rotation</button>
</form>
{% endif %}
</section>
{% endif %}
</section>
{% endfor %}
</div>
{% endfor %}
</div>
{% endblock %}
{% endblock %}
+24 -16
View File
@@ -1,22 +1,30 @@
{% extends "base.html" %}
{% block title %}Schedules · {{ settings.app_name }}{% endblock %}
{% block content %}
<div class="page-head"><div><h1>Schedules</h1><p class="muted">Saturday lunch and Sunday lunch rotate independently. Sign in to see who is on duty for a given month.</p></div></div>
<div class="page-head"><div><h1>Schedules</h1><p class="muted">The night schedule repeats every month. Saturday lunch and Sunday lunch are separate rotations that never share an order.</p></div></div>
<div class="stack gap-lg">
{% for card in cards %}
<section class="panel">
<h2>{{ card.schedule.name }}</h2>
<p class="muted">{{ card.schedule.schedule_type }}{% if card.schedule.label_time %} · {{ card.schedule.label_time }}{% endif %}</p>
<p class="muted">{{ card.weekday_label }}</p>
{% if card.schedule.schedule_type == 'rotation' %}
<h3>Rotation order</h3>
<ol class="rotation-list">
{% for name in card.rotation_names %}
<li><span>{{ name }}</span></li>
{% else %}<li class="muted">No members yet.</li>{% endfor %}
</ol>
{% endif %}
</section>
{% for group in groups %}
<div class="stack gap">
<h2 class="group-title">{{ group.title }}</h2>
<p class="muted">{{ group.muted }}</p>
{% for card in group.cards %}
<section class="panel">
<div class="row between wrap gap">
<div><h3>{{ card.schedule.name }}</h3><p class="muted">{{ card.kind_label }}{% if card.schedule.label_time %} · {{ card.schedule.label_time }}{% endif %}</p></div>
</div>
{% if card.kind == 'rotation' %}
<p class="muted">Next dates</p>
<p class="date-list">{{ card.dates | join(' · ') }}</p>
<h4>Rotation order</h4>
<ol class="rotation-list">
{% for name in card.rotation_names %}
<li><span>{{ name }}</span></li>
{% else %}<li class="muted">No members yet.</li>{% endfor %}
</ol>
{% endif %}
</section>
{% endfor %}
</div>
{% else %}
<div class="empty">No schedules are published yet.</div>
{% endfor %}
@@ -24,4 +32,4 @@
{% if not current_user %}
<div class="top-space"><a class="button primary" href="/login">Sign in</a></div>
{% endif %}
{% endblock %}
{% endblock %}
+64 -11
View File
@@ -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),
)
+4
View File
@@ -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"]
+62 -1
View File
@@ -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