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:
@@ -3,7 +3,7 @@
|
||||
{% load static %}
|
||||
{% load i18n %}
|
||||
{% load l10n %}
|
||||
|
||||
{% load custom_tags %}
|
||||
|
||||
{% block title %}Test{% endblock %}
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
{% block content_fluid %}
|
||||
|
||||
<div id="app">
|
||||
|
||||
<import-view></import-view>
|
||||
</div>
|
||||
|
||||
@@ -27,6 +28,8 @@
|
||||
|
||||
<script type="application/javascript">
|
||||
window.CUSTOM_LOCALE = '{{ request.LANGUAGE_CODE }}'
|
||||
window.API_TOKEN = '{{ api_token }}'
|
||||
window.BOOKMARKLET_IMPORT_ID = {{ bookmarklet_import_id }}
|
||||
</script>
|
||||
|
||||
{% render_bundle 'import_view' %}
|
||||
|
||||
@@ -136,7 +136,7 @@ def bookmarklet(request):
|
||||
if (api_token := Token.objects.filter(user=request.user).first()) is None:
|
||||
api_token = Token.objects.create(user=request.user)
|
||||
|
||||
bookmark = "javascript: \
|
||||
bookmark = "<a href='javascript: \
|
||||
(function(){ \
|
||||
if(window.bookmarkletTandoor!==undefined){ \
|
||||
bookmarkletTandoor(); \
|
||||
@@ -146,8 +146,8 @@ def bookmarklet(request):
|
||||
localStorage.setItem('token', '" + api_token.__str__() + "'); \
|
||||
document.body.appendChild(document.createElement(\'script\')).src=\'" \
|
||||
+ server + prefix + static('js/bookmarklet.js') + "? \
|
||||
r=\'+Math.floor(Math.random()*999999999);}})();"
|
||||
return re.sub(r"[\n\t\s]*", "", bookmark)
|
||||
r=\'+Math.floor(Math.random()*999999999);}})();'>Test</a>"
|
||||
return re.sub(r"[\n\t]*", "", bookmark)
|
||||
|
||||
|
||||
@register.simple_tag
|
||||
|
||||
@@ -1085,13 +1085,20 @@ def recipe_from_source(request):
|
||||
- url: url to use for importing recipe
|
||||
- data: if no url is given recipe is imported from provided source data
|
||||
- auto: true to return just the recipe as json, false to return source json, html and images as well
|
||||
:return:
|
||||
- (optional) bookmarklet: id of bookmarklet import to use, overrides URL and data attributes
|
||||
:return: JsonResponse containing the parsed json, original html,json and images
|
||||
"""
|
||||
request_payload = json.loads(request.body.decode('utf-8'))
|
||||
url = request_payload.get('url', None)
|
||||
data = request_payload.get('data', None)
|
||||
bookmarklet = request_payload.get('bookmarklet', None)
|
||||
auto = True if request_payload.get('auto', 'true') == 'true' else False
|
||||
|
||||
if bookmarklet := BookmarkletImport.objects.filter(pk=bookmarklet).first():
|
||||
url = bookmarklet.url
|
||||
data = bookmarklet.html
|
||||
bookmarklet.delete()
|
||||
|
||||
# headers to use for request to external sites
|
||||
external_request_headers = {"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7"}
|
||||
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -327,10 +327,10 @@ def user_settings(request):
|
||||
if not sp:
|
||||
sp = SearchPreferenceForm(user=request.user)
|
||||
fields_searched = (
|
||||
len(search_form.cleaned_data['icontains'])
|
||||
+ len(search_form.cleaned_data['istartswith'])
|
||||
+ len(search_form.cleaned_data['trigram'])
|
||||
+ len(search_form.cleaned_data['fulltext'])
|
||||
len(search_form.cleaned_data['icontains'])
|
||||
+ len(search_form.cleaned_data['istartswith'])
|
||||
+ len(search_form.cleaned_data['trigram'])
|
||||
+ len(search_form.cleaned_data['fulltext'])
|
||||
)
|
||||
if fields_searched == 0:
|
||||
search_form.add_error(None, _('You must select at least one field to search!'))
|
||||
@@ -647,7 +647,10 @@ def test(request):
|
||||
if not settings.DEBUG:
|
||||
return HttpResponseRedirect(reverse('index'))
|
||||
|
||||
return render(request, 'test.html', {})
|
||||
if (api_token := Token.objects.filter(user=request.user).first()) is None:
|
||||
api_token = Token.objects.create(user=request.user)
|
||||
|
||||
return render(request, 'test.html', {'api_token': api_token})
|
||||
|
||||
|
||||
def test2(request):
|
||||
|
||||
Reference in New Issue
Block a user