From 98c278fe6044d81397336832e2263c17026dfc81 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Tue, 23 Jun 2020 11:04:49 +0200 Subject: [PATCH] importing improvements --- cookbook/helper/recipe_url_import.py | 21 +++++++++++++++++---- cookbook/templates/index.html | 4 ++++ cookbook/templates/url_import.html | 23 +++++++++++++++++++++-- cookbook/views/api.py | 4 ++-- cookbook/views/data.py | 2 ++ 5 files changed, 46 insertions(+), 8 deletions(-) diff --git a/cookbook/helper/recipe_url_import.py b/cookbook/helper/recipe_url_import.py index 4afca8061..74000dafc 100644 --- a/cookbook/helper/recipe_url_import.py +++ b/cookbook/helper/recipe_url_import.py @@ -1,12 +1,13 @@ import re from django.http import JsonResponse -from django.utils.dateparse import parse_datetime, parse_duration +from django.utils.dateparse import parse_duration +from django.utils.translation import gettext as _ from cookbook.models import Keyword -def find_recipe_json(ld_json): +def find_recipe_json(ld_json, url): ld_json['org'] = str(ld_json) if type(ld_json['name']) == list: @@ -24,6 +25,12 @@ def find_recipe_json(ld_json): if len(ld_json['recipeIngredient']) == 1 and len(ld_json['recipeIngredient'][0]) > 30: ld_json['recipeIngredient'] = ld_json['recipeIngredient'][0].split(',') + for x in ld_json['recipeIngredient']: + if '\n' in x: + ld_json['recipeIngredient'].remove(x) + for i in x.split('\n'): + ld_json['recipeIngredient'].insert(0, i) + ingredients = [] for x in ld_json['recipeIngredient']: @@ -86,6 +93,8 @@ def find_recipe_json(ld_json): else: ld_json['recipeInstructions'] = '' + ld_json['recipeInstructions'] += _('Imported from ') + url + if 'image' in ld_json: # check if list of images is returned, take first if so if (type(ld_json['image'])) == list: @@ -101,11 +110,15 @@ def find_recipe_json(ld_json): if 'cookTime' in ld_json: if type(ld_json['cookTime']) == list and len(ld_json['cookTime']) > 0: ld_json['cookTime'] = ld_json['cookTime'][0] - ld_json['cookTime'] = round(parse_duration(ld_json['cookTime']).seconds/60) + ld_json['cookTime'] = round(parse_duration(ld_json['cookTime']).seconds / 60) + else: + ld_json['cookTime'] = 0 if 'prepTime' in ld_json: if type(ld_json['prepTime']) == list and len(ld_json['prepTime']) > 0: ld_json['prepTime'] = ld_json['prepTime'][0] - ld_json['prepTime'] = round(parse_duration(ld_json['prepTime']).seconds/60) + ld_json['prepTime'] = round(parse_duration(ld_json['prepTime']).seconds / 60) + else: + ld_json['prepTime'] = 0 return JsonResponse(ld_json) diff --git a/cookbook/templates/index.html b/cookbook/templates/index.html index 778608560..981fd36cc 100644 --- a/cookbook/templates/index.html +++ b/cookbook/templates/index.html @@ -42,6 +42,10 @@ + + +
+ +

+
+ @@ -179,7 +189,7 @@ delimiters: ['[[', ']]'], el: '#app', data: { - remote_url: 'https://www.rezeptschachtel.de/schwarzwaelder_kirschtorte_rezept.html', + remote_url: '', keywords: [], recipe_data: undefined, error: undefined, @@ -187,7 +197,6 @@ all_keywords: false, }, mounted: function () { - this.loadRecipe(); this.getKeywords(); }, methods: { @@ -212,6 +221,16 @@ console.log("dragChanged create error", err); }) }, + deleteIngredient: function (i) { + this.recipe_data.recipeIngredient = this.recipe_data.recipeIngredient.filter(item => item !== i) + }, + addIngredient: function (i) { + this.recipe_data.recipeIngredient.push({ + unit: '{{ request.user.userpreference.default_unit }}', + amount: 0, + ingredient: '' + }) + }, getKeywords: function () { this.$http.get("{% url 'dal_keyword' %}").then((response) => { this.keywords = response.data.results; diff --git a/cookbook/views/api.py b/cookbook/views/api.py index 5a361b9ec..658b37b24 100644 --- a/cookbook/views/api.py +++ b/cookbook/views/api.py @@ -291,7 +291,7 @@ def recipe_from_url(request, url): ld_json_item = x if '@type' in ld_json_item and ld_json_item['@type'] == 'Recipe': - return find_recipe_json(ld_json_item) + return find_recipe_json(ld_json_item, url) except JSONDecodeError: JsonResponse({'error': True, 'msg': _('The requested site does not provided malformed data and cannot be read.')}, status=400) @@ -300,6 +300,6 @@ def recipe_from_url(request, url): for i in items: md_json = json.loads(i.json()) if 'schema.org/Recipe' in str(md_json['type']): - return find_recipe_json(md_json['properties']) + return find_recipe_json(md_json['properties'], url) return JsonResponse({'error': True, 'msg': _('The requested site does not provide any recognized data format to import the recipe from.')}, status=400) diff --git a/cookbook/views/data.py b/cookbook/views/data.py index 3ca87a639..3a2f71dd2 100644 --- a/cookbook/views/data.py +++ b/cookbook/views/data.py @@ -101,6 +101,8 @@ def import_url(request): recipe = Recipe.objects.create( name=data['name'], instructions=data['recipeInstructions'], + waiting_time=data['cookTime'], + working_time=data['prepTime'], internal=True, created_by=request.user, )