From 26f9d25bd2f23bff097ef8d734f033d61dea1019 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Tue, 14 Sep 2021 20:31:41 +0200 Subject: [PATCH] fixed some integration issues --- cookbook/integration/integration.py | 1 + cookbook/integration/paprika.py | 2 +- cookbook/serializer.py | 18 +++++------------- cookbook/views/import_export.py | 2 +- 4 files changed, 8 insertions(+), 15 deletions(-) diff --git a/cookbook/integration/integration.py b/cookbook/integration/integration.py index d5ab65750..9e118ec8e 100644 --- a/cookbook/integration/integration.py +++ b/cookbook/integration/integration.py @@ -158,6 +158,7 @@ class Integration: il.imported_recipes += 1 il.save() except Exception as e: + traceback.print_exc() self.handle_exception(e, log=il, message=f'-------------------- \nERROR \n{e}\n--------------------\n') import_zip.close() elif '.json' in f['name'] or '.txt' in f['name'] or '.mmf' in f['name']: diff --git a/cookbook/integration/paprika.py b/cookbook/integration/paprika.py index b006bdd67..92d4bbe0c 100644 --- a/cookbook/integration/paprika.py +++ b/cookbook/integration/paprika.py @@ -58,7 +58,7 @@ class Paprika(Integration): instruction=instructions, space=self.request.space, ) - if len(recipe_json['description'].strip()) > 500: + if 'description' in recipe_json and len(recipe_json['description'].strip()) > 500: step.instruction = recipe_json['description'].strip() + '\n\n' + step.instruction if 'categories' in recipe_json: diff --git a/cookbook/serializer.py b/cookbook/serializer.py index 72cb90692..6fbd7b3c2 100644 --- a/cookbook/serializer.py +++ b/cookbook/serializer.py @@ -341,22 +341,14 @@ class FoodSerializer(UniqueFieldsMixin, WritableNestedModelSerializer): def count_recipes(self, obj): return Recipe.objects.filter(steps__ingredients__food=obj, space=obj.space).count() - # def to_representation(self, instance): - # response = super().to_representation(instance) - # # turns a GET of food.recipe into a dict of data while allowing a PATCH/PUT of an integer to update a food with a recipe - # recipe = RecipeSimpleSerializer(instance.recipe, allow_null=True).data - # supermarket_category = SupermarketCategorySerializer(instance.supermarket_category, allow_null=True).data - # response['recipe'] = recipe if recipe else None - # # the SupermarketCategorySerializer returns a dict instead of None when the column is null - # if supermarket_category == {'name': ''} or None: - # response['supermarket_category'] = None - # else: - # response['supermarket_category'] = supermarket_category - # return response - def create(self, validated_data): validated_data['name'] = validated_data['name'].strip() validated_data['space'] = self.context['request'].space + # supermarket category needs to be handled manually as food.get or create does not create nested serializers unlike a super.create of serializer + if 'supermarket_category' in validated_data and validated_data['supermarket_category']: + validated_data['supermarket_category'], sc_created = SupermarketCategory.objects.get_or_create( + name=validated_data.pop('supermarket_category')['name'], + space=self.context['request'].space) obj, created = Food.objects.get_or_create(**validated_data) return obj diff --git a/cookbook/views/import_export.py b/cookbook/views/import_export.py index 6dac02d92..48e131c46 100644 --- a/cookbook/views/import_export.py +++ b/cookbook/views/import_export.py @@ -73,7 +73,7 @@ def import_recipe(request): if request.method == "POST": form = ImportForm(request.POST, request.FILES) - if form.is_valid(): + if form.is_valid() and request.FILES != {}: try: integration = get_integration(request, form.cleaned_data['type'])