Skip to content

Commit

Permalink
Merge branch 'develop' into feature/vue3
Browse files Browse the repository at this point in the history
# Conflicts:
#	cookbook/views/api.py
  • Loading branch information
vabene1111 committed Nov 27, 2024
2 parents 425f008 + 793c152 commit 2fab51e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
19 changes: 12 additions & 7 deletions cookbook/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1697,17 +1697,22 @@ 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)
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):
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='
Expand All @@ -1724,8 +1729,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):
Expand Down Expand Up @@ -1764,6 +1768,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)

Expand Down
22 changes: 22 additions & 0 deletions vue/src/apps/ImportView/ImportView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@
<loading-spinner></loading-spinner>
</b-card>

<!-- Warnings -->
<b-card no-body v-if="duplicateWarning" class="warning">
{{ duplicateWarning }}
</b-card>

<!-- OPTIONS -->
<b-card no-body v-if="recipe_json !== undefined">
<b-card-header header-tag="header" class="p-1" role="tab">
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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'];
Expand Down Expand Up @@ -763,6 +775,16 @@ export default {

<style>
.warning {
color: rgb(255, 149, 0);
align-items: center;
background-color: #fff4ec;
padding: 10px;
border: 1px solid rgb(255, 149, 0);
border-radius: 5px;
margin: 10px 0;
}
.bounce {
animation: bounce 0.82s cubic-bezier(0.36, 0.07, 0.19, 0.97) both;
transform: translate3d(0, 0, 0);
Expand Down

0 comments on commit 2fab51e

Please sign in to comment.