diff --git a/cookbook/admin.py b/cookbook/admin.py index 325955927..9578e70ee 100644 --- a/cookbook/admin.py +++ b/cookbook/admin.py @@ -15,7 +15,7 @@ from .models import (BookmarkletImport, Comment, CookLog, Food, FoodInheritField Recipe, RecipeBook, RecipeBookEntry, RecipeImport, SearchPreference, ShareLink, ShoppingList, ShoppingListEntry, ShoppingListRecipe, Space, Step, Storage, Supermarket, SupermarketCategory, SupermarketCategoryRelation, Sync, SyncLog, - TelegramBot, Unit, UserFile, UserPreference, ViewLog, Automation, UserSpace, UnitConversion, PropertyType, FoodProperty) + TelegramBot, Unit, UserFile, UserPreference, ViewLog, Automation, UserSpace, UnitConversion, PropertyType, FoodProperty, RecipeProperty) class CustomUserAdmin(UserAdmin): @@ -327,11 +327,11 @@ class ShareLinkAdmin(admin.ModelAdmin): admin.site.register(ShareLink, ShareLinkAdmin) -class FoodPropertyTypeAdmin(admin.ModelAdmin): +class PropertyTypeAdmin(admin.ModelAdmin): list_display = ('id', 'name') -admin.site.register(PropertyType, FoodPropertyTypeAdmin) +admin.site.register(PropertyType, PropertyTypeAdmin) class FoodPropertyAdmin(admin.ModelAdmin): @@ -341,6 +341,13 @@ class FoodPropertyAdmin(admin.ModelAdmin): admin.site.register(FoodProperty, FoodPropertyAdmin) +class RecipePropertyAdmin(admin.ModelAdmin): + list_display = ('id', 'property_type', 'property_amount',) + + +admin.site.register(RecipeProperty, RecipePropertyAdmin) + + class NutritionInformationAdmin(admin.ModelAdmin): list_display = ('id',) diff --git a/cookbook/migrations/0190_auto_20230506_2239.py b/cookbook/migrations/0190_auto_20230506_2239.py new file mode 100644 index 000000000..c0b334558 --- /dev/null +++ b/cookbook/migrations/0190_auto_20230506_2239.py @@ -0,0 +1,40 @@ +# Generated by Django 4.1.7 on 2023-05-06 20:39 + +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', 'RecipeProperty') + 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_foodproperty_propertytype_recipeproperty_and_more'), + ] + + operations = [ + migrations.RunPython(migrate_old_nutrition_data) + ]