mirror of
https://github.com/TandoorRecipes/recipes.git
synced 2026-01-06 22:58:19 -05:00
import with image working
This commit is contained in:
@@ -58,18 +58,6 @@ def get_recipe_from_source(text, url, request):
|
||||
})
|
||||
return kid_list
|
||||
|
||||
recipe_json = {
|
||||
'name': '',
|
||||
'url': '',
|
||||
'description': '',
|
||||
'image': '',
|
||||
'keywords': [],
|
||||
'recipeIngredient': [],
|
||||
'recipeInstructions': '',
|
||||
'servings': '',
|
||||
'prepTime': '',
|
||||
'cookTime': ''
|
||||
}
|
||||
recipe_tree = []
|
||||
parse_list = []
|
||||
html_data = []
|
||||
|
||||
@@ -131,9 +131,7 @@ def get_from_scraper(scrape, request):
|
||||
recipe_json['steps'][0]['ingredients'].append(
|
||||
{
|
||||
'amount': 0,
|
||||
'unit': {
|
||||
'name': '',
|
||||
},
|
||||
'unit': None,
|
||||
'food': {
|
||||
'name': x,
|
||||
},
|
||||
@@ -275,9 +273,9 @@ def parse_keywords(keyword_json, space):
|
||||
kw = normalize_string(kw)
|
||||
if len(kw) != 0:
|
||||
if k := Keyword.objects.filter(name=kw, space=space).first():
|
||||
keywords.append({'name': str(k)})
|
||||
keywords.append({'label': str(k), 'name': k.name, 'id': k.id})
|
||||
else:
|
||||
keywords.append({'name': kw})
|
||||
keywords.append({'label': kw, 'name': kw})
|
||||
|
||||
return keywords
|
||||
|
||||
|
||||
@@ -621,9 +621,12 @@ class RecipeSerializer(RecipeBaseSerializer):
|
||||
|
||||
|
||||
class RecipeImageSerializer(WritableNestedModelSerializer):
|
||||
image = serializers.ImageField(required=False, allow_null=True)
|
||||
image_url = serializers.CharField(max_length=4096, required=False, allow_null=True)
|
||||
|
||||
class Meta:
|
||||
model = Recipe
|
||||
fields = ['image', ]
|
||||
fields = ['image', 'image_url', ]
|
||||
|
||||
|
||||
class RecipeImportSerializer(SpacedModelSerializer):
|
||||
|
||||
@@ -5,6 +5,7 @@ import uuid
|
||||
from collections import OrderedDict
|
||||
|
||||
import requests
|
||||
from PIL import UnidentifiedImageError
|
||||
from annoying.decorators import ajax_request
|
||||
from annoying.functions import get_object_or_None
|
||||
from django.contrib import messages
|
||||
@@ -23,6 +24,7 @@ from django.utils.translation import gettext as _
|
||||
from django_scopes import scopes_disabled
|
||||
from icalendar import Calendar, Event
|
||||
from recipe_scrapers import NoSchemaFoundInWildMode, WebsiteNotImplementedError, scrape_me
|
||||
from requests.exceptions import MissingSchema
|
||||
from rest_framework import decorators, status, viewsets
|
||||
from rest_framework.exceptions import APIException, PermissionDenied
|
||||
from rest_framework.pagination import PageNumberPagination
|
||||
@@ -706,20 +708,33 @@ class RecipeViewSet(viewsets.ModelViewSet):
|
||||
|
||||
serializer = self.serializer_class(obj, data=request.data, partial=True)
|
||||
|
||||
if self.request.space.demo:
|
||||
raise PermissionDenied(detail='Not available in demo', code=None)
|
||||
|
||||
if serializer.is_valid():
|
||||
serializer.save()
|
||||
image = None
|
||||
|
||||
if serializer.validated_data == {}:
|
||||
obj.image = None
|
||||
else:
|
||||
img, filetype = handle_image(request, obj.image)
|
||||
if 'image' in serializer.validated_data:
|
||||
image = obj.image
|
||||
elif 'image_url' in serializer.validated_data:
|
||||
try:
|
||||
response = requests.get(serializer.validated_data['image_url'])
|
||||
image = File(io.BytesIO(response.content))
|
||||
print('test')
|
||||
except UnidentifiedImageError as e:
|
||||
print(e)
|
||||
pass
|
||||
except MissingSchema as e:
|
||||
print(e)
|
||||
pass
|
||||
except Exception as e:
|
||||
print(e)
|
||||
pass
|
||||
|
||||
if image is not None:
|
||||
img, filetype = handle_image(request, image)
|
||||
obj.image = File(img, name=f'{uuid.uuid4()}_{obj.pk}{filetype}')
|
||||
obj.save()
|
||||
obj.save()
|
||||
return Response(serializer.data)
|
||||
|
||||
return Response(serializer.data)
|
||||
return Response(serializer.errors, 400)
|
||||
|
||||
# TODO: refactor API to use post/put/delete or leave as put and change VUE to use list_recipe after creating
|
||||
|
||||
Reference in New Issue
Block a user