Skip to content

Commit

Permalink
paprika improvements and importer fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vabene1111 committed Apr 5, 2021
1 parent a8434ce commit 01a53ad
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 39 deletions.
13 changes: 7 additions & 6 deletions cookbook/integration/Pepperplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ def get_recipe_from_file(self, file):
)

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
))
if len(ingredient.strip()) > 0:
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
Expand Down
13 changes: 7 additions & 6 deletions cookbook/integration/cheftap.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ def get_recipe_from_file(self, file):
step.save()

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
))
if len(ingredient.strip()) > 0:
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
Expand Down
13 changes: 7 additions & 6 deletions cookbook/integration/domestica.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ def get_recipe_from_file(self, file):
step.instruction += '\n' + file['source']

for ingredient in file['ingredients'].split('\n'):
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
))
if len(ingredient.strip()) > 0:
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)

if file['image'] != '':
Expand Down
13 changes: 7 additions & 6 deletions cookbook/integration/mealmaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ def get_recipe_from_file(self, file):
)

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
))
if len(ingredient.strip()) > 0:
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
Expand Down
50 changes: 42 additions & 8 deletions cookbook/integration/paprika.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import base64
import gzip
import json
import re
from io import BytesIO

from cookbook.helper.ingredient_parser import parse, get_food, get_unit
from cookbook.integration.integration import Integration
from cookbook.models import Recipe, Step, Ingredient
from cookbook.models import Recipe, Step, Ingredient, Keyword
from gettext import gettext as _


class Paprika(Integration):
Expand All @@ -21,17 +23,49 @@ def get_recipe_from_file(self, file):
name=recipe_json['name'].strip(), description=recipe_json['description'].strip(),
created_by=self.request.user, internal=True, space=self.request.space)

try:
if re.match(r'([0-9])+\s(.)*', recipe_json['servings'] ):
s = recipe_json['servings'].split(' ')
recipe.servings = s[0]
recipe.servings_text = s[1]

if len(recipe_json['cook_time'].strip()) > 0:
recipe.waiting_time = re.findall(r'\d+', recipe_json['cook_time'])[0]

if len(recipe_json['prep_time'].strip()) > 0:
recipe.working_time = re.findall(r'\d+', recipe_json['prep_time'])[0]
except Exception:
pass

recipe.save()

instructions = recipe_json['directions']
if len(recipe_json['notes'].strip()) > 0:
instructions += '\n\n### ' + _('Notes') + ' \n' + recipe_json['notes']

if len(recipe_json['nutritional_info'].strip()) > 0:
instructions += '\n\n### ' + _('Nutritional Information') + ' \n' + recipe_json['nutritional_info']

if len(recipe_json['source'].strip()) > 0 or len(recipe_json['source_url'].strip()) > 0:
instructions += '\n\n### ' + _('Source') + ' \n' + recipe_json['source'].strip() + ' \n' + recipe_json['source_url'].strip()

step = Step.objects.create(
instruction=recipe_json['directions'] + '\n\n' + recipe_json['nutritional_info']
instruction=instructions
)

if 'categories' in recipe_json:
for c in recipe_json['categories']:
keyword, created = Keyword.objects.get_or_create(name=c.strip(), space=self.request.space)
recipe.keywords.add(keyword)

for ingredient in recipe_json['ingredients'].split('\n'):
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
))
if len(ingredient.strip()) > 0:
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)

Expand Down
13 changes: 7 additions & 6 deletions cookbook/integration/rezkonv.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ def get_recipe_from_file(self, file):
)

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
))
if len(ingredient.strip()) > 0:
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
Expand Down
2 changes: 1 addition & 1 deletion cookbook/views/import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,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, form['duplicates']])
t = threading.Thread(target=integration.do_import, args=[files, il, form.cleaned_data['duplicates']])
t.setDaemon(True)
t.start()

Expand Down

0 comments on commit 01a53ad

Please sign in to comment.