feat: add task archive and simplify task cards

This commit is contained in:
2026-04-16 12:00:07 +02:00
parent 8cab2d1929
commit 03d3a50169
7 changed files with 314 additions and 70 deletions

View File

@@ -2,7 +2,7 @@ from __future__ import annotations
import calendar
from collections import defaultdict
from datetime import date
from datetime import date, timedelta
from flask import Blueprint, flash, redirect, render_template, request, url_for
from flask_login import current_user, login_required
@@ -37,15 +37,72 @@ def _my_tasks_soon_priority(task: TaskInstance) -> int:
order = {
"due_tomorrow": 0,
"due_day_after_tomorrow": 1,
"due_today": 2,
"open": 2,
}
return order.get(task.status, 99)
def _task_sort_key(task: TaskInstance, sort: str) -> tuple:
if sort == "points":
return (-task.points_awarded, task.due_date, task.title.lower())
if sort == "user":
return (task.assignee_label.lower(), task.due_date, task.title.lower())
return (task.due_date, task.title.lower())
def _group_active_tasks(tasks: list[TaskInstance], *, sort: str = "due") -> dict[str, list[TaskInstance]]:
sections = {
"overdue": [],
"today": [],
"soon": [],
"open": [],
}
for task in tasks:
if task.is_completed:
continue
if task.status == "overdue":
sections["overdue"].append(task)
elif task.status == "due_today":
sections["today"].append(task)
elif task.status in {"due_tomorrow", "due_day_after_tomorrow"}:
sections["soon"].append(task)
else:
sections["open"].append(task)
sections["overdue"].sort(key=lambda task: _task_sort_key(task, sort))
sections["today"].sort(key=lambda task: _task_sort_key(task, sort))
sections["soon"].sort(
key=lambda task: (_my_tasks_soon_priority(task),) + _task_sort_key(task, sort)
)
sections["open"].sort(key=lambda task: _task_sort_key(task, sort))
return sections
def _archive_day_priority(day: date, today: date) -> tuple[int, int]:
if day == today:
return (0, 0)
if day == today - timedelta(days=1):
return (1, 0)
if day == today - timedelta(days=2):
return (2, 0)
return (3, -day.toordinal())
def _archive_day_label(day: date, today: date) -> str:
if day == today:
return "Heute"
if day == today - timedelta(days=1):
return "Gestern"
if day == today - timedelta(days=2):
return "Vorgestern"
return day.strftime("%d.%m.%Y")
@bp.route("/my-tasks")
@login_required
def my_tasks():
tasks = (
all_tasks = (
TaskInstance.query.filter(
or_(
TaskInstance.assigned_user_id == current_user.id,
@@ -55,38 +112,21 @@ def my_tasks():
.order_by(TaskInstance.completed_at.is_(None).desc(), TaskInstance.due_date.asc())
.all()
)
refresh_task_statuses(tasks)
refresh_task_statuses(all_tasks)
sections = _group_active_tasks(all_tasks)
sections = {
"open": [],
"due_today": [],
"due_tomorrow": [],
"due_day_after_tomorrow": [],
"overdue": [],
"completed": [],
}
for task in tasks:
sections[task.status].append(task)
soon_tasks = sorted(
sections["due_tomorrow"] + sections["due_day_after_tomorrow"] + sections["due_today"],
key=lambda task: (_my_tasks_soon_priority(task), task.due_date, task.title.lower()),
)
completed_count = len(sections["completed"])
completed_count = len([task for task in all_tasks if task.is_completed])
active_count = (
len(sections["open"])
+ len(sections["due_today"])
+ len(sections["due_tomorrow"])
+ len(sections["due_day_after_tomorrow"])
+ len(sections["overdue"])
len(sections["overdue"])
+ len(sections["today"])
+ len(sections["soon"])
+ len(sections["open"])
)
completion_ratio = 0 if completed_count + active_count == 0 else round(completed_count / (completed_count + active_count) * 100)
return render_template(
"tasks/my_tasks.html",
sections=sections,
soon_tasks=soon_tasks,
completion_ratio=completion_ratio,
today=today_local(),
)
@@ -128,10 +168,9 @@ def all_tasks():
if status != "all":
if status == "soon":
tasks = [task for task in tasks if task.status in {"due_today", "due_tomorrow", "due_day_after_tomorrow"}]
tasks = [task for task in tasks if task.status in {"due_tomorrow", "due_day_after_tomorrow"}]
else:
status_map = {
"completed": "completed",
"overdue": "overdue",
"open": "open",
"today": "due_today",
@@ -141,15 +180,58 @@ def all_tasks():
selected = status_map.get(status)
if selected:
tasks = [task for task in tasks if task.status == selected]
tasks = [task for task in tasks if not task.is_completed]
else:
tasks = [task for task in tasks if not task.is_completed]
sections = _group_active_tasks(tasks, sort=sort)
return render_template(
"tasks/all_tasks.html",
tasks=tasks,
sections=sections,
users=User.query.order_by(User.name.asc()).all(),
filters={"status": status, "mine": mine, "user_id": user_filter, "sort": sort},
)
@bp.route("/archive")
@login_required
def archive_view():
selected_user_id = request.args.get("user_id", type=int) or current_user.id
archive_users = User.query.order_by(User.name.asc()).all()
selected_user = next((user for user in archive_users if user.id == selected_user_id), current_user)
ordered_users = sorted(archive_users, key=lambda user: (user.id != current_user.id, user.name.lower()))
completed_tasks = (
TaskInstance.query.filter_by(completed_by_user_id=selected_user.id)
.filter(TaskInstance.completed_at.isnot(None))
.order_by(TaskInstance.completed_at.desc(), TaskInstance.updated_at.desc())
.all()
)
today = today_local()
grouped: dict[date, list[TaskInstance]] = defaultdict(list)
for task in completed_tasks:
grouped[task.completed_at.date()].append(task)
archive_sections = [
{
"label": _archive_day_label(day, today),
"day": day,
"tasks": grouped[day],
}
for day in sorted(grouped.keys(), key=lambda value: _archive_day_priority(value, today))
]
return render_template(
"tasks/archive.html",
archive_users=ordered_users,
selected_user=selected_user,
archive_sections=archive_sections,
)
@bp.route("/tasks/new", methods=["GET", "POST"])
@login_required
def create():