diff --git a/cookbook/admin.py b/cookbook/admin.py index 4569404c5..76af757a6 100644 --- a/cookbook/admin.py +++ b/cookbook/admin.py @@ -5,7 +5,7 @@ from .models import (Comment, CookLog, Food, Ingredient, InviteLink, Keyword, RecipeBook, RecipeBookEntry, RecipeImport, ShareLink, ShoppingList, ShoppingListEntry, ShoppingListRecipe, Space, Step, Storage, Sync, SyncLog, Unit, UserPreference, - ViewLog, Supermarket, SupermarketCategory) + ViewLog, Supermarket, SupermarketCategory, SupermarketCategoryRelation) class SpaceAdmin(admin.ModelAdmin): @@ -42,7 +42,16 @@ class SyncAdmin(admin.ModelAdmin): admin.site.register(Sync, SyncAdmin) -admin.site.register(Supermarket) + +class SupermarketCategoryInline(admin.TabularInline): + model = SupermarketCategoryRelation + + +class SupermarketAdmin(admin.ModelAdmin): + inlines = (SupermarketCategoryInline,) + + +admin.site.register(Supermarket, SupermarketAdmin) admin.site.register(SupermarketCategory) diff --git a/cookbook/migrations/0104_auto_20210125_2133.py b/cookbook/migrations/0104_auto_20210125_2133.py new file mode 100644 index 000000000..e837f5d86 --- /dev/null +++ b/cookbook/migrations/0104_auto_20210125_2133.py @@ -0,0 +1,28 @@ +# Generated by Django 3.1.5 on 2021-01-25 20:33 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('cookbook', '0103_food_ignore_shopping'), + ] + + operations = [ + migrations.CreateModel( + name='SupermarketCategoryRelation', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('order', models.IntegerField(default=0)), + ('category', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='cookbook.supermarketcategory')), + ('supermarket', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='cookbook.supermarket')), + ], + ), + migrations.AddField( + model_name='supermarket', + name='categories', + field=models.ManyToManyField(through='cookbook.SupermarketCategoryRelation', to='cookbook.SupermarketCategory'), + ), + ] diff --git a/cookbook/models.py b/cookbook/models.py index 0363a74ba..7f63326d2 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -144,14 +144,6 @@ class Sync(models.Model): return self.path -class Supermarket(models.Model): - name = models.CharField(unique=True, max_length=128, validators=[MinLengthValidator(1)]) - description = models.TextField(blank=True, null=True) - - def __str__(self): - return self.name - - class SupermarketCategory(models.Model): name = models.CharField(unique=True, max_length=128, validators=[MinLengthValidator(1)]) description = models.TextField(blank=True, null=True) @@ -160,6 +152,21 @@ class SupermarketCategory(models.Model): return self.name +class Supermarket(models.Model): + name = models.CharField(unique=True, max_length=128, validators=[MinLengthValidator(1)]) + description = models.TextField(blank=True, null=True) + categories = models.ManyToManyField(SupermarketCategory, through='SupermarketCategoryRelation') + + def __str__(self): + return self.name + + +class SupermarketCategoryRelation(models.Model): + supermarket = models.ForeignKey(Supermarket, on_delete=models.CASCADE) + category = models.ForeignKey(SupermarketCategory, on_delete=models.CASCADE) + order = models.IntegerField(default=0) + + class SyncLog(models.Model): sync = models.ForeignKey(Sync, on_delete=models.CASCADE) status = models.CharField(max_length=32) diff --git a/cookbook/serializer.py b/cookbook/serializer.py index e6af4e00f..aeac42476 100644 --- a/cookbook/serializer.py +++ b/cookbook/serializer.py @@ -11,7 +11,7 @@ from cookbook.models import (Comment, CookLog, Food, Ingredient, Keyword, RecipeBook, RecipeBookEntry, RecipeImport, ShareLink, ShoppingList, ShoppingListEntry, ShoppingListRecipe, Step, Storage, Sync, SyncLog, - Unit, UserPreference, ViewLog, SupermarketCategory) + Unit, UserPreference, ViewLog, SupermarketCategory, Supermarket, SupermarketCategoryRelation) from cookbook.templatetags.custom_tags import markdown @@ -140,6 +140,20 @@ class UnitSerializer(UniqueFieldsMixin, serializers.ModelSerializer): read_only_fields = ('id',) +class SupermarketCategoryRelationSerializer(serializers.ModelSerializer): + class Meta: + model = SupermarketCategoryRelation + fields = "__all__" + + +class SupermarketSerializer(UniqueFieldsMixin, serializers.ModelSerializer): + categories = SupermarketCategoryRelationSerializer(many=True, read_only=True) + + class Meta: + model = Supermarket + fields = ('id', 'name', 'categories') + + class SupermarketCategorySerializer(UniqueFieldsMixin, WritableNestedModelSerializer): def create(self, validated_data): diff --git a/cookbook/templates/shopping_list.html b/cookbook/templates/shopping_list.html index e0d672d1e..d866992a9 100644 --- a/cookbook/templates/shopping_list.html +++ b/cookbook/templates/shopping_list.html @@ -201,6 +201,28 @@ +
+
+{# #} +{# #} + +
+
+