diff --git a/cookbook/forms.py b/cookbook/forms.py index 626f2d12..1c1a90c0 100644 --- a/cookbook/forms.py +++ b/cookbook/forms.py @@ -154,6 +154,7 @@ class ImportExportBase(forms.Form): COOKBOOKAPP = 'COOKBOOKAPP' COPYMETHAT = 'COPYMETHAT' COOKMATE = 'COOKMATE' + REZEPTSUITEDE = 'REZEPTSUITEDE' PDF = 'PDF' type = forms.ChoiceField(choices=( @@ -162,7 +163,7 @@ class ImportExportBase(forms.Form): (PEPPERPLATE, 'Pepperplate'), (RECETTETEK, 'RecetteTek'), (RECIPESAGE, 'Recipe Sage'), (DOMESTICA, 'Domestica'), (MEALMASTER, 'MealMaster'), (REZKONV, 'RezKonv'), (OPENEATS, 'Openeats'), (RECIPEKEEPER, 'Recipe Keeper'), (PLANTOEAT, 'Plantoeat'), (COOKBOOKAPP, 'CookBookApp'), (COPYMETHAT, 'CopyMeThat'), (PDF, 'PDF'), (MELARECIPES, 'Melarecipes'), - (COOKMATE, 'Cookmate') + (COOKMATE, 'Cookmate'), (REZEPTSUITEDE, 'Recipesuite.de') )) diff --git a/cookbook/integration/integration.py b/cookbook/integration/integration.py index b54e7e56..dbe3d667 100644 --- a/cookbook/integration/integration.py +++ b/cookbook/integration/integration.py @@ -1,16 +1,12 @@ -import time +import traceback import datetime -import json import traceback import uuid -from io import BytesIO, StringIO +from io import BytesIO from zipfile import BadZipFile, ZipFile -import lxml -from django.core.cache import cache -import datetime - from bs4 import Tag +from django.core.cache import cache from django.core.exceptions import ObjectDoesNotExist from django.core.files import File from django.db import IntegrityError @@ -20,8 +16,7 @@ from django.utils.translation import gettext as _ from django_scopes import scope from lxml import etree -from cookbook.forms import ImportExportBase -from cookbook.helper.image_processing import get_filetype, handle_image +from cookbook.helper.image_processing import handle_image from cookbook.models import Keyword, Recipe from recipes.settings import DEBUG from recipes.settings import EXPORT_FILE_CACHE_DURATION @@ -182,7 +177,7 @@ class Integration: 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'] or '.rk' in f['name'] or '.melarecipe' in f['name']: + elif '.json' in f['name'] or '.xml' in f['name'] or '.txt' in f['name'] or '.mmf' in f['name'] or '.rk' in f['name'] or '.melarecipe' in f['name']: data_list = self.split_recipe_file(f['file']) il.total_recipes += len(data_list) for d in data_list: diff --git a/cookbook/integration/rezeptsuitede.py b/cookbook/integration/rezeptsuitede.py new file mode 100644 index 00000000..2c05ccaf --- /dev/null +++ b/cookbook/integration/rezeptsuitede.py @@ -0,0 +1,59 @@ +from xml import etree + +from lxml import etree + +from cookbook.helper.ingredient_parser import IngredientParser +from cookbook.helper.recipe_url_import import parse_time, parse_servings, parse_servings_text +from cookbook.integration.integration import Integration +from cookbook.models import Ingredient, Recipe, Step + + +class Rezeptsuitede(Integration): + + def split_recipe_file(self, file): + xml_file = etree.parse(file).getroot().getchildren() + recipe_list = xml_file.find('recipe') + return recipe_list + + def get_recipe_from_file(self, file): + recipe_xml = file + + recipe = Recipe.objects.create( + name=recipe_xml.find('title').text.strip(), + created_by=self.request.user, internal=True, space=self.request.space) + + if recipe_xml.find('servingtype') is not None and recipe_xml.find('servingtype').text is not None: + recipe.servings = parse_servings(recipe_xml.find('servingtype').text.strip()) + recipe.servings_text = parse_servings_text(recipe_xml.find('servingtype').text.strip()) + + if recipe_xml.find('description') is not None: # description is a list of
  • 's with text + if len(recipe_xml.find('description')) > 0: + recipe.description = recipe_xml.find('description')[0].text[:512] + + for step in recipe_xml.find('step'): + if step.text: + step = Step.objects.create( + instruction=step.text.strip(), space=self.request.space, + ) + recipe.steps.add(step) + + ingredient_parser = IngredientParser(self.request, True) + + if recipe_xml.find('ingredient'): + ingredient_step = recipe.steps.first() + if ingredient_step is None: + ingredient_step = Step.objects.create(space=self.request.space, instruction='') + + for ingredient in recipe_xml.find('ingredient'): + f = ingredient_parser.get_food(ingredient.attrib['item']) + u = ingredient_parser.get_unit(ingredient.attrib['unit']) + ingredient_step.ingredients.add(Ingredient.objects.create( + food=f, unit=u, amount=ingredient.attrib['qty'], original_text=ingredient.text.strip(), space=self.request.space, + )) + + recipe.save() + + return recipe + + def get_file_from_recipe(self, recipe): + raise NotImplementedError('Method not implemented in storage integration') diff --git a/cookbook/views/api.py b/cookbook/views/api.py index e4da5c15..5a78f7b2 100644 --- a/cookbook/views/api.py +++ b/cookbook/views/api.py @@ -1306,6 +1306,8 @@ def import_files(request): return Response({'import_id': il.pk}, status=status.HTTP_200_OK) except NotImplementedError: return Response({'error': True, 'msg': _('Importing is not implemented for this provider')}, status=status.HTTP_400_BAD_REQUEST) + else: + return Response({'error': True, 'msg': form.errors}, status=status.HTTP_400_BAD_REQUEST) def get_recipe_provider(recipe): diff --git a/cookbook/views/import_export.py b/cookbook/views/import_export.py index d4885949..4a205900 100644 --- a/cookbook/views/import_export.py +++ b/cookbook/views/import_export.py @@ -31,6 +31,7 @@ from cookbook.integration.plantoeat import Plantoeat from cookbook.integration.recettetek import RecetteTek from cookbook.integration.recipekeeper import RecipeKeeper from cookbook.integration.recipesage import RecipeSage +from cookbook.integration.rezeptsuitede import Rezeptsuitede from cookbook.integration.rezkonv import RezKonv from cookbook.integration.saffron import Saffron from cookbook.models import ExportLog, ImportLog, Recipe, UserPreference @@ -80,6 +81,8 @@ def get_integration(request, export_type): return MelaRecipes(request, export_type) if export_type == ImportExportBase.COOKMATE: return Cookmate(request, export_type) + if export_type == ImportExportBase.REZEPTSUITEDE: + return Rezeptsuitede(request, export_type) @group_required('user') diff --git a/vue/src/utils/integration.js b/vue/src/utils/integration.js index 5aeb5c17..4fbf10f3 100644 --- a/vue/src/utils/integration.js +++ b/vue/src/utils/integration.js @@ -21,4 +21,5 @@ export const INTEGRATIONS = [ {id: 'RECIPESAGE', name: "Recipesage", import: true, export: true, help_url: 'https://docs.tandoor.dev/features/import_export/#recipesage'}, {id: 'REZKONV', name: "Rezkonv", import: true, export: false, help_url: 'https://docs.tandoor.dev/features/import_export/#rezkonv'}, {id: 'SAFRON', name: "Safron", import: true, export: true, help_url: 'https://docs.tandoor.dev/features/import_export/#safron'}, + {id: 'REZEPTSUITEDE', name: "Rezeptsuite.de", import: true, export: false, help_url: 'https://docs.tandoor.dev/features/import_export/#rezeptsuitede'}, ]