# Generated by Django 4.1.9 on 2023-05-25 13:06 from django.db import migrations from django_scopes import scopes_disabled from gettext import gettext as _ def migrate_old_nutrition_data(apps, schema_editor): print('Transforming nutrition information, this might take a while on large databases') with scopes_disabled(): PropertyType = apps.get_model('cookbook', 'PropertyType') RecipeProperty = apps.get_model('cookbook', 'Property') Recipe = apps.get_model('cookbook', 'Recipe') Space = apps.get_model('cookbook', 'Space') # TODO respect space for s in Space.objects.all(): property_fat = PropertyType.objects.get_or_create(name=_('Fat'), unit=_('g'), space=s, )[0] property_carbohydrates = PropertyType.objects.get_or_create(name=_('Carbohydrates'), unit=_('g'), space=s, )[0] property_proteins = PropertyType.objects.get_or_create(name=_('Proteins'), unit=_('g'), space=s, )[0] property_calories = PropertyType.objects.get_or_create(name=_('Calories'), unit=_('kcal'), space=s, )[0] for r in Recipe.objects.filter(nutrition__isnull=False, space=s).all(): rp_fat = RecipeProperty.objects.create(property_type=property_fat, property_amount=r.nutrition.fats, space=s) rp_carbohydrates = RecipeProperty.objects.create(property_type=property_carbohydrates, property_amount=r.nutrition.carbohydrates, space=s) rp_proteins = RecipeProperty.objects.create(property_type=property_proteins, property_amount=r.nutrition.proteins, space=s) rp_calories = RecipeProperty.objects.create(property_type=property_calories, property_amount=r.nutrition.calories, space=s) r.properties.add(rp_fat, rp_carbohydrates, rp_proteins, rp_calories) r.nutrition = None r.save() class Migration(migrations.Migration): dependencies = [ ('cookbook', '0189_property_propertytype_unitconversion_food_fdc_id_and_more'), ] operations = [ migrations.RunPython(migrate_old_nutrition_data) ]