enable cross tandoor importing

This commit is contained in:
vabene1111
2023-02-16 17:32:43 +01:00
parent 50429207c5
commit 5dad6b8b17
3 changed files with 43 additions and 6 deletions

View File

@ -424,3 +424,18 @@ def get_images_from_soup(soup, url):
if 'http' in u: if 'http' in u:
images.append(u) images.append(u)
return images return images
def clean_dict(input_dict, key):
if type(input_dict) == dict:
for x in list(input_dict):
if x == key:
del input_dict[x]
elif type(input_dict[x]) == dict:
input_dict[x] = clean_dict(input_dict[x], key)
elif type(input_dict[x]) == list:
temp_list = []
for e in input_dict[x]:
temp_list.append(clean_dict(e, key))
return input_dict

View File

@ -1,6 +1,7 @@
import io import io
import json import json
import mimetypes import mimetypes
import pathlib
import re import re
import threading import threading
import traceback import traceback
@ -56,7 +57,7 @@ from cookbook.helper.permission_helper import (CustomIsAdmin, CustomIsOwner,
CustomIsSpaceOwner, CustomIsUser, group_required, CustomIsSpaceOwner, CustomIsUser, group_required,
is_space_owner, switch_user_active_space, above_space_limit, CustomRecipePermission, CustomUserPermission, CustomTokenHasReadWriteScope, CustomTokenHasScope, has_group_permission) is_space_owner, switch_user_active_space, above_space_limit, CustomRecipePermission, CustomUserPermission, CustomTokenHasReadWriteScope, CustomTokenHasScope, has_group_permission)
from cookbook.helper.recipe_search import RecipeFacet, RecipeSearch from cookbook.helper.recipe_search import RecipeFacet, RecipeSearch
from cookbook.helper.recipe_url_import import get_from_youtube_scraper, get_images_from_soup from cookbook.helper.recipe_url_import import get_from_youtube_scraper, get_images_from_soup, clean_dict
from cookbook.helper.scrapers.scrapers import text_scraper from cookbook.helper.scrapers.scrapers import text_scraper
from cookbook.helper.shopping_helper import RecipeShoppingEditor, shopping_helper from cookbook.helper.shopping_helper import RecipeShoppingEditor, shopping_helper
from cookbook.models import (Automation, BookmarkletImport, CookLog, CustomFilter, ExportLog, Food, from cookbook.models import (Automation, BookmarkletImport, CookLog, CustomFilter, ExportLog, Food,
@ -87,7 +88,7 @@ from cookbook.serializer import (AutomationSerializer, BookmarkletImportListSeri
SupermarketCategorySerializer, SupermarketSerializer, SupermarketCategorySerializer, SupermarketSerializer,
SyncLogSerializer, SyncSerializer, UnitSerializer, SyncLogSerializer, SyncSerializer, UnitSerializer,
UserFileSerializer, UserSerializer, UserPreferenceSerializer, UserFileSerializer, UserSerializer, UserPreferenceSerializer,
UserSpaceSerializer, ViewLogSerializer, AccessTokenSerializer, FoodSimpleSerializer) UserSpaceSerializer, ViewLogSerializer, AccessTokenSerializer, FoodSimpleSerializer, RecipeExportSerializer)
from cookbook.views.import_export import get_integration from cookbook.views.import_export import get_integration
from recipes import settings from recipes import settings
@ -1174,6 +1175,18 @@ def recipe_from_source(request):
# 'recipe_html': '', # 'recipe_html': '',
'recipe_images': [], 'recipe_images': [],
}, status=status.HTTP_200_OK) }, 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=' + re.split('/view/recipe/[0-9]+', url)[1].replace('/', '')).json()
recipe_json = clean_dict(recipe_json, 'id')
serialized_recipe = RecipeExportSerializer(data=recipe_json, context={'request': request})
if serialized_recipe.is_valid():
recipe = serialized_recipe.save()
recipe.image = File(handle_image(request, File(io.BytesIO(requests.get(recipe_json['image']).content), name='image'), 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)
else: else:
try: try:
if validators.url(url, public=True): if validators.url(url, public=True):

View File

@ -24,8 +24,11 @@
<div class="row justify-content-center"> <div class="row justify-content-center">
<div class="col-12 justify-content-cente"> <div class="col-12 justify-content-cente">
<b-checkbox v-model="import_multiple" switch><span <b-checkbox v-model="import_multiple" switch><span
v-if="import_multiple"><i class="far fa-copy fa-fw"></i> {{ $t('Multiple') }}</span><span v-if="import_multiple"><i
v-if="!import_multiple"><i class="far fa-file fa-fw"></i> {{ $t('Single') }}</span></b-checkbox> class="far fa-copy fa-fw"></i> {{ $t('Multiple') }}</span><span
v-if="!import_multiple"><i
class="far fa-file fa-fw"></i> {{ $t('Single') }}</span>
</b-checkbox>
</div> </div>
</div> </div>
<b-input-group class="mt-2" :class="{ bounce: empty_input }" <b-input-group class="mt-2" :class="{ bounce: empty_input }"
@ -242,13 +245,15 @@
<b-spinner variant="primary"></b-spinner> <b-spinner variant="primary"></b-spinner>
</div> </div>
<b-button-group> <b-button-group>
<b-button @click="importRecipe('view')" v-if="!import_multiple" :disabled="import_loading">Import & <b-button @click="importRecipe('view')" v-if="!import_multiple"
:disabled="import_loading">Import &
View View
</b-button> <!-- TODO localize --> </b-button> <!-- TODO localize -->
<b-button @click="importRecipe('edit')" variant="success" <b-button @click="importRecipe('edit')" variant="success"
v-if="!import_multiple" :disabled="import_loading">Import & Edit v-if="!import_multiple" :disabled="import_loading">Import & Edit
</b-button> </b-button>
<b-button @click="importRecipe('import')" v-if="!import_multiple" :disabled="import_loading">Import & <b-button @click="importRecipe('import')" v-if="!import_multiple"
:disabled="import_loading">Import &
Restart Restart
</b-button> </b-button>
<b-button @click="location.reload()" :disabled="import_loading">Restart <b-button @click="location.reload()" :disabled="import_loading">Restart
@ -622,6 +627,10 @@ export default {
} }
return axios.post(resolveDjangoUrl('api_recipe_from_source'), payload,).then((response) => { return axios.post(resolveDjangoUrl('api_recipe_from_source'), payload,).then((response) => {
if (response.status === 201 && 'link' in response.data) {
window.location = response.data.link
}
this.loading = false this.loading = false
this.recipe_json = response.data['recipe_json']; this.recipe_json = response.data['recipe_json'];