From 78e2ee6631f7d80bebb07faca18479ae4b68793c Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Mon, 9 Jun 2025 11:05:54 +0200 Subject: [PATCH] fixed external recipes not sharable --- cookbook/templates/pdf_viewer.html | 11 +++++++--- cookbook/urls.py | 4 ++-- cookbook/views/api.py | 10 ++++----- cookbook/views/views.py | 11 +++++++--- .../display/ExternalRecipeViewer.vue | 21 +++++++++++++++++-- 5 files changed, 42 insertions(+), 15 deletions(-) diff --git a/cookbook/templates/pdf_viewer.html b/cookbook/templates/pdf_viewer.html index d81bef81a..da694834a 100644 --- a/cookbook/templates/pdf_viewer.html +++ b/cookbook/templates/pdf_viewer.html @@ -5,7 +5,8 @@ PDF - PDF + + PDF - + - +{% if share %} + +{% else %} + +{% endif %} \ No newline at end of file diff --git a/cookbook/urls.py b/cookbook/urls.py index 5aff8f42c..38980761b 100644 --- a/cookbook/urls.py +++ b/cookbook/urls.py @@ -114,8 +114,8 @@ urlpatterns = [ path('api/import/', api.AppImportView.as_view(), name='view_import'), path('api/export/', api.AppExportView.as_view(), name='api_export'), path('data/import/url', data.import_url, name='data_import_url'), - path('api/get_external_file_link//', api.get_external_file_link, name='api_get_external_file_link'), - path('api/get_recipe_file//', api.get_recipe_file, name='api_get_recipe_file'), + path('api/get_external_file_link//', api.get_external_file_link, name='api_get_external_file_link'), + path('api/get_recipe_file//', api.get_recipe_file, name='api_get_recipe_file'), path('api/sync_all/', api.sync_all, name='api_sync'), path('api/recipe-from-source/', api.RecipeUrlImportView.as_view(), name='api_recipe_from_source'), path('api/ai-import/', api.AiImportView.as_view(), name='api_ai_import'), diff --git a/cookbook/views/api.py b/cookbook/views/api.py index be2d47153..cf71c311f 100644 --- a/cookbook/views/api.py +++ b/cookbook/views/api.py @@ -2283,8 +2283,8 @@ def get_recipe_provider(recipe): ) @api_view(['GET']) @permission_classes([CustomIsUser & CustomTokenHasReadWriteScope]) -def get_external_file_link(request, recipe_id): - recipe = get_object_or_404(Recipe, pk=recipe_id, space=request.space) +def get_external_file_link(request, pk): + recipe = get_object_or_404(Recipe, pk=pk, space=request.space) if not recipe.link: recipe.link = get_recipe_provider(recipe).get_share_link(recipe) recipe.save() @@ -2297,9 +2297,9 @@ def get_external_file_link(request, recipe_id): responses=None, ) @api_view(['GET']) -@permission_classes([(CustomIsGuest | CustomIsUser) & CustomTokenHasReadWriteScope]) -def get_recipe_file(request, recipe_id): - recipe = get_object_or_404(Recipe, pk=recipe_id, space=request.space) +@permission_classes([CustomRecipePermission & CustomTokenHasReadWriteScope]) +def get_recipe_file(request, pk): + recipe = get_object_or_404(Recipe, pk=pk) # space check handled by CustomRecipePermission if recipe.storage: return FileResponse(get_recipe_provider(recipe).get_file(recipe), filename=f'{recipe.name}.pdf') else: diff --git a/cookbook/views/views.py b/cookbook/views/views.py index 218d6e2ad..91b16993b 100644 --- a/cookbook/views/views.py +++ b/cookbook/views/views.py @@ -24,6 +24,7 @@ from django.utils.datetime_safe import date from django.utils.translation import gettext as _ from django_scopes import scopes_disabled from drf_spectacular.views import SpectacularRedocView, SpectacularSwaggerView +from rest_framework.response import Response from cookbook.forms import CommentForm, Recipe, SearchPreferenceForm, SpaceCreateForm, SpaceJoinForm, User, UserCreateForm, UserPreference from cookbook.helper.HelperFunctions import str2bool @@ -197,10 +198,14 @@ def meal_plan(request): return render(request, 'meal_plan.html', {}) -@group_required('guest') def recipe_pdf_viewer(request, pk): - recipe = get_object_or_404(Recipe, pk=pk, space=request.space) - return render(request, 'pdf_viewer.html', {'recipe_id': pk}) + with scopes_disabled(): + recipe = get_object_or_404(Recipe, pk=pk) + if share_link_valid(recipe, request.GET.get('share', None)) or (has_group_permission( + request.user, ['guest']) and recipe.space == request.space): + + return render(request, 'pdf_viewer.html', {'recipe_id': pk, 'share': request.GET.get('share', None)}) + return HttpResponseRedirect(reverse('index')) @group_required('guest') diff --git a/vue3/src/components/display/ExternalRecipeViewer.vue b/vue3/src/components/display/ExternalRecipeViewer.vue index d2f366afc..f68c609d0 100644 --- a/vue3/src/components/display/ExternalRecipeViewer.vue +++ b/vue3/src/components/display/ExternalRecipeViewer.vue @@ -1,8 +1,8 @@ @@ -10,12 +10,14 @@ import {computed, PropType} from "vue"; import {Recipe} from "@/openapi"; import {useDjangoUrls} from "@/composables/useDjangoUrls"; +import {useUrlSearchParams} from "@vueuse/core"; const props = defineProps({ recipe: {type: {} as PropType, required: true} }) +const params = useUrlSearchParams('history') const {getDjangoUrl} = useDjangoUrls() /** @@ -40,6 +42,21 @@ const isImage = computed(() => { return false }) +const externalUrl = computed(() => { + let url = '' + if (isImage.value) { + url = `${getDjangoUrl('/api/get_recipe_file/')}${props.recipe.id}/` + } else if (isPdf.value) { + url = `${getDjangoUrl('/view-recipe-pdf/')}${props.recipe.id}/` + } + + if (params.share && typeof params.share == "string") { + url += `?share=${params.share}` + } + + return url +}) +