mirror of
https://github.com/TandoorRecipes/recipes.git
synced 2026-01-01 12:18:45 -05:00
lots of improvements and bookmarklet import working again
This commit is contained in:
@@ -16,6 +16,7 @@ from django.utils.translation import ngettext
|
||||
from django_tables2 import RequestConfig
|
||||
from PIL import UnidentifiedImageError
|
||||
from requests.exceptions import MissingSchema
|
||||
from rest_framework.authtoken.models import Token
|
||||
|
||||
from cookbook.forms import BatchEditForm, SyncForm
|
||||
from cookbook.helper.image_processing import handle_image
|
||||
@@ -23,7 +24,7 @@ from cookbook.helper.ingredient_parser import IngredientParser
|
||||
from cookbook.helper.permission_helper import group_required, has_group_permission
|
||||
from cookbook.helper.recipe_url_import import parse_cooktime
|
||||
from cookbook.models import (Comment, Food, Ingredient, Keyword, Recipe, RecipeImport, Step, Sync,
|
||||
Unit, UserPreference)
|
||||
Unit, UserPreference, BookmarkletImport)
|
||||
from cookbook.tables import SyncTable
|
||||
from recipes import settings
|
||||
|
||||
@@ -123,7 +124,6 @@ def batch_edit(request):
|
||||
|
||||
|
||||
@group_required('user')
|
||||
@atomic
|
||||
def import_url(request):
|
||||
if request.space.max_recipes != 0 and Recipe.objects.filter(space=request.space).count() >= request.space.max_recipes: # TODO move to central helper function
|
||||
messages.add_message(request, messages.WARNING, _('You have reached the maximum number of recipes for your space.'))
|
||||
@@ -133,93 +133,15 @@ def import_url(request):
|
||||
messages.add_message(request, messages.WARNING, _('You have more users than allowed in your space.'))
|
||||
return HttpResponseRedirect(reverse('index'))
|
||||
|
||||
if request.method == 'POST':
|
||||
data = json.loads(request.body)
|
||||
data['cookTime'] = parse_cooktime(data.get('cookTime', ''))
|
||||
data['prepTime'] = parse_cooktime(data.get('prepTime', ''))
|
||||
|
||||
recipe = Recipe.objects.create(
|
||||
name=data['name'],
|
||||
description=data['description'],
|
||||
waiting_time=data['cookTime'],
|
||||
working_time=data['prepTime'],
|
||||
servings=data['servings'],
|
||||
internal=True,
|
||||
created_by=request.user,
|
||||
space=request.space,
|
||||
)
|
||||
|
||||
step = Step.objects.create(
|
||||
instruction=data['recipeInstructions'], space=request.space,
|
||||
)
|
||||
|
||||
recipe.steps.add(step)
|
||||
|
||||
for kw in data['keywords']:
|
||||
if data['all_keywords']: # do not remove this check :) https://github.com/vabene1111/recipes/issues/645
|
||||
k, created = Keyword.objects.get_or_create(name=kw['text'], space=request.space)
|
||||
recipe.keywords.add(k)
|
||||
else:
|
||||
try:
|
||||
k = Keyword.objects.get(name=kw['text'], space=request.space)
|
||||
recipe.keywords.add(k)
|
||||
except ObjectDoesNotExist:
|
||||
pass
|
||||
|
||||
ingredient_parser = IngredientParser(request, True)
|
||||
for ing in data['recipeIngredient']:
|
||||
original = ing.pop('original', None) or ing.pop('original_text', None)
|
||||
ingredient = Ingredient(original_text=original, space=request.space, )
|
||||
|
||||
if food_text := ing['ingredient']['text'].strip():
|
||||
ingredient.food = ingredient_parser.get_food(food_text)
|
||||
|
||||
if ing['unit']:
|
||||
if unit_text := ing['unit']['text'].strip():
|
||||
ingredient.unit = ingredient_parser.get_unit(unit_text)
|
||||
|
||||
# TODO properly handle no_amount recipes
|
||||
if isinstance(ing['amount'], str):
|
||||
try:
|
||||
ingredient.amount = float(ing['amount'].replace(',', '.'))
|
||||
except ValueError:
|
||||
ingredient.no_amount = True
|
||||
pass
|
||||
elif isinstance(ing['amount'], float) \
|
||||
or isinstance(ing['amount'], int):
|
||||
ingredient.amount = ing['amount']
|
||||
ingredient.note = ing['note'].strip() if 'note' in ing else ''
|
||||
|
||||
ingredient.save()
|
||||
step.ingredients.add(ingredient)
|
||||
|
||||
if 'image' in data and data['image'] != '' and data['image'] is not None:
|
||||
try:
|
||||
response = requests.get(data['image'])
|
||||
|
||||
img, filetype = handle_image(request, File(BytesIO(response.content), name='image'))
|
||||
recipe.image = File(
|
||||
img, name=f'{uuid.uuid4()}_{recipe.pk}{filetype}'
|
||||
)
|
||||
recipe.save()
|
||||
except UnidentifiedImageError as e:
|
||||
print(e)
|
||||
pass
|
||||
except MissingSchema as e:
|
||||
print(e)
|
||||
pass
|
||||
except Exception as e:
|
||||
print(e)
|
||||
pass
|
||||
|
||||
return HttpResponse(reverse('view_recipe', args=[recipe.pk]))
|
||||
if (api_token := Token.objects.filter(user=request.user).first()) is None:
|
||||
api_token = Token.objects.create(user=request.user)
|
||||
|
||||
bookmarklet_import_id = -1
|
||||
if 'id' in request.GET:
|
||||
context = {'bookmarklet': request.GET.get('id', '')}
|
||||
else:
|
||||
context = {}
|
||||
if bookmarklet_import := BookmarkletImport.objects.filter(id=request.GET['id']).first():
|
||||
bookmarklet_import_id = bookmarklet_import.pk
|
||||
|
||||
return render(request, 'url_import.html', context)
|
||||
return render(request, 'test.html', {'api_token': api_token, 'bookmarklet_import_id': bookmarklet_import_id})
|
||||
|
||||
|
||||
class Object(object):
|
||||
|
||||
Reference in New Issue
Block a user