173 lines
6.1 KiB
Python
173 lines
6.1 KiB
Python
"""Reset a safe, self-contained demo account with representative Tasknest data."""
|
||
from datetime import time, timedelta
|
||
|
||
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 planner.models import (
|
||
FastingPause,
|
||
FastingPeriod,
|
||
FastingSettings,
|
||
NeckHeadacheCheckIn,
|
||
SickDaySettings,
|
||
SleepCheckIn,
|
||
Task,
|
||
UserSettings,
|
||
Workout,
|
||
WorkoutCompletion,
|
||
)
|
||
from planner.services.fasting import synchronize_fasting_periods
|
||
from planner.timezones import DEFAULT_TIME_ZONE, localdate_for_user
|
||
|
||
|
||
class Command(BaseCommand):
|
||
help = 'Reset a demo account with safe, representative sample data.'
|
||
|
||
def add_arguments(self, parser):
|
||
parser.add_argument('--username', default='demo', help='Demo username (default: demo).')
|
||
parser.add_argument('--password', default='demo', help='Demo password (default: demo).')
|
||
|
||
@transaction.atomic
|
||
def handle(self, *args, **options):
|
||
username = options['username']
|
||
password = options['password']
|
||
user_model = get_user_model()
|
||
user, created = user_model.objects.get_or_create(username=username)
|
||
user.set_password(password)
|
||
user.is_active = True
|
||
user.save()
|
||
|
||
profile, _ = UserSettings.objects.get_or_create(user=user)
|
||
profile.time_zone = DEFAULT_TIME_ZONE
|
||
profile.appearance = UserSettings.Appearance.LIGHT
|
||
profile.latitude = '52.367600'
|
||
profile.longitude = '4.904100'
|
||
profile.onboarding_enabled = False
|
||
profile.onboarding_step = 0
|
||
profile.save()
|
||
SickDaySettings.objects.update_or_create(user=user, defaults={'enabled': False, 'recovery_date': None})
|
||
|
||
# Reset only demo-owned records. Cascades remove task weather shifts and workout completions.
|
||
Task.objects.filter(owner=user).delete()
|
||
Workout.objects.filter(owner=user).delete()
|
||
NeckHeadacheCheckIn.objects.filter(user=user).delete()
|
||
SleepCheckIn.objects.filter(user=user).delete()
|
||
FastingPause.objects.filter(user=user).delete()
|
||
FastingPeriod.objects.filter(user=user).delete()
|
||
|
||
now = timezone.now()
|
||
today = localdate_for_user(user, now)
|
||
|
||
laundry = Task.objects.create(
|
||
owner=user,
|
||
title='Run the washing machine',
|
||
description='A simple parent task to demonstrate dependencies.',
|
||
category='Home',
|
||
priority=Task.Priority.HIGH,
|
||
estimated_duration_minutes=10,
|
||
scheduled_for=today,
|
||
)
|
||
Task.objects.create(
|
||
owner=user,
|
||
title='Fold the clothes',
|
||
description='This appears after the washing-machine task is complete.',
|
||
category='Home',
|
||
priority=Task.Priority.MEDIUM,
|
||
estimated_duration_minutes=15,
|
||
scheduled_for=today,
|
||
parent_task=laundry,
|
||
)
|
||
finished_parent = Task.objects.create(
|
||
owner=user,
|
||
title='Plan this week',
|
||
category='Planning',
|
||
priority=Task.Priority.MEDIUM,
|
||
estimated_duration_minutes=20,
|
||
scheduled_for=today,
|
||
is_completed=True,
|
||
)
|
||
Task.objects.create(
|
||
owner=user,
|
||
title='Choose tomorrow’s priority',
|
||
category='Planning',
|
||
priority=Task.Priority.HIGH,
|
||
estimated_duration_minutes=5,
|
||
scheduled_for=today,
|
||
parent_task=finished_parent,
|
||
)
|
||
Task.objects.create(
|
||
owner=user,
|
||
title='Take a neighbourhood walk',
|
||
description='An outdoor task with a weather preference.',
|
||
category='Health',
|
||
priority=Task.Priority.LOW,
|
||
estimated_duration_minutes=30,
|
||
scheduled_for=today,
|
||
is_outdoor=True,
|
||
weather_profile=Task.WeatherProfile.DRY,
|
||
)
|
||
|
||
daily_stretch = Workout.objects.create(
|
||
owner=user,
|
||
name='Morning stretch',
|
||
scheduled_for=today - timedelta(days=7),
|
||
is_daily_recurring=True,
|
||
duration_minutes=10,
|
||
calories_burned=35,
|
||
)
|
||
WorkoutCompletion.objects.create(workout=daily_stretch, completed_on=today)
|
||
Workout.objects.create(
|
||
owner=user,
|
||
name='Easy cycle',
|
||
scheduled_for=today,
|
||
is_completed=False,
|
||
duration_minutes=30,
|
||
calories_burned=180,
|
||
)
|
||
|
||
for days_ago, neck, headache, physiotherapy, note in (
|
||
(3, 3, 1, False, 'A little stiff after desk work.'),
|
||
(2, 1, 0, True, 'Physiotherapy session.'),
|
||
(1, 2, 1, False, ''),
|
||
(0, 1, 0, False, 'Feeling better today.'),
|
||
):
|
||
NeckHeadacheCheckIn.objects.create(
|
||
user=user,
|
||
check_in_date=today - timedelta(days=days_ago),
|
||
neck_pain_severity=neck,
|
||
headache_severity=headache,
|
||
had_physiotherapy=physiotherapy,
|
||
note=note,
|
||
)
|
||
for days_ago, rested, watch_score, note in (
|
||
(3, 5, 68, 'Late evening.'),
|
||
(2, 7, 82, 'Good recovery.'),
|
||
(1, 6, None, ''),
|
||
(0, 8, 88, 'Rested this morning.'),
|
||
):
|
||
SleepCheckIn.objects.create(
|
||
user=user,
|
||
wake_up_date=today - timedelta(days=days_ago),
|
||
rested_score=rested,
|
||
watch_sleep_score=watch_score,
|
||
note=note,
|
||
)
|
||
|
||
fasting, _ = FastingSettings.objects.update_or_create(
|
||
user=user,
|
||
defaults={
|
||
'enabled': True,
|
||
'fasting_start_time': time(20, 0),
|
||
'fasting_duration_minutes': 16 * 60,
|
||
'tracking_started_at': now - timedelta(days=3),
|
||
},
|
||
)
|
||
synchronize_fasting_periods(fasting, now)
|
||
|
||
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.'
|
||
))
|