added import for pepperplate

This commit is contained in:
vabene1111
2021-03-28 18:22:30 +02:00
parent 6143e31e1a
commit 738f0781b2
6 changed files with 82 additions and 6 deletions

View File

@@ -111,11 +111,14 @@ class ImportExportBase(forms.Form):
CHOWDOWN = 'CHOWDOWN'
SAFRON = 'SAFRON'
CHEFTAP = 'CHEFTAP'
PEPPERPLATE = 'PEPPERPLATE'
type = forms.ChoiceField(choices=(
(DEFAULT, _('Default')), (PAPRIKA, 'Paprika'), (NEXTCLOUD, 'Nextcloud Cookbook'),
(MEALIE, 'Mealie'), (CHOWDOWN, 'Chowdown'), (SAFRON, 'Safron'), (CHEFTAP, 'ChefTap'),
(PEPPERPLATE, 'Pepperplate'),
))
duplicates = forms.BooleanField(help_text=_('To prevent duplicates recipes with the same name as existing ones are ignored. Check this box to import everything.'))
class ImportForm(ImportExportBase):

View File

@@ -0,0 +1,58 @@
import json
import re
from io import BytesIO
from zipfile import ZipFile
from cookbook.helper.ingredient_parser import parse, get_food, get_unit
from cookbook.integration.integration import Integration
from cookbook.models import Recipe, Step, Food, Unit, Ingredient, Keyword
class Pepperplate(Integration):
def get_recipe_from_file(self, file):
ingredient_mode = False
direction_mode = False
ingredients = []
directions = []
for fl in file.readlines():
line = fl.decode("utf-8")
if 'Title:' in line:
title = line.replace('Title:', '').replace('"', '').strip()
if 'Description:' in line:
description = line.replace('Description:', '').strip()
if 'Original URL:' in line or 'Source:' in line or 'Yield:' in line or 'Total:' in line:
if len(line.strip().split(':')[1]) > 0:
directions.append(line.strip() + '\n')
if ingredient_mode:
if len(line) > 2 and 'Instructions:' not in line:
ingredients.append(line.strip())
if direction_mode:
if len(line) > 2:
directions.append(line.strip() + '\n')
if 'Ingredients:' in line:
ingredient_mode = True
if 'Instructions:' in line:
ingredient_mode = False
direction_mode = True
recipe = Recipe.objects.create(name=title, description=description, created_by=self.request.user, internal=True, space=self.request.space)
step = Step.objects.create(
instruction='\n'.join(directions) + '\n\n'
)
for ingredient in ingredients:
amount, unit, ingredient, note = parse(ingredient)
f = get_food(ingredient, self.request.space)
u = get_unit(unit, self.request.space)
step.ingredients.add(Ingredient.objects.create(
food=f, unit=u, amount=amount, note=note
))
recipe.steps.add(step)
return recipe
def get_file_from_recipe(self, recipe):
raise NotImplementedError('Method not implemented in storage integration')

View File

@@ -77,9 +77,10 @@ class Integration:
"""
return True
def do_import(self, files, il):
def do_import(self, files, il, import_duplicates):
"""
Imports given files
:param import_duplicates: if true duplicates are imported as well
:param files: List of in memory files
:param il: Import Log object to refresh while running
:return: HttpResponseRedirect to the recipe search showing all imported recipes
@@ -99,15 +100,17 @@ class Integration:
recipe = self.get_recipe_from_file(BytesIO(import_zip.read(z.filename)))
recipe.keywords.add(self.keyword)
il.msg += f'{recipe.pk} - {recipe.name} \n'
if duplicate := self.is_duplicate(recipe):
ignored_recipes.append(duplicate)
if not import_duplicates:
if duplicate := self.is_duplicate(recipe):
ignored_recipes.append(duplicate)
import_zip.close()
else:
recipe = self.get_recipe_from_file(f['file'])
recipe.keywords.add(self.keyword)
il.msg += f'{recipe.pk} - {recipe.name} \n'
if duplicate := self.is_duplicate(recipe):
ignored_recipes.append(duplicate)
if not import_duplicates:
if duplicate := self.is_duplicate(recipe):
ignored_recipes.append(duplicate)
except BadZipFile:
il.msg += 'ERROR ' + _('Importer expected a .zip file. Did you choose the correct importer type for your data ?') + '\n'

View File

@@ -10,6 +10,7 @@ from django.utils.translation import gettext as _
from cookbook.forms import ExportForm, ImportForm, ImportExportBase
from cookbook.helper.permission_helper import group_required
from cookbook.integration.Pepperplate import Pepperplate
from cookbook.integration.cheftap import ChefTap
from cookbook.integration.chowdown import Chowdown
from cookbook.integration.default import Default
@@ -35,6 +36,8 @@ def get_integration(request, export_type):
return Safron(request, export_type)
if export_type == ImportExportBase.CHEFTAP:
return ChefTap(request, export_type)
if export_type == ImportExportBase.PEPPERPLATE:
return Pepperplate(request, export_type)
@group_required('user')
@@ -49,7 +52,7 @@ def import_recipe(request):
files = []
for f in request.FILES.getlist('files'):
files.append({'file': BytesIO(f.read()), 'name': f.name})
t = threading.Thread(target=integration.do_import, args=[files, il])
t = threading.Thread(target=integration.do_import, args=[files, il, form['duplicates']])
t.setDaemon(True)
t.start()