Files
Django_TasksApp/main/planner/context_processors.py
T
2026-10-22 10:32:41 +02:00

44 lines
2.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Template context shared by Tasknest pages."""
from .models import UserSettings
from django.urls import reverse
def appearance(request):
"""Expose the signed-in user’s saved colour preference without creating settings."""
user = getattr(request, "user", None)
if user is not None and user.is_authenticated:
profile = UserSettings.objects.filter(user=user).values('appearance', 'is_demo').first()
return {"user_appearance": profile['appearance'] if profile else UserSettings.Appearance.LIGHT,
"is_demo_account": bool(profile and profile['is_demo'])}
return {"user_appearance": UserSettings.Appearance.LIGHT, "is_demo_account": False}
def navigation(request):
"""A single site navigation tree for desktop and mobile."""
user = getattr(request, 'user', None)
if not user or not user.is_authenticated:
return {'nav_groups': ()}
current = request.path
definitions = (
('tasks', 'My tasks', 'planner:task-list', (('Daily planner', 'planner:daily-planner'),)),
('health', 'Health', 'planner:health', (('Stress', 'stress:journal'), ('Dreams', 'dreams:journal'))),
('food', 'Food', 'food:diary', (('Recipes', 'recipes:list'),
('Kitchen Inventory', 'food:inventory'),
('Groceries', 'groceries:index'))),
)
groups = []
for key, label, route, children in definitions:
url = reverse(route)
items = [{'label': child_label, 'url': reverse(child_route),
'active': current.startswith(reverse(child_route))}
for child_label, child_route in children]
# Food's root URL is a prefix of its children; exact match keeps its parent state clear.
parent_active = current == url or (key == 'food' and
getattr(request.resolver_match, 'namespace', '') == 'food' and
not any(item['active'] for item in items))
groups.append({'key': key, 'label': label, 'url': url, 'items': items,
'parent_active': parent_active,
'active': parent_active or any(item['active'] for item in items)})
return {'nav_groups': groups}