adding food logging, polish + demo updates

This commit is contained in:
Joren de Graaf
2026-10-03 21:44:38 +02:00
parent 90b21a39fc
commit be49b66dae
7 changed files with 184 additions and 13 deletions
+111 -3
View File
@@ -1,12 +1,16 @@
"""Reset a safe, self-contained demo account with representative Tasknest data."""
from datetime import time, timedelta
from datetime import datetime, time, timedelta
from decimal import Decimal
from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand
from django.db import transaction
from django.utils import timezone
from food.models import FoodFavourite, FoodLogEntry, FoodPortion, FoodProduct, MealType, Unit
from planner.models import (
BowelHealthCheckIn,
FastingPause,
FastingPeriod,
FastingSettings,
@@ -14,12 +18,76 @@ from planner.models import (
SickDaySettings,
SleepCheckIn,
Task,
ToothBrushing,
UserSettings,
WaterIntakeEntry,
Workout,
WorkoutCompletion,
)
from planner.services.fasting import synchronize_fasting_periods
from planner.timezones import DEFAULT_TIME_ZONE, localdate_for_user
from planner.timezones import DEFAULT_TIME_ZONE, localdate_for_user, timezone_for_user
def _seed_food(user, today):
"""Create local demo foods without network calls or third-party assets."""
product_data = (
('Greek yoghurt', 'Demo dairy', Unit.GRAM, '73', '4', '8', '2', '0'),
('Banana', '', Unit.GRAM, '89', '22.8', '1.1', '0.3', '2.6'),
('Wholegrain sandwich', 'Demo bakery', Unit.GRAM, '250', '42', '10', '4', '6'),
('Tomato soup', 'Home made', Unit.MILLILITRE, '35', '5', '1.2', '1', None),
('Pasta with vegetables', 'Home made', Unit.GRAM, '145', '23', '5', '3', '2.5'),
('Almonds', '', Unit.GRAM, '579', '10', '21', '50', '12.5'),
)
products = {}
for name, brand, unit, kcal, carbs, protein, fat, fibre in product_data:
products[name] = FoodProduct.objects.create(
owner=user, product_name=name, brand=brand, base_unit=unit,
energy_kcal_100=Decimal(kcal), carbohydrates_100=Decimal(carbs),
protein_100=Decimal(protein), fat_100=Decimal(fat),
fibre_100=Decimal(fibre) if fibre is not None else None,
source=FoodProduct.Source.MANUAL,
)
yoghurt = products['Greek yoghurt']
soup = products['Tomato soup']
for product in (yoghurt, soup):
FoodFavourite.objects.create(user=user, product=product)
FoodPortion.objects.create(user=user, product=yoghurt, label='My bowl',
amount=Decimal('250'), unit=Unit.GRAM, is_default=True)
FoodPortion.objects.create(user=user, product=soup, label='One mug',
amount=Decimal('250'), unit=Unit.MILLILITRE)
def log(days_ago, meal, name, amount, label=''):
product = products[name]
amount = Decimal(str(amount))
factor = amount / Decimal('100')
def consumed(value):
return (value * factor).quantize(Decimal('0.001')) if value is not None else None
FoodLogEntry.objects.create(
user=user, date=today - timedelta(days=days_ago), meal=meal, product=product,
product_name_snapshot=product.product_name, brand_snapshot=product.brand,
portion_label_snapshot=label, amount=amount, unit=product.base_unit,
calories_kcal=consumed(product.energy_kcal_100),
carbohydrates_g=consumed(product.carbohydrates_100),
protein_g=consumed(product.protein_100), fat_g=consumed(product.fat_100),
fibre_g=consumed(product.fibre_100), source=product.source,
)
for days_ago, meal, name, amount, label in (
(0, MealType.BREAKFAST, 'Greek yoghurt', 250, 'My bowl'),
(0, MealType.BREAKFAST, 'Banana', 120, ''),
(0, MealType.LUNCH, 'Wholegrain sandwich', 100, ''),
(0, MealType.LUNCH, 'Tomato soup', 250, 'One mug'),
(0, MealType.DINNER, 'Pasta with vegetables', 180, ''),
(0, MealType.SNACKS, 'Almonds', 30, ''),
(1, MealType.BREAKFAST, 'Greek yoghurt', 200, ''),
(1, MealType.DINNER, 'Pasta with vegetables', 200, ''),
(2, MealType.LUNCH, 'Wholegrain sandwich', 100, ''),
(5, MealType.SNACKS, 'Banana', 100, ''),
):
log(days_ago, meal, name, amount, label)
return len(products), 10
class Command(BaseCommand):
@@ -46,6 +114,14 @@ class Command(BaseCommand):
profile.longitude = '4.904100'
profile.onboarding_enabled = False
profile.onboarding_step = 0
profile.daily_water_goal_ml = 2000
profile.daily_calorie_goal = 2000
profile.carbohydrate_goal_g = Decimal('250.0')
profile.protein_goal_g = Decimal('100.0')
profile.fat_goal_g = Decimal('70.0')
profile.ntfy_url = ''
profile.ntfy_enabled = False
profile.ntfy_last_tested_at = None
profile.save()
SickDaySettings.objects.update_or_create(user=user, defaults={'enabled': False, 'recovery_date': None})
@@ -56,10 +132,37 @@ class Command(BaseCommand):
SleepCheckIn.objects.filter(user=user).delete()
FastingPause.objects.filter(user=user).delete()
FastingPeriod.objects.filter(user=user).delete()
WaterIntakeEntry.objects.filter(user=user).delete()
ToothBrushing.objects.filter(user=user).delete()
BowelHealthCheckIn.objects.filter(user=user).delete()
FoodLogEntry.objects.filter(user=user).delete()
FoodFavourite.objects.filter(user=user).delete()
FoodPortion.objects.filter(user=user).delete()
FoodProduct.objects.filter(owner=user).delete()
now = timezone.now()
today = localdate_for_user(user, now)
ToothBrushing.objects.create(user=user, date=today - timedelta(days=1), morning=True, evening=True)
ToothBrushing.objects.create(user=user, date=today, morning=True, evening=False)
for days_ago, severity, note in (
(2, 3, 'A slightly unsettled day.'),
(1, 1, 'Feeling better after lunch.'),
(0, 0, 'Feeling well today.'),
):
BowelHealthCheckIn.objects.create(
user=user, check_in_date=today - timedelta(days=days_ago),
morning_severity=severity, morning_note=note,
afternoon_severity=max(0, severity - 1),
evening_severity=max(0, severity - 1),
)
yesterday = today - timedelta(days=1)
local_zone = timezone_for_user(user)
for hour, amount in ((9, 350), (12, 500), (16, 400)):
WaterIntakeEntry.objects.create(user=user, amount_ml=amount,
recorded_at=timezone.make_aware(datetime.combine(yesterday, time(hour, 0)), local_zone))
WaterIntakeEntry.objects.create(user=user, amount_ml=350, recorded_at=now)
laundry = Task.objects.create(
owner=user,
title='Run the washing machine',
@@ -168,11 +271,16 @@ class Command(BaseCommand):
'fasting_start_time': time(20, 0),
'fasting_duration_minutes': 16 * 60,
'tracking_started_at': now - timedelta(days=3),
'last_phase_notification_at': None,
},
)
synchronize_fasting_periods(fasting, now)
food_products, food_entries = _seed_food(user, today)
verb = 'Created' if created else 'Reset'
self.stdout.write(self.style.SUCCESS(
f'{verb} demo user {username!r}: 5 tasks, 2 workouts, 4 pain check-ins, and 4 sleep check-ins.'
f'{verb} demo user {username!r}: 5 tasks, 2 workouts, 4 pain check-ins, '
f'4 sleep check-ins, 3 bowel check-ins, 4 water entries, '
f'{food_products} foods, and {food_entries} food logs.'
))