fixed some integration issues

This commit is contained in:
vabene1111 2021-09-14 20:31:41 +02:00
parent 3b429c5d84
commit 26f9d25bd2
4 changed files with 8 additions and 15 deletions

View File

@ -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']:

View File

@ -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:

View File

@ -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

View File

@ -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'])