importing improvements

This commit is contained in:
vabene1111
2020-06-23 11:04:49 +02:00
parent 8594346488
commit 98c278fe60
5 changed files with 46 additions and 8 deletions

View File

@@ -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)

View File

@@ -42,6 +42,10 @@
<button class="dropdown-item" type="button"
onclick="location.href='{% url 'new_recipe' %}'"><i
class="fas fa-plus-circle fa-fw"></i> {% trans 'New Recipe' %}</button>
<button class="dropdown-item" type="button"
onclick="location.href='{% url 'data_import_url' %}'"><i
class="fas fa-cloud-download-alt fa-fw"></i> {% trans 'Website Import' %}
</button>
<button data-toggle="collapse" href="#collapse_adv_search"
role="button" class="dropdown-item"
aria-expanded="false" type="button"

View File

@@ -73,6 +73,7 @@
<th>{% trans 'Amount' %}</th>
<th>{% trans 'Unit' %}</th>
<th>{% trans 'Ingredient' %}</th>
<th></th>
</tr>
</thead>
<tbody>
@@ -81,9 +82,18 @@
<td><input class="form-control" v-model="i.amount"></td>
<td><input class="form-control" v-model="i.unit"></td>
<td><input class="form-control" v-model="i.ingredient"></td>
<td style="vertical-align: middle;text-align: center">
<button class="btn btn-outline-danger" type="button" @click="deleteIngredient(i)"><i
class="fas fa-trash-alt"></i></button>
</td>
</tr>
</tbody>
</table>
<div style="text-align: center">
<button class="btn btn-success" type="button" @click="addIngredient()"><i class="fas fa-plus"></i></button>
<br/><br/>
</div>
</div>
</div>
@@ -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;

View File

@@ -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)

View File

@@ -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,
)