Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add new migration to prefill the end dates for nutritional plans

Roland Geider22f1a3ec848d
diff --git a/wger/nutrition/migrations/0027_prefill_end_date.py b/wger/nutrition/migrations/0027_prefill_end_date.py
new file mode 100644
index 000000000..dfe074a80
--- /dev/null
+++ b/wger/nutrition/migrations/0027_prefill_end_date.py
@@ -0,0 +1,34 @@
+# Generated by Django 5.2.6 on 2025-09-16 20:54
+
+from django.db import migrations
+
+
+def prefill_end_date(apps, schema_editor):
+ """
+ Sets the end date of each user's nutrition plan to the start date of their
+ next one. The last one is left unchanged.
+ """
+ NutritionPlan = apps.get_model('nutrition', 'NutritionPlan')
+ user_ids = NutritionPlan.objects.order_by().values_list('user_id', flat=True).distinct()
+
+ for user_id in user_ids:
+ plans = list(
+ NutritionPlan.objects.filter(user_id=user_id).order_by('start', 'id')
+ )
+ for i in range(len(plans) - 1):
+ current = plans[i]
+ next_plan = plans[i + 1]
+ NutritionPlan.objects.filter(pk=current.pk).update(end=next_plan.start)
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ('nutrition', '0026_add_start_and_end_fields'),
+ ]
+
+ operations = [
+ migrations.RunPython(
+ prefill_end_date,
+ reverse_code=migrations.RunPython.noop,
+ ),
+ ]