Skip to content

Commit 22f1a3e

Browse files
committed
Add new migration to prefill the end dates for nutritional plans
This just sets each user's nutrition plan to the start date of their next one and should just make the nutritional plan stats ( wger-project/flutter/pull/855 ) get a bit less crowded since not all plans since the beginning of time should show up in the comparison view.
1 parent 432b927 commit 22f1a3e

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Generated by Django 5.2.6 on 2025-09-16 20:54
2+
3+
from django.db import migrations
4+
5+
6+
def prefill_end_date(apps, schema_editor):
7+
"""
8+
Sets the end date of each user's nutrition plan to the start date of their
9+
next one. The last one is left unchanged.
10+
"""
11+
NutritionPlan = apps.get_model('nutrition', 'NutritionPlan')
12+
user_ids = NutritionPlan.objects.order_by().values_list('user_id', flat=True).distinct()
13+
14+
for user_id in user_ids:
15+
plans = list(
16+
NutritionPlan.objects.filter(user_id=user_id).order_by('start', 'id')
17+
)
18+
for i in range(len(plans) - 1):
19+
current = plans[i]
20+
next_plan = plans[i + 1]
21+
NutritionPlan.objects.filter(pk=current.pk).update(end=next_plan.start)
22+
23+
24+
class Migration(migrations.Migration):
25+
dependencies = [
26+
('nutrition', '0026_add_start_and_end_fields'),
27+
]
28+
29+
operations = [
30+
migrations.RunPython(
31+
prefill_end_date,
32+
reverse_code=migrations.RunPython.noop,
33+
),
34+
]

0 commit comments

Comments
 (0)