lots of importer adjustments

This commit is contained in:
vabene1111 2021-06-17 13:38:57 +02:00
parent 018fcf27ea
commit 151f43b0d5
18 changed files with 90 additions and 55 deletions

View File

@ -40,7 +40,7 @@ class Pepperplate(Integration):
recipe = Recipe.objects.create(name=title, description=description, created_by=self.request.user, internal=True, space=self.request.space) recipe = Recipe.objects.create(name=title, description=description, created_by=self.request.user, internal=True, space=self.request.space)
step = Step.objects.create( step = Step.objects.create(
instruction='\n'.join(directions) + '\n\n' instruction='\n'.join(directions) + '\n\n', space=self.request.space,
) )
for ingredient in ingredients: for ingredient in ingredients:
@ -49,7 +49,7 @@ class Pepperplate(Integration):
f = get_food(ingredient, self.request.space) f = get_food(ingredient, self.request.space)
u = get_unit(unit, self.request.space) u = get_unit(unit, self.request.space)
step.ingredients.add(Ingredient.objects.create( step.ingredients.add(Ingredient.objects.create(
food=f, unit=u, amount=amount, note=note food=f, unit=u, amount=amount, note=note, space=self.request.space,
)) ))
recipe.steps.add(step) recipe.steps.add(step)

View File

@ -38,7 +38,7 @@ class ChefTap(Integration):
recipe = Recipe.objects.create(name=title, created_by=self.request.user, internal=True, space=self.request.space, ) recipe = Recipe.objects.create(name=title, created_by=self.request.user, internal=True, space=self.request.space, )
step = Step.objects.create(instruction='\n'.join(directions)) step = Step.objects.create(instruction='\n'.join(directions), space=self.request.space,)
if source_url != '': if source_url != '':
step.instruction += '\n' + source_url step.instruction += '\n' + source_url
@ -50,7 +50,7 @@ class ChefTap(Integration):
f = get_food(ingredient, self.request.space) f = get_food(ingredient, self.request.space)
u = get_unit(unit, self.request.space) u = get_unit(unit, self.request.space)
step.ingredients.add(Ingredient.objects.create( step.ingredients.add(Ingredient.objects.create(
food=f, unit=u, amount=amount, note=note food=f, unit=u, amount=amount, note=note, space=self.request.space,
)) ))
recipe.steps.add(step) recipe.steps.add(step)

View File

@ -55,7 +55,7 @@ class Chowdown(Integration):
recipe.keywords.add(keyword) recipe.keywords.add(keyword)
step = Step.objects.create( step = Step.objects.create(
instruction='\n'.join(directions) + '\n\n' + '\n'.join(descriptions) instruction='\n'.join(directions) + '\n\n' + '\n'.join(descriptions), space=self.request.space,
) )
for ingredient in ingredients: for ingredient in ingredients:
@ -63,7 +63,7 @@ class Chowdown(Integration):
f = get_food(ingredient, self.request.space) f = get_food(ingredient, self.request.space)
u = get_unit(unit, self.request.space) u = get_unit(unit, self.request.space)
step.ingredients.add(Ingredient.objects.create( step.ingredients.add(Ingredient.objects.create(
food=f, unit=u, amount=amount, note=note food=f, unit=u, amount=amount, note=note, space=self.request.space,
)) ))
recipe.steps.add(step) recipe.steps.add(step)

View File

@ -28,7 +28,7 @@ class Domestica(Integration):
recipe.save() recipe.save()
step = Step.objects.create( step = Step.objects.create(
instruction=file['directions'] instruction=file['directions'], space=self.request.space,
) )
if file['source'] != '': if file['source'] != '':
@ -40,7 +40,7 @@ class Domestica(Integration):
f = get_food(ingredient, self.request.space) f = get_food(ingredient, self.request.space)
u = get_unit(unit, self.request.space) u = get_unit(unit, self.request.space)
step.ingredients.add(Ingredient.objects.create( step.ingredients.add(Ingredient.objects.create(
food=f, unit=u, amount=amount, note=note food=f, unit=u, amount=amount, note=note, space=self.request.space,
)) ))
recipe.steps.add(step) recipe.steps.add(step)

View File

@ -109,35 +109,52 @@ class Integration:
for f in files: for f in files:
if 'RecipeKeeper' in f['name']: if 'RecipeKeeper' in f['name']:
import_zip = ZipFile(f['file']) import_zip = ZipFile(f['file'])
file_list = []
for z in import_zip.filelist: for z in import_zip.filelist:
if self.import_file_name_filter(z): if self.import_file_name_filter(z):
data_list = self.split_recipe_file(import_zip.read(z.filename).decode('utf-8')) file_list.append(z)
for d in data_list: il.total_recipes += len(file_list)
recipe = self.get_recipe_from_file(d)
recipe.keywords.add(self.keyword) for z in file_list:
il.msg += f'{recipe.pk} - {recipe.name} \n' data_list = self.split_recipe_file(import_zip.read(z.filename).decode('utf-8'))
self.handle_duplicates(recipe, import_duplicates) for d in data_list:
recipe = self.get_recipe_from_file(d)
recipe.keywords.add(self.keyword)
il.msg += f'{recipe.pk} - {recipe.name} \n'
self.handle_duplicates(recipe, import_duplicates)
il.imported_recipes += 1
il.save()
import_zip.close() import_zip.close()
elif '.zip' in f['name'] or '.paprikarecipes' in f['name']: elif '.zip' in f['name'] or '.paprikarecipes' in f['name']:
import_zip = ZipFile(f['file']) import_zip = ZipFile(f['file'])
file_list = []
for z in import_zip.filelist: for z in import_zip.filelist:
if self.import_file_name_filter(z): if self.import_file_name_filter(z):
try: file_list.append(z)
recipe = self.get_recipe_from_file(BytesIO(import_zip.read(z.filename))) il.total_recipes += len(file_list)
recipe.keywords.add(self.keyword)
il.msg += f'{recipe.pk} - {recipe.name} \n' for z in file_list:
self.handle_duplicates(recipe, import_duplicates) try:
except Exception as e: recipe = self.get_recipe_from_file(BytesIO(import_zip.read(z.filename)))
il.msg += f'-------------------- \n ERROR \n{e}\n--------------------\n' recipe.keywords.add(self.keyword)
il.msg += f'{recipe.pk} - {recipe.name} \n'
self.handle_duplicates(recipe, import_duplicates)
il.imported_recipes += 1
il.save()
except Exception as e:
il.msg += f'-------------------- \n ERROR \n{e}\n--------------------\n'
import_zip.close() import_zip.close()
elif '.json' in f['name'] or '.txt' in f['name']: elif '.json' in f['name'] or '.txt' in f['name']:
data_list = self.split_recipe_file(f['file']) data_list = self.split_recipe_file(f['file'])
il.total_recipes += len(data_list)
for d in data_list: for d in data_list:
try: try:
recipe = self.get_recipe_from_file(d) recipe = self.get_recipe_from_file(d)
recipe.keywords.add(self.keyword) recipe.keywords.add(self.keyword)
il.msg += f'{recipe.pk} - {recipe.name} \n' il.msg += f'{recipe.pk} - {recipe.name} \n'
self.handle_duplicates(recipe, import_duplicates) self.handle_duplicates(recipe, import_duplicates)
il.imported_recipes += 1
il.save()
except Exception as e: except Exception as e:
il.msg += f'-------------------- \n ERROR \n{e}\n--------------------\n' il.msg += f'-------------------- \n ERROR \n{e}\n--------------------\n'
elif '.rtk' in f['name']: elif '.rtk' in f['name']:
@ -145,12 +162,16 @@ class Integration:
for z in import_zip.filelist: for z in import_zip.filelist:
if self.import_file_name_filter(z): if self.import_file_name_filter(z):
data_list = self.split_recipe_file(import_zip.read(z.filename).decode('utf-8')) data_list = self.split_recipe_file(import_zip.read(z.filename).decode('utf-8'))
il.total_recipes += len(data_list)
for d in data_list: for d in data_list:
try: try:
recipe = self.get_recipe_from_file(d) recipe = self.get_recipe_from_file(d)
recipe.keywords.add(self.keyword) recipe.keywords.add(self.keyword)
il.msg += f'{recipe.pk} - {recipe.name} \n' il.msg += f'{recipe.pk} - {recipe.name} \n'
self.handle_duplicates(recipe, import_duplicates) self.handle_duplicates(recipe, import_duplicates)
il.imported_recipes += 1
il.save()
except Exception as e: except Exception as e:
il.msg += f'-------------------- \n ERROR \n{e}\n--------------------\n' il.msg += f'-------------------- \n ERROR \n{e}\n--------------------\n'
import_zip.close() import_zip.close()

View File

@ -12,7 +12,7 @@ from cookbook.models import Recipe, Step, Food, Unit, Ingredient
class Mealie(Integration): class Mealie(Integration):
def import_file_name_filter(self, zip_info_object): def import_file_name_filter(self, zip_info_object):
return re.match(r'^recipes/([A-Za-z\d-])+.json$', zip_info_object.filename) return re.match(r'^recipes/([A-Za-z\d-])+/([A-Za-z\d-])+.json$', zip_info_object.filename)
def get_recipe_from_file(self, file): def get_recipe_from_file(self, file):
recipe_json = json.loads(file.getvalue().decode("utf-8")) recipe_json = json.loads(file.getvalue().decode("utf-8"))
@ -26,9 +26,9 @@ class Mealie(Integration):
# TODO parse times (given in PT2H3M ) # TODO parse times (given in PT2H3M )
ingredients_added = False ingredients_added = False
for s in recipe_json['recipeInstructions']: for s in recipe_json['recipe_instructions']:
step = Step.objects.create( step = Step.objects.create(
instruction=s['text'] instruction=s['text'], space=self.request.space,
) )
if not ingredients_added: if not ingredients_added:
ingredients_added = True ingredients_added = True
@ -36,21 +36,31 @@ class Mealie(Integration):
if len(recipe_json['description'].strip()) > 500: if len(recipe_json['description'].strip()) > 500:
step.instruction = recipe_json['description'].strip() + '\n\n' + step.instruction step.instruction = recipe_json['description'].strip() + '\n\n' + step.instruction
for ingredient in recipe_json['recipeIngredient']: for ingredient in recipe_json['recipe_ingredient']:
amount, unit, ingredient, note = parse(ingredient) try:
f = get_food(ingredient, self.request.space) if ingredient['food']:
u = get_unit(unit, self.request.space) f = get_food(ingredient['food'], self.request.space)
step.ingredients.add(Ingredient.objects.create( u = get_unit(ingredient['unit'], self.request.space)
food=f, unit=u, amount=amount, note=note amount = ingredient['quantity']
)) note = ingredient['note']
else:
amount, unit, ingredient, note = parse(ingredient['note'])
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, space=self.request.space,
))
except:
pass
recipe.steps.add(step) recipe.steps.add(step)
for f in self.files: for f in self.files:
if '.zip' in f['name']: if '.zip' in f['name']:
import_zip = ZipFile(f['file']) import_zip = ZipFile(f['file'])
for z in import_zip.filelist: try:
if re.match(f'^images/{recipe_json["slug"]}.jpg$', z.filename): self.import_recipe_image(recipe, BytesIO(import_zip.read(f'recipes/{recipe_json["slug"]}/images/min-original.webp')), filetype=get_filetype(f'recipes/{recipe_json["slug"]}/images/original'))
self.import_recipe_image(recipe, BytesIO(import_zip.read(z.filename)), filetype=get_filetype(z.filename)) except:
pass
return recipe return recipe

View File

@ -44,7 +44,7 @@ class MealMaster(Integration):
recipe.keywords.add(keyword) recipe.keywords.add(keyword)
step = Step.objects.create( step = Step.objects.create(
instruction='\n'.join(directions) + '\n\n' instruction='\n'.join(directions) + '\n\n', space=self.request.space,
) )
for ingredient in ingredients: for ingredient in ingredients:
@ -53,7 +53,7 @@ class MealMaster(Integration):
f = get_food(ingredient, self.request.space) f = get_food(ingredient, self.request.space)
u = get_unit(unit, self.request.space) u = get_unit(unit, self.request.space)
step.ingredients.add(Ingredient.objects.create( step.ingredients.add(Ingredient.objects.create(
food=f, unit=u, amount=amount, note=note food=f, unit=u, amount=amount, note=note, space=self.request.space,
)) ))
recipe.steps.add(step) recipe.steps.add(step)

View File

@ -30,7 +30,7 @@ class NextcloudCookbook(Integration):
ingredients_added = False ingredients_added = False
for s in recipe_json['recipeInstructions']: for s in recipe_json['recipeInstructions']:
step = Step.objects.create( step = Step.objects.create(
instruction=s instruction=s, space=self.request.space,
) )
if not ingredients_added: if not ingredients_added:
if len(recipe_json['description'].strip()) > 500: if len(recipe_json['description'].strip()) > 500:
@ -43,7 +43,7 @@ class NextcloudCookbook(Integration):
f = get_food(ingredient, self.request.space) f = get_food(ingredient, self.request.space)
u = get_unit(unit, self.request.space) u = get_unit(unit, self.request.space)
step.ingredients.add(Ingredient.objects.create( step.ingredients.add(Ingredient.objects.create(
food=f, unit=u, amount=amount, note=note food=f, unit=u, amount=amount, note=note, space=self.request.space,
)) ))
recipe.steps.add(step) recipe.steps.add(step)

View File

@ -24,13 +24,13 @@ class OpenEats(Integration):
if file["source"] != '': if file["source"] != '':
instructions += file["source"] instructions += file["source"]
step = Step.objects.create(instruction=instructions) step = Step.objects.create(instruction=instructions, space=self.request.space,)
for ingredient in file['ingredients']: for ingredient in file['ingredients']:
f = get_food(ingredient['food'], self.request.space) f = get_food(ingredient['food'], self.request.space)
u = get_unit(ingredient['unit'], self.request.space) u = get_unit(ingredient['unit'], self.request.space)
step.ingredients.add(Ingredient.objects.create( step.ingredients.add(Ingredient.objects.create(
food=f, unit=u, amount=ingredient['amount'] food=f, unit=u, amount=ingredient['amount'], space=self.request.space,
)) ))
recipe.steps.add(step) recipe.steps.add(step)

View File

@ -55,7 +55,7 @@ class Paprika(Integration):
pass pass
step = Step.objects.create( step = Step.objects.create(
instruction=instructions instruction=instructions, space=self.request.space,
) )
if len(recipe_json['description'].strip()) > 500: if len(recipe_json['description'].strip()) > 500:
@ -73,7 +73,7 @@ class Paprika(Integration):
f = get_food(ingredient, self.request.space) f = get_food(ingredient, self.request.space)
u = get_unit(unit, self.request.space) u = get_unit(unit, self.request.space)
step.ingredients.add(Ingredient.objects.create( step.ingredients.add(Ingredient.objects.create(
food=f, unit=u, amount=amount, note=note food=f, unit=u, amount=amount, note=note, space=self.request.space,
)) ))
except AttributeError: except AttributeError:
pass pass

View File

@ -45,7 +45,7 @@ class RecetteTek(Integration):
if not instructions: if not instructions:
instructions = '' instructions = ''
step = Step.objects.create(instruction=instructions) step = Step.objects.create(instruction=instructions, space=self.request.space,)
# Append the original import url to the step (if it exists) # Append the original import url to the step (if it exists)
try: try:
@ -63,7 +63,7 @@ class RecetteTek(Integration):
f = get_food(ingredient, self.request.space) f = get_food(ingredient, self.request.space)
u = get_unit(unit, self.request.space) u = get_unit(unit, self.request.space)
step.ingredients.add(Ingredient.objects.create( step.ingredients.add(Ingredient.objects.create(
food=f, unit=u, amount=amount, note=note food=f, unit=u, amount=amount, note=note, space=self.request.space,
)) ))
except Exception as e: except Exception as e:
print(recipe.name, ': failed to parse recipe ingredients ', str(e)) print(recipe.name, ': failed to parse recipe ingredients ', str(e))

View File

@ -41,7 +41,7 @@ class RecipeKeeper(Integration):
except AttributeError: except AttributeError:
pass pass
step = Step.objects.create(instruction='') step = Step.objects.create(instruction='', space=self.request.space,)
for ingredient in file.find("div", {"itemprop": "recipeIngredients"}).findChildren("p"): for ingredient in file.find("div", {"itemprop": "recipeIngredients"}).findChildren("p"):
if ingredient.text == "": if ingredient.text == "":
@ -50,7 +50,7 @@ class RecipeKeeper(Integration):
f = get_food(ingredient, self.request.space) f = get_food(ingredient, self.request.space)
u = get_unit(unit, self.request.space) u = get_unit(unit, self.request.space)
step.ingredients.add(Ingredient.objects.create( step.ingredients.add(Ingredient.objects.create(
food=f, unit=u, amount=amount, note=note food=f, unit=u, amount=amount, note=note, space=self.request.space,
)) ))
for s in file.find("div", {"itemprop": "recipeDirections"}).find_all("p"): for s in file.find("div", {"itemprop": "recipeDirections"}).find_all("p"):

View File

@ -36,7 +36,7 @@ class RecipeSage(Integration):
ingredients_added = False ingredients_added = False
for s in file['recipeInstructions']: for s in file['recipeInstructions']:
step = Step.objects.create( step = Step.objects.create(
instruction=s['text'] instruction=s['text'], space=self.request.space,
) )
if not ingredients_added: if not ingredients_added:
ingredients_added = True ingredients_added = True
@ -46,7 +46,7 @@ class RecipeSage(Integration):
f = get_food(ingredient, self.request.space) f = get_food(ingredient, self.request.space)
u = get_unit(unit, self.request.space) u = get_unit(unit, self.request.space)
step.ingredients.add(Ingredient.objects.create( step.ingredients.add(Ingredient.objects.create(
food=f, unit=u, amount=amount, note=note food=f, unit=u, amount=amount, note=note, space=self.request.space,
)) ))
recipe.steps.add(step) recipe.steps.add(step)

View File

@ -43,7 +43,7 @@ class RezKonv(Integration):
recipe.keywords.add(keyword) recipe.keywords.add(keyword)
step = Step.objects.create( step = Step.objects.create(
instruction='\n'.join(directions) + '\n\n' instruction='\n'.join(directions) + '\n\n', space=self.request.space,
) )
for ingredient in ingredients: for ingredient in ingredients:
@ -52,7 +52,7 @@ class RezKonv(Integration):
f = get_food(ingredient, self.request.space) f = get_food(ingredient, self.request.space)
u = get_unit(unit, self.request.space) u = get_unit(unit, self.request.space)
step.ingredients.add(Ingredient.objects.create( step.ingredients.add(Ingredient.objects.create(
food=f, unit=u, amount=amount, note=note food=f, unit=u, amount=amount, note=note, space=self.request.space,
)) ))
recipe.steps.add(step) recipe.steps.add(step)

View File

@ -43,14 +43,14 @@ class Safron(Integration):
recipe = Recipe.objects.create(name=title, description=description, created_by=self.request.user, internal=True, space=self.request.space, ) 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)) step = Step.objects.create(instruction='\n'.join(directions), space=self.request.space,)
for ingredient in ingredients: for ingredient in ingredients:
amount, unit, ingredient, note = parse(ingredient) amount, unit, ingredient, note = parse(ingredient)
f = get_food(ingredient, self.request.space) f = get_food(ingredient, self.request.space)
u = get_unit(unit, self.request.space) u = get_unit(unit, self.request.space)
step.ingredients.add(Ingredient.objects.create( step.ingredients.add(Ingredient.objects.create(
food=f, unit=u, amount=amount, note=note food=f, unit=u, amount=amount, note=note, space=self.request.space,
)) ))
recipe.steps.add(step) recipe.steps.add(step)

View File

@ -665,6 +665,10 @@ class ImportLog(models.Model, PermissionModelMixin):
running = models.BooleanField(default=True) running = models.BooleanField(default=True)
msg = models.TextField(default="") msg = models.TextField(default="")
keyword = models.ForeignKey(Keyword, null=True, blank=True, on_delete=models.SET_NULL) keyword = models.ForeignKey(Keyword, null=True, blank=True, on_delete=models.SET_NULL)
total_recipes = models.IntegerField(default=0)
imported_recipes = models.IntegerField(default=0)
created_at = models.DateTimeField(auto_now_add=True) created_at = models.DateTimeField(auto_now_add=True)
created_by = models.ForeignKey(User, on_delete=models.CASCADE) created_by = models.ForeignKey(User, on_delete=models.CASCADE)

View File

@ -533,7 +533,7 @@ class ImportLogSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = ImportLog model = ImportLog
fields = ('id', 'type', 'msg', 'running', 'keyword', 'created_by', 'created_at') fields = ('id', 'type', 'msg', 'running', 'keyword', 'total_recipes', 'imported_recipes', 'created_by', 'created_at')
read_only_fields = ('created_by',) read_only_fields = ('created_by',)

View File

@ -143,7 +143,7 @@ def import_url(request):
) )
step = Step.objects.create( step = Step.objects.create(
instruction=data['recipeInstructions'], instruction=data['recipeInstructions'], space=request.space,
) )
recipe.steps.add(step) recipe.steps.add(step)
@ -156,7 +156,7 @@ def import_url(request):
recipe.keywords.add(k) recipe.keywords.add(k)
for ing in data['recipeIngredient']: for ing in data['recipeIngredient']:
ingredient = Ingredient() ingredient = Ingredient(space=request.space,)
if ing['ingredient']['text'] != '': if ing['ingredient']['text'] != '':
ingredient.food, f_created = Food.objects.get_or_create( ingredient.food, f_created = Food.objects.get_or_create(