From cc62b088fd40edd8a6068961258fd0d997a0fdc4 Mon Sep 17 00:00:00 2001 From: smilerz Date: Wed, 24 Feb 2021 17:52:40 -0600 Subject: [PATCH 1/4] fix URL import when recipeYield is a list --- cookbook/helper/recipe_url_import.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cookbook/helper/recipe_url_import.py b/cookbook/helper/recipe_url_import.py index 0ff4f1c9f..139b20ab2 100644 --- a/cookbook/helper/recipe_url_import.py +++ b/cookbook/helper/recipe_url_import.py @@ -222,6 +222,8 @@ def find_recipe_json(ld_json, url): if 'recipeYield' in ld_json: if type(ld_json['recipeYield']) == str: ld_json['servings'] = int(re.findall(r'\b\d+\b', ld_json['recipeYield'])[0]) + elif type(ld_json['recipeYield']) == list: + ld_json['servings'] = int(re.findall(r'\b\d+\b', ld_json['recipeYield'][0])[0]) except Exception as e: print(e) ld_json['servings'] = 1 From 3ac22c08ff3fa6b92baa0bcea674b59b2027d768 Mon Sep 17 00:00:00 2001 From: smilerz Date: Wed, 24 Feb 2021 18:52:04 -0600 Subject: [PATCH 2/4] fixed duplicate keyword handling --- cookbook/views/data.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cookbook/views/data.py b/cookbook/views/data.py index 5425e7dcb..e74cfe00e 100644 --- a/cookbook/views/data.py +++ b/cookbook/views/data.py @@ -134,8 +134,7 @@ def import_url(request): recipe.steps.add(step) for kw in data['keywords']: - if kw['id'] != "null" \ - and (k := Keyword.objects.filter(id=kw['id']).first()): + if k := Keyword.objects.filter(name=kw['text']).first(): recipe.keywords.add(k) elif data['all_keywords']: k = Keyword.objects.create(name=kw['text']) From 47090ce863ca4b0cc93a1a97f7dce3b10524af99 Mon Sep 17 00:00:00 2001 From: smilerz Date: Wed, 24 Feb 2021 18:52:43 -0600 Subject: [PATCH 3/4] added tests for add keyword and add duplicate keyword --- cookbook/tests/api/test_api_keyword.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/cookbook/tests/api/test_api_keyword.py b/cookbook/tests/api/test_api_keyword.py index a86f4e426..ffc2bb401 100644 --- a/cookbook/tests/api/test_api_keyword.py +++ b/cookbook/tests/api/test_api_keyword.py @@ -4,7 +4,6 @@ from cookbook.models import Keyword from cookbook.tests.views.test_views import TestViews from django.urls import reverse - class TestApiKeyword(TestViews): def setUp(self): @@ -63,6 +62,26 @@ class TestApiKeyword(TestViews): self.assertEqual(r.status_code, 200) self.assertEqual(response['name'], 'new') + def test_keyword_add(self): + r = self.user_client_1.post( + reverse('api:keyword-list'), + {'name': 'test'}, + content_type='application/json' + ) + response = json.loads(r.content) + self.assertEqual(r.status_code, 201) + self.assertEqual(response['name'], 'test') + + def test_keyword_add_duplicate(self): + r = self.user_client_1.post( + reverse('api:keyword-list'), + {'name': self.keyword_1.name}, + content_type='application/json' + ) + response = json.loads(r.content) + self.assertEqual(r.status_code, 201) + self.assertEqual(response['name'], {self.keyword_1.name}) + def test_keyword_delete(self): r = self.user_client_1.delete( reverse( From 21740522bcc3448a27ac63615e5e4ea082da9a0c Mon Sep 17 00:00:00 2001 From: smilerz Date: Wed, 24 Feb 2021 20:13:06 -0600 Subject: [PATCH 4/4] fixed URL import when ingredient is a string --- cookbook/helper/recipe_url_import.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cookbook/helper/recipe_url_import.py b/cookbook/helper/recipe_url_import.py index 139b20ab2..3a734d846 100644 --- a/cookbook/helper/recipe_url_import.py +++ b/cookbook/helper/recipe_url_import.py @@ -69,8 +69,10 @@ def find_recipe_json(ld_json, url): if 'recipeIngredient' in ld_json: # some pages have comma separated ingredients in a single array entry if (len(ld_json['recipeIngredient']) == 1 - and len(ld_json['recipeIngredient'][0]) > 30): + and type(ld_json['recipeIngredient']) == list): ld_json['recipeIngredient'] = ld_json['recipeIngredient'][0].split(',') # noqa: E501 + elif type(ld_json['recipeIngredient']) == str: + ld_json['recipeIngredient'] = ld_json['recipeIngredient'].split(',') for x in ld_json['recipeIngredient']: if '\n' in x: