From 9df75f551cada56257aaffeb3797680e3ae4ece0 Mon Sep 17 00:00:00 2001 From: Lavanya Mehndiratta Date: Mon, 25 Nov 2024 21:48:06 -0500 Subject: [PATCH] Changed functionality to only display a warning message --- cookbook/views/api.py | 13 +++++++++++-- vue/src/apps/ImportView/ImportView.vue | 22 ++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/cookbook/views/api.py b/cookbook/views/api.py index 003e7b9bd2..ab6ef27e37 100644 --- a/cookbook/views/api.py +++ b/cookbook/views/api.py @@ -1455,13 +1455,21 @@ def post(self, request, *args, **kwargs): url = serializer.validated_data.get('url', None) data = unquote(serializer.validated_data.get('data', None)) + + duplicate = False + if url: + # Check for existing recipes with provided url + existing_recipe = Recipe.objects.filter(source_url=url).first() + if existing_recipe: + duplicate = True + if not url and not data: return Response({'error': True, 'msg': _('Nothing to do.')}, status=status.HTTP_400_BAD_REQUEST) elif url and not data: if re.match('^(https?://)?(www\\.youtube\\.com|youtu\\.be)/.+$', url): if validate_import_url(url): - return Response({'recipe_json': get_from_youtube_scraper(url, request), 'recipe_images': [], }, status=status.HTTP_200_OK) + return Response({'recipe_json': get_from_youtube_scraper(url, request), 'recipe_images': [], 'duplicate': duplicate}, status=status.HTTP_200_OK) if re.match('^(.)*/view/recipe/[0-9]+/[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$', url): recipe_json = requests.get( url.replace('/view/recipe/', '/api/recipe/').replace(re.split('/view/recipe/[0-9]+', url)[1], '') + '?share=' @@ -1476,7 +1484,7 @@ def post(self, request, *args, **kwargs): filetype=pathlib.Path(recipe_json['image']).suffix), name=f'{uuid.uuid4()}_{recipe.pk}{pathlib.Path(recipe_json["image"]).suffix}') recipe.save() - return Response({'link': request.build_absolute_uri(reverse('view_recipe', args={recipe.pk}))}, status=status.HTTP_201_CREATED) + return Response({'link': request.build_absolute_uri(reverse('view_recipe', args={recipe.pk})), 'duplicate': duplicate}, status=status.HTTP_201_CREATED) else: try: if validate_import_url(url): @@ -1511,6 +1519,7 @@ def post(self, request, *args, **kwargs): return Response({ 'recipe_json': helper.get_from_scraper(scrape, request), 'recipe_images': list(dict.fromkeys(get_images_from_soup(scrape.soup, url))), + 'duplicate': duplicate }, status=status.HTTP_200_OK) diff --git a/vue/src/apps/ImportView/ImportView.vue b/vue/src/apps/ImportView/ImportView.vue index b1743944ce..fbd5a0402b 100644 --- a/vue/src/apps/ImportView/ImportView.vue +++ b/vue/src/apps/ImportView/ImportView.vue @@ -83,6 +83,11 @@ + + + {{ duplicateWarning }} + + @@ -463,6 +468,7 @@ export default { }, // URL import LS_IMPORT_RECENT: 'import_recent_urls', //TODO use central helper to manage all local storage keys (and maybe even access) + duplicateWarning: '', website_url: '', website_url_list: '', import_multiple: false, @@ -643,6 +649,12 @@ export default { return } + if ('duplicate' in response.data && response.data['duplicate']) { + this.duplicateWarning = "A recipe with this URL already exists."; + } else { + this.duplicateWarning = ""; + } + this.loading = false this.recipe_json = response.data['recipe_json']; @@ -763,6 +775,16 @@ export default {