diff --git a/cookbook/helper/dal.py b/cookbook/helper/dal.py index 6a109a6d7..879279c88 100644 --- a/cookbook/helper/dal.py +++ b/cookbook/helper/dal.py @@ -10,7 +10,7 @@ class BaseAutocomplete(autocomplete.Select2QuerySetView): if not self.request.user.is_authenticated: return self.model.objects.none() - qs = self.model.objects.all() + qs = self.model.objects.filter(space=self.request.space).all() if self.q: qs = qs.filter(name__icontains=self.q) diff --git a/cookbook/helper/recipe_url_import.py b/cookbook/helper/recipe_url_import.py index 0ff4f1c9f..78104ca38 100644 --- a/cookbook/helper/recipe_url_import.py +++ b/cookbook/helper/recipe_url_import.py @@ -12,7 +12,7 @@ from django.utils.dateparse import parse_duration 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") # 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 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: return JsonResponse( { @@ -45,7 +45,7 @@ def get_from_html(html_text, url): for i in items: md_json = json.loads(i.json()) 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( { @@ -55,7 +55,7 @@ def get_from_html(html_text, url): status=400) -def find_recipe_json(ld_json, url): +def find_recipe_json(ld_json, url, space): if type(ld_json['name']) == list: try: ld_json['name'] = ld_json['name'][0] @@ -136,7 +136,7 @@ def find_recipe_json(ld_json, url): # keywords as list 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()}) else: keywords.append({'id': random.randrange(1111111, 9999999, 1), 'text': kw.strip()}) diff --git a/cookbook/integration/chowdown.py b/cookbook/integration/chowdown.py index f7a21b5b5..8ce741895 100644 --- a/cookbook/integration/chowdown.py +++ b/cookbook/integration/chowdown.py @@ -47,10 +47,10 @@ class Chowdown(Integration): if description_mode and len(line) > 3 and '---' not in 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(','): - 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) step = Step.objects.create( @@ -59,8 +59,8 @@ class Chowdown(Integration): for ingredient in ingredients: amount, unit, ingredient, note = parse(ingredient) - f, created = Food.objects.get_or_create(name=ingredient) - u, created = Unit.objects.get_or_create(name=unit) + f, created = Food.objects.get_or_create(name=ingredient, space=self.request.space) + u, created = Unit.objects.get_or_create(name=unit, space=self.request.space) step.ingredients.add(Ingredient.objects.create( food=f, unit=u, amount=amount, note=note )) diff --git a/cookbook/integration/integration.py b/cookbook/integration/integration.py index 89c4af010..49329e41f 100644 --- a/cookbook/integration/integration.py +++ b/cookbook/integration/integration.py @@ -40,7 +40,7 @@ class Integration: export_zip_obj = ZipFile(export_zip_stream, 'w') for r in recipes: - if r.internal: + if r.internal and r.space == self.request.space: recipe_zip_stream = BytesIO() recipe_zip_obj = ZipFile(recipe_zip_stream, 'w') diff --git a/cookbook/integration/mealie.py b/cookbook/integration/mealie.py index 8099f6658..2cce31632 100644 --- a/cookbook/integration/mealie.py +++ b/cookbook/integration/mealie.py @@ -18,7 +18,7 @@ class Mealie(Integration): recipe = Recipe.objects.create( 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 ) @@ -32,8 +32,8 @@ class Mealie(Integration): for ingredient in recipe_json['recipeIngredient']: amount, unit, ingredient, note = parse(ingredient) - f, created = Food.objects.get_or_create(name=ingredient) - u, created = Unit.objects.get_or_create(name=unit) + f, created = Food.objects.get_or_create(name=ingredient, space=self.request.space) + u, created = Unit.objects.get_or_create(name=unit, space=self.request.space) step.ingredients.add(Ingredient.objects.create( food=f, unit=u, amount=amount, note=note )) diff --git a/cookbook/integration/nextcloud_cookbook.py b/cookbook/integration/nextcloud_cookbook.py index 47580e1a8..8d43dc9d5 100644 --- a/cookbook/integration/nextcloud_cookbook.py +++ b/cookbook/integration/nextcloud_cookbook.py @@ -19,7 +19,7 @@ class NextcloudCookbook(Integration): recipe = Recipe.objects.create( name=recipe_json['name'].strip(), description=recipe_json['description'].strip(), 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 keywords @@ -34,8 +34,8 @@ class NextcloudCookbook(Integration): for ingredient in recipe_json['recipeIngredient']: amount, unit, ingredient, note = parse(ingredient) - f, created = Food.objects.get_or_create(name=ingredient) - u, created = Unit.objects.get_or_create(name=unit) + f, created = Food.objects.get_or_create(name=ingredient, space=self.request.space) + u, created = Unit.objects.get_or_create(name=unit, space=self.request.space) step.ingredients.add(Ingredient.objects.create( food=f, unit=u, amount=amount, note=note )) diff --git a/cookbook/integration/paprika.py b/cookbook/integration/paprika.py index 520c90b68..596b0ba2b 100644 --- a/cookbook/integration/paprika.py +++ b/cookbook/integration/paprika.py @@ -27,15 +27,15 @@ class Paprika(Integration): for i in items: md_json = json.loads(i.json()) if 'schema.org/Recipe' in str(md_json['type']): - recipe_json = find_recipe_json(md_json['properties'], '') - recipe = Recipe.objects.create(name=recipe_json['name'].strip(), created_by=self.request.user, internal=True) + 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, space=self.request.space) step = Step.objects.create( instruction=recipe_json['recipeInstructions'] ) for ingredient in recipe_json['recipeIngredient']: - f, created = Food.objects.get_or_create(name=ingredient['ingredient']['text']) - u, created = Unit.objects.get_or_create(name=ingredient['unit']['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'], space=self.request.space) step.ingredients.add(Ingredient.objects.create( food=f, unit=u, amount=ingredient['amount'], note=ingredient['note'] )) diff --git a/cookbook/integration/safron.py b/cookbook/integration/safron.py index 772779505..c2cb90aaa 100644 --- a/cookbook/integration/safron.py +++ b/cookbook/integration/safron.py @@ -41,14 +41,14 @@ class Safron(Integration): ingredient_mode = False 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)) for ingredient in ingredients: amount, unit, ingredient, note = parse(ingredient) - f, created = Food.objects.get_or_create(name=ingredient) - u, created = Unit.objects.get_or_create(name=unit) + f, created = Food.objects.get_or_create(name=ingredient, space=self.request.space) + u, created = Unit.objects.get_or_create(name=unit, space=self.request.space) step.ingredients.add(Ingredient.objects.create( food=f, unit=u, amount=amount, note=note )) diff --git a/cookbook/tests/other/test_edits_recipe.py b/cookbook/tests/other/test_edits_recipe.py index 7394854c3..19d73e50b 100644 --- a/cookbook/tests/other/test_edits_recipe.py +++ b/cookbook/tests/other/test_edits_recipe.py @@ -25,7 +25,7 @@ class TestEditsRecipe(TestBase): for test in test_list: with open(test['file'], 'rb') as file: 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']) file.close()