Self hosted FLOSS fitness/workout, nutrition and weight tracker
Merge branch 'feature/health-sync' into pr-2421
Roland Geidercd002561a101
diff --cc wger/measurements/migrations/0007_official_categories.pyindex 000000000,000000000..902be8212new file mode 100644--- /dev/null+++ b/wger/measurements/migrations/0007_official_categories.py@@@ -1,0 -1,0 +1,81 @@@++from django.db import migrations, models++++BATCH_SIZE = 2000++++++def migrate_weight_to_measurements(apps, schema_editor):++ """++ For every existing WeightEntry, ensure the owning user has an official++ 'body_weight' Category and create a matching Measurement row.++++ The WeightEntry uuid is carried over as the Measurement id so that++ clients that already synchronise weight entries by uuid keep a stable++ identity. The category unit follows the user's preferred weight unit,++ since WeightEntry values are stored in that unit.++ """++ WeightEntry = apps.get_model('weight', 'WeightEntry')++ Category = apps.get_model('measurements', 'Category')++ Measurement = apps.get_model('measurements', 'Measurement')++ UserProfile = apps.get_model('core', 'UserProfile')++++ unit_by_user = dict(UserProfile.objects.values_list('user_id', 'weight_unit'))++++ category_by_user = {}++ user_ids = WeightEntry.objects.values_list('user_id', flat=True).distinct()++ for user_id in user_ids.iterator():++ category, _ = Category.objects.get_or_create(++ user_id=user_id,++ metric_type='body_weight',++ is_official=True,++ defaults={'name': 'Body weight', 'unit': unit_by_user.get(user_id, 'kg')},++ )++ category_by_user[user_id] = category++++ batch = []++ for entry in WeightEntry.objects.all().iterator():++ batch.append(++ Measurement(++ id=entry.uuid,++ category=category_by_user[entry.user_id],++ date=entry.date,++ value=entry.weight,++ source='user',++ )++ )++ if len(batch) >= BATCH_SIZE:++ Measurement.objects.bulk_create(batch)++ batch = []++ if batch:++ Measurement.objects.bulk_create(batch)++++++def noop_reverse(apps, schema_editor):++ pass++++++class Migration(migrations.Migration):++ dependencies = [++ (++ 'measurements',++ '0006_category_externally_synced_category_metric_type_and_more',++ ),++ ('weight', '0005_add_uuid'),++ ('core', '0002_auto_20141225_1512'),++ ]++++ operations = [++ migrations.AddField(++ model_name='category',++ name='is_official',++ field=models.BooleanField(default=False, verbose_name='Official category'),++ ),++ migrations.AddConstraint(++ model_name='category',++ constraint=models.UniqueConstraint(++ condition=models.Q(('is_official', True)),++ fields=('user', 'metric_type'),++ name='unique_official_category_per_metric_type',++ ),++ ),++ migrations.RunPython(migrate_weight_to_measurements, noop_reverse),++ ]diff --cc wger/measurements/models/category.pyindex d2046a875,e6aa88dab..02497535a--- a/wger/measurements/models/category.py+++ b/wger/measurements/models/category.py@@@ -41,17 -41,9 +41,18 @@@ class MetricType(models.TextChoices)class Category(models.Model):class Meta:ordering = [+ 'order','-name',]+ constraints = [+ # official category per (user, metric_type) where is_official=True+ # user-created categories are unaffected+ models.UniqueConstraint(+ fields=['user', 'metric_type'],+ condition=models.Q(is_official=True),+ name='unique_official_category_per_metric_type',+ )+ ]id = models.UUIDField(default=uuid7,@@@ -81,16 -73,24 +82,29 @@@default=MetricType.CUSTOM,)- externally_synced = models.BooleanField(- verbose_name='Externally synced',- default=False,+ # Multi-value measurements (e.g. blood pressure) are modelled as a parent+ # category holding one child category per component. Only leaf categories+ # (no children) carry measurements; nesting is limited to one level.+ parent = models.ForeignKey(+ 'self',+ verbose_name='Parent',+ on_delete=models.CASCADE,+ related_name='children',+ blank=True,+ null=True,+ )++ # Position in the category list; for children, the position within the group+ order = models.IntegerField(+ verbose_name='Order',+ default=0,)+ is_official = models.BooleanField(+ verbose_name='Official category',+ default=False,+ )+def get_owner_object(self):"""Returns the object that has owner information