ingredients and helpers

This commit is contained in:
vabene1111
2021-02-20 21:26:16 +01:00
parent 96c963795e
commit d7675d4b80
9 changed files with 25 additions and 25 deletions

View File

@ -10,7 +10,7 @@ class BaseAutocomplete(autocomplete.Select2QuerySetView):
if not self.request.user.is_authenticated: if not self.request.user.is_authenticated:
return self.model.objects.none() return self.model.objects.none()
qs = self.model.objects.all() qs = self.model.objects.filter(space=self.request.space).all()
if self.q: if self.q:
qs = qs.filter(name__icontains=self.q) qs = qs.filter(name__icontains=self.q)

View File

@ -12,7 +12,7 @@ from django.utils.dateparse import parse_duration
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
def get_from_html(html_text, url): def get_from_html(html_text, url, space):
soup = BeautifulSoup(html_text, "html.parser") soup = BeautifulSoup(html_text, "html.parser")
# first try finding ld+json as its most common # first try finding ld+json as its most common
@ -31,7 +31,7 @@ def get_from_html(html_text, url):
if ('@type' in ld_json_item if ('@type' in ld_json_item
and ld_json_item['@type'] == 'Recipe'): and ld_json_item['@type'] == 'Recipe'):
return JsonResponse(find_recipe_json(ld_json_item, url)) return JsonResponse(find_recipe_json(ld_json_item, url, space))
except JSONDecodeError: except JSONDecodeError:
return JsonResponse( return JsonResponse(
{ {
@ -45,7 +45,7 @@ def get_from_html(html_text, url):
for i in items: for i in items:
md_json = json.loads(i.json()) md_json = json.loads(i.json())
if 'schema.org/Recipe' in str(md_json['type']): if 'schema.org/Recipe' in str(md_json['type']):
return JsonResponse(find_recipe_json(md_json['properties'], url)) return JsonResponse(find_recipe_json(md_json['properties'], url, space))
return JsonResponse( return JsonResponse(
{ {
@ -55,7 +55,7 @@ def get_from_html(html_text, url):
status=400) status=400)
def find_recipe_json(ld_json, url): def find_recipe_json(ld_json, url, space):
if type(ld_json['name']) == list: if type(ld_json['name']) == list:
try: try:
ld_json['name'] = ld_json['name'][0] ld_json['name'] = ld_json['name'][0]
@ -136,7 +136,7 @@ def find_recipe_json(ld_json, url):
# keywords as list # keywords as list
for kw in ld_json['keywords']: for kw in ld_json['keywords']:
if k := Keyword.objects.filter(name=kw).first(): if k := Keyword.objects.filter(name=kw, space=space).first():
keywords.append({'id': str(k.id), 'text': str(k).strip()}) keywords.append({'id': str(k.id), 'text': str(k).strip()})
else: else:
keywords.append({'id': random.randrange(1111111, 9999999, 1), 'text': kw.strip()}) keywords.append({'id': random.randrange(1111111, 9999999, 1), 'text': kw.strip()})

View File

@ -47,10 +47,10 @@ class Chowdown(Integration):
if description_mode and len(line) > 3 and '---' not in line: if description_mode and len(line) > 3 and '---' not in line:
descriptions.append(line) descriptions.append(line)
recipe = Recipe.objects.create(name=title, created_by=self.request.user, internal=True, ) recipe = Recipe.objects.create(name=title, created_by=self.request.user, internal=True, space=self.request.space)
for k in tags.split(','): for k in tags.split(','):
keyword, created = Keyword.objects.get_or_create(name=k.strip()) keyword, created = Keyword.objects.get_or_create(name=k.strip(), space=self.request.space)
recipe.keywords.add(keyword) recipe.keywords.add(keyword)
step = Step.objects.create( step = Step.objects.create(
@ -59,8 +59,8 @@ class Chowdown(Integration):
for ingredient in ingredients: for ingredient in ingredients:
amount, unit, ingredient, note = parse(ingredient) amount, unit, ingredient, note = parse(ingredient)
f, created = Food.objects.get_or_create(name=ingredient) f, created = Food.objects.get_or_create(name=ingredient, space=self.request.space)
u, created = Unit.objects.get_or_create(name=unit) u, created = Unit.objects.get_or_create(name=unit, space=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
)) ))

View File

@ -40,7 +40,7 @@ class Integration:
export_zip_obj = ZipFile(export_zip_stream, 'w') export_zip_obj = ZipFile(export_zip_stream, 'w')
for r in recipes: for r in recipes:
if r.internal: if r.internal and r.space == self.request.space:
recipe_zip_stream = BytesIO() recipe_zip_stream = BytesIO()
recipe_zip_obj = ZipFile(recipe_zip_stream, 'w') recipe_zip_obj = ZipFile(recipe_zip_stream, 'w')

View File

@ -18,7 +18,7 @@ class Mealie(Integration):
recipe = Recipe.objects.create( recipe = Recipe.objects.create(
name=recipe_json['name'].strip(), description=recipe_json['description'].strip(), name=recipe_json['name'].strip(), description=recipe_json['description'].strip(),
created_by=self.request.user, internal=True) created_by=self.request.user, internal=True, space=self.request.space)
# TODO parse times (given in PT2H3M ) # TODO parse times (given in PT2H3M )
@ -32,8 +32,8 @@ class Mealie(Integration):
for ingredient in recipe_json['recipeIngredient']: for ingredient in recipe_json['recipeIngredient']:
amount, unit, ingredient, note = parse(ingredient) amount, unit, ingredient, note = parse(ingredient)
f, created = Food.objects.get_or_create(name=ingredient) f, created = Food.objects.get_or_create(name=ingredient, space=self.request.space)
u, created = Unit.objects.get_or_create(name=unit) u, created = Unit.objects.get_or_create(name=unit, space=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
)) ))

View File

@ -19,7 +19,7 @@ class NextcloudCookbook(Integration):
recipe = Recipe.objects.create( recipe = Recipe.objects.create(
name=recipe_json['name'].strip(), description=recipe_json['description'].strip(), name=recipe_json['name'].strip(), description=recipe_json['description'].strip(),
created_by=self.request.user, internal=True, created_by=self.request.user, internal=True,
servings=recipe_json['recipeYield']) servings=recipe_json['recipeYield'], space=self.request.space)
# TODO parse times (given in PT2H3M ) # TODO parse times (given in PT2H3M )
# TODO parse keywords # TODO parse keywords
@ -34,8 +34,8 @@ class NextcloudCookbook(Integration):
for ingredient in recipe_json['recipeIngredient']: for ingredient in recipe_json['recipeIngredient']:
amount, unit, ingredient, note = parse(ingredient) amount, unit, ingredient, note = parse(ingredient)
f, created = Food.objects.get_or_create(name=ingredient) f, created = Food.objects.get_or_create(name=ingredient, space=self.request.space)
u, created = Unit.objects.get_or_create(name=unit) u, created = Unit.objects.get_or_create(name=unit, space=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
)) ))

View File

@ -27,15 +27,15 @@ class Paprika(Integration):
for i in items: for i in items:
md_json = json.loads(i.json()) md_json = json.loads(i.json())
if 'schema.org/Recipe' in str(md_json['type']): if 'schema.org/Recipe' in str(md_json['type']):
recipe_json = find_recipe_json(md_json['properties'], '') recipe_json = find_recipe_json(md_json['properties'], '', space=self.request.space)
recipe = Recipe.objects.create(name=recipe_json['name'].strip(), created_by=self.request.user, internal=True) recipe = Recipe.objects.create(name=recipe_json['name'].strip(), created_by=self.request.user, internal=True, space=self.request.space)
step = Step.objects.create( step = Step.objects.create(
instruction=recipe_json['recipeInstructions'] instruction=recipe_json['recipeInstructions']
) )
for ingredient in recipe_json['recipeIngredient']: for ingredient in recipe_json['recipeIngredient']:
f, created = Food.objects.get_or_create(name=ingredient['ingredient']['text']) f, created = Food.objects.get_or_create(name=ingredient['ingredient']['text'], space=self.request.space)
u, created = Unit.objects.get_or_create(name=ingredient['unit']['text']) u, created = Unit.objects.get_or_create(name=ingredient['unit']['text'], space=self.request.space)
step.ingredients.add(Ingredient.objects.create( step.ingredients.add(Ingredient.objects.create(
food=f, unit=u, amount=ingredient['amount'], note=ingredient['note'] food=f, unit=u, amount=ingredient['amount'], note=ingredient['note']
)) ))

View File

@ -41,14 +41,14 @@ class Safron(Integration):
ingredient_mode = False ingredient_mode = False
direction_mode = True direction_mode = True
recipe = Recipe.objects.create(name=title, description=description, created_by=self.request.user, internal=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)) step = Step.objects.create(instruction='\n'.join(directions))
for ingredient in ingredients: for ingredient in ingredients:
amount, unit, ingredient, note = parse(ingredient) amount, unit, ingredient, note = parse(ingredient)
f, created = Food.objects.get_or_create(name=ingredient) f, created = Food.objects.get_or_create(name=ingredient, space=self.request.space)
u, created = Unit.objects.get_or_create(name=unit) u, created = Unit.objects.get_or_create(name=unit, space=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
)) ))

View File

@ -25,7 +25,7 @@ class TestEditsRecipe(TestBase):
for test in test_list: for test in test_list:
with open(test['file'], 'rb') as file: with open(test['file'], 'rb') as file:
print(f'Testing {test["file"]} expecting length {test["result_length"]}') print(f'Testing {test["file"]} expecting length {test["result_length"]}')
parsed_content = json.loads(get_from_html(file.read(), 'test_url').content) parsed_content = json.loads(get_from_html(file.read(), 'test_url', None).content)
self.assertEqual(len(str(parsed_content)), test['result_length']) self.assertEqual(len(str(parsed_content)), test['result_length'])
file.close() file.close()