Self hosted FLOSS fitness/workout, nutrition and weight tracker
Add a UUID field to the ingredient units
Roland Geidere2da6164d578
diff --git a/wger/nutrition/migrations/0031_merge_weight_unit_into_ingredient_weight_unit.py b/wger/nutrition/migrations/0031_merge_weight_unit_into_ingredient_weight_unit.pyindex cf5b17bd2..3ee2ee63d 100644--- a/wger/nutrition/migrations/0031_merge_weight_unit_into_ingredient_weight_unit.py+++ b/wger/nutrition/migrations/0031_merge_weight_unit_into_ingredient_weight_unit.py@@ -1,15 +1,34 @@from django.db import migrations, models-from django.db.migrations.state import StateApps+from wger.utils.uuid import uuid7-def copy_weight_unit_names(apps: StateApps, schema_editor):- """Copy the name from WeightUnit into IngredientWeightUnit.name"""+def migrate_weight_units(apps, schema_editor):+ """+ Copy unit names from WeightUnit into IngredientWeightUnit.name,+ then delete entries that are not referenced by any MealItem or LogItem.+ """IngredientWeightUnit = apps.get_model('nutrition', 'IngredientWeightUnit')+ MealItem = apps.get_model('nutrition', 'MealItem')+ LogItem = apps.get_model('nutrition', 'LogItem')++ # Copy names from the related WeightUnitfor iwu in IngredientWeightUnit.objects.select_related('unit').filter(unit__isnull=False):iwu.name = iwu.unit.nameiwu.save(update_fields=['name'])+ # Delete entries not referenced by any MealItem or LogItem.+ # These units will become orphaned in local wger instances since we won't be able to update+ # as they will have a local UUID. However, the current units in the system are from the USDA+ # entries which can't be updated anymore and are dwarfed by the 3 million OFF entries, so+ # this step is acceptable.+ referenced_ids = set(+ MealItem.objects.filter(weight_unit__isnull=False).values_list('weight_unit_id', flat=True)+ ) | set(+ LogItem.objects.filter(weight_unit__isnull=False).values_list('weight_unit_id', flat=True)+ )+ IngredientWeightUnit.objects.exclude(id__in=referenced_ids).delete()+class Migration(migrations.Migration):dependencies = [@@ -23,7 +42,13 @@ class Migration(migrations.Migration):field=models.CharField(default='Serving', max_length=200, verbose_name='Name'),preserve_default=False,),- migrations.RunPython(copy_weight_unit_names, migrations.RunPython.noop),+ # Copy names and delete unreferenced entries+ migrations.RunPython(migrate_weight_units, migrations.RunPython.noop),+ migrations.AddField(+ model_name='ingredientweightunit',+ name='uuid',+ field=models.UUIDField(default=uuid7, editable=False, unique=True),+ ),migrations.RemoveField(model_name='ingredientweightunit',name='unit',diff --git a/wger/nutrition/models/ingredient_weight_unit.py b/wger/nutrition/models/ingredient_weight_unit.pyindex 1e9019d76..32ca4a698 100644--- a/wger/nutrition/models/ingredient_weight_unit.py+++ b/wger/nutrition/models/ingredient_weight_unit.py@@ -20,6 +20,9 @@ import logging# Djangofrom django.db import models+# wger+from wger.utils.uuid import uuid7+# Localfrom .ingredient import Ingredient@@ -32,6 +35,12 @@ class IngredientWeightUnit(models.Model):A specific human usable weight unit for an ingredient"""+ uuid = models.UUIDField(+ default=uuid7,+ unique=True,+ editable=False,+ )+ingredient = models.ForeignKey(Ingredient,verbose_name='Ingredient',diff --git a/wger/tasks.py b/wger/tasks.pyindex 106328927..f8b3afd7e 100644--- a/wger/tasks.py+++ b/wger/tasks.py@@ -168,7 +168,7 @@ def load_online_fixtures(context, settings_path: str = None):setup_django_environment(settings_path)# Prepare the download- for name in ('ingredients', 'weight_units', 'ingredient_units'):+ for name in ('ingredients',):url = f'{FIXTURE_URL}{name}.json.zip'print(f'Downloading fixture data from {url}...')