From 7053a857c21f2adb2c61d566fade6936f6ed0c75 Mon Sep 17 00:00:00 2001 From: smilerz Date: Mon, 6 Sep 2021 10:20:43 -0500 Subject: [PATCH] reworked MergeMixin to handle non-trees --- cookbook/models.py | 3 +- cookbook/serializer.py | 28 +++- cookbook/templates/base.html | 29 +++- .../templates/forms/edit_internal_recipe.html | 2 +- cookbook/templates/shopping_list.html | 6 +- cookbook/tests/api/test_api_food.py | 3 +- cookbook/tests/api/test_api_unit.py | 129 ++++++++++++++++-- cookbook/urls.py | 4 +- cookbook/views/api.py | 21 +-- cookbook/views/lists.py | 17 +++ vue/src/components/GenericSplitLists.vue | 2 +- vue/src/locales/en.json | 3 +- vue/src/utils/models.js | 26 +++- 13 files changed, 230 insertions(+), 43 deletions(-) diff --git a/cookbook/models.py b/cookbook/models.py index e03c7461..0a239c87 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -104,8 +104,7 @@ class TreeModel(MP_Node): class Meta: abstract = True - - + class PermissionModelMixin: @staticmethod diff --git a/cookbook/serializer.py b/cookbook/serializer.py index 7e21e300..8bada6ca 100644 --- a/cookbook/serializer.py +++ b/cookbook/serializer.py @@ -118,8 +118,10 @@ class UserFileSerializer(serializers.ModelSerializer): except TypeError: current_file_size_mb = 0 - if (validated_data['file'].size / 1000 / 1000 + current_file_size_mb - 5) > self.context[ - 'request'].space.max_file_storage_mb != 0: + if ( + (validated_data['file'].size / 1000 / 1000 + current_file_size_mb - 5) + > self.context['request'].space.max_file_storage_mb != 0 + ): raise ValidationError(_('You have reached your file upload limit.')) def create(self, validated_data): @@ -241,10 +243,24 @@ class KeywordSerializer(UniqueFieldsMixin, serializers.ModelSerializer): class UnitSerializer(UniqueFieldsMixin, serializers.ModelSerializer): + image = serializers.SerializerMethodField('get_image') + numrecipe = serializers.SerializerMethodField('count_recipes') + + def get_image(self, obj): + recipes = Recipe.objects.filter(steps__ingredients__unit=obj, space=obj.space).exclude(image__isnull=True).exclude(image__exact='') + + if len(recipes) != 0: + return random.choice(recipes).image.url + else: + return None + + def count_recipes(self, obj): + return Recipe.objects.filter(steps__ingredients__unit=obj, space=obj.space).count() def create(self, validated_data): - obj, created = Unit.objects.get_or_create(name=validated_data['name'].strip(), - space=self.context['request'].space) + validated_data['name'] = validated_data['name'].strip() + validated_data['space'] = self.context['request'].space + obj, created = Unit.objects.get_or_create(**validated_data) return obj def update(self, instance, validated_data): @@ -253,8 +269,8 @@ class UnitSerializer(UniqueFieldsMixin, serializers.ModelSerializer): class Meta: model = Unit - fields = ('id', 'name', 'description') - read_only_fields = ('id',) + fields = ('id', 'name', 'description', 'numrecipe', 'image') + read_only_fields = ('id', 'numrecipe', 'image') class SupermarketCategorySerializer(UniqueFieldsMixin, WritableNestedModelSerializer): diff --git a/cookbook/templates/base.html b/cookbook/templates/base.html index 45886ae7..97859839 100644 --- a/cookbook/templates/base.html +++ b/cookbook/templates/base.html @@ -97,6 +97,9 @@ {% trans 'Ingredients' %} + {% trans 'Units' %} + {% trans 'Supermarket' %} @@ -114,6 +117,28 @@ class="fas fa-edit fa-fw"> {% trans 'Batch Edit' %} + {% comment %} should food and units be moved to the keyword dropdown and rename that? + feels like the original intent of the organization of these menus has shifted maybe something like this?{% endcomment %} +