more cleanup from ugly merge

This commit is contained in:
smilerz 2021-03-19 13:17:10 -05:00
parent 90670613c5
commit 7c8d41753c
7 changed files with 35 additions and 71 deletions

View File

@ -77,7 +77,7 @@ def find_recipe_json(ld_json, url, space):
if 'recipeCuisine' in ld_json: if 'recipeCuisine' in ld_json:
keywords += listify_keywords(ld_json['recipeCuisine']) keywords += listify_keywords(ld_json['recipeCuisine'])
try: try:
ld_json['keywords'] = parse_keywords(list(set(map(str.casefold, keywords)))) ld_json['keywords'] = parse_keywords(list(set(map(str.casefold, keywords))), space)
except TypeError: except TypeError:
pass pass
@ -179,7 +179,7 @@ def get_from_scraper(scrape, space):
ingredients = [] ingredients = []
for x in scrape.ingredients(): for x in scrape.ingredients():
try: try:
amount, unit, ingredient, note = parse_ingredient(x) amount, unit, ingredient, note = parse_single_ingredient(x)
if ingredient: if ingredient:
ingredients.append( ingredients.append(
{ {

View File

@ -137,7 +137,7 @@
this.error = undefined this.error = undefined
this.parsed = true this.parsed = true
this.loading = true this.loading = true
this.$http.post("{% url 'api_recipe_from_html' %}", {'html_text': this.html_recipe}, {emulateJSON: true}).then((response) => { this.$http.post("{% url 'api_manual_recipe_from_json' %}", {'html_text': this.html_recipe}, {emulateJSON: true}).then((response) => {
console.log(response.data) console.log(response.data)
this.recipe_data = response.data['recipe_data']; this.recipe_data = response.data['recipe_data'];
this.recipe_tree = response.data['recipe_tree']; this.recipe_tree = response.data['recipe_tree'];

View File

@ -152,7 +152,7 @@
this.error = undefined this.error = undefined
this.parsed = true this.parsed = true
this.loading = true this.loading = true
this.$http.post("{% url 'api_recipe_from_html' %}", {'html_text': this.html_recipe}, {emulateJSON: true}).then((response) => { this.$http.post("{% url 'api_manual_recipe_from_json' %}", {'html_text': this.html_recipe}, {emulateJSON: true}).then((response) => {
console.log(response.data) console.log(response.data)
this.recipe_data = response.data['recipe_data']; this.recipe_data = response.data['recipe_data'];
this.recipe_tree = response.data['recipe_tree']; this.recipe_tree = response.data['recipe_tree'];

View File

@ -52,7 +52,7 @@
<div class="row tab-pane fade show" id="nav-ldjson" role="tabpanel"> <div class="row tab-pane fade show" id="nav-ldjson" role="tabpanel">
<div class="col-md-12"> <div class="col-md-12">
<div class="input-group input-group-lg"> <div class="input-group input-group-lg">
<textarea class="form-control input-group-append" v-model="json_data" rows=10 placeholder="{% trans 'Paste ld+json here to parse recipe automatically.' %}" style="font-size: 12px"> <textarea class="form-control input-group-append" v-model="raw_data" rows=10 placeholder="{% trans 'Paste ld+json here to parse recipe automatically.' %}" style="font-size: 12px">
</textarea> </textarea>
</div> </div>
<br> <br>
@ -66,7 +66,7 @@
<div class="row tab-pane fade show" id="nav-json" role="tabpanel"> <div class="row tab-pane fade show" id="nav-json" role="tabpanel">
<div class="col-md-12"> <div class="col-md-12">
<div class="input-group input-group-lg"> <div class="input-group input-group-lg">
<textarea class="form-control input-group-append" v-model="html_data" rows=10 <textarea class="form-control input-group-append" v-model="raw_data" rows=10
placeholder="{% trans 'To parse recipe manually: Paste JSON document here or a web page source that contains one or more JSON elements here.' %}" style="font-size: 12px"> placeholder="{% trans 'To parse recipe manually: Paste JSON document here or a web page source that contains one or more JSON elements here.' %}" style="font-size: 12px">
</textarea> </textarea>
</div> </div>
@ -81,7 +81,7 @@
<div class="row tab-pane fade show" id="nav-html" role="tabpanel"> <div class="row tab-pane fade show" id="nav-html" role="tabpanel">
<div class="col-md-12"> <div class="col-md-12">
<div class="input-group input-group-lg"> <div class="input-group input-group-lg">
<textarea class="form-control input-group-append" v-model="html_data" rows=10 placeholder="{% trans 'Paste html source here to parse recipe manually.' %}" style="font-size: 12px"> <textarea class="form-control input-group-append" v-model="raw_data" rows=10 placeholder="{% trans 'Paste html source here to parse recipe manually.' %}" style="font-size: 12px">
</textarea> </textarea>
</div> </div>
<br> <br>
@ -100,7 +100,7 @@
<i class="fas fa-spinner fa-spin fa-8x"></i> <i class="fas fa-spinner fa-spin fa-8x"></i>
</div> </div>
<!-- recipe preview on HTML Import --> <!-- recipe preview before Import -->
<div class="container-fluid" v-if="parsed" id="manage_tree"> <div class="container-fluid" v-if="parsed" id="manage_tree">
<h2></h2> <h2></h2>
@ -274,7 +274,7 @@
</div> </div>
</div> </div>
<!-- end of recipe preview on HTML Import --> <!-- end of recipe preview before Import -->
<template v-if="recipe_data !== undefined"> <template v-if="recipe_data !== undefined">
@ -288,12 +288,6 @@
<textarea id="id_description" class="form-control" rows="3" v-model="recipe_data.description"></textarea> <textarea id="id_description" class="form-control" rows="3" v-model="recipe_data.description"></textarea>
</div> </div>
<div class="form-group">
<label for="id_description">{% trans 'Recipe Description' %}</label>
<textarea id="id_description" class="form-control" v-model="recipe_data.description"
rows="4"></textarea>
</div>
<div class="row"> <div class="row">
<div class="col col-md-6" v-if="recipe_data.image !== ''"> <div class="col col-md-6" v-if="recipe_data.image !== ''">
<img v-bind:src="recipe_data.image" alt="{% trans 'Recipe Image' %}" <img v-bind:src="recipe_data.image" alt="{% trans 'Recipe Image' %}"
@ -521,7 +515,7 @@
el: '#app', el: '#app',
data: { data: {
remote_url: '', remote_url: '',
json_data: undefined, raw_data: undefined,
keywords: [], keywords: [],
keywords_loading: false, keywords_loading: false,
units: [], units: [],
@ -536,8 +530,6 @@
importing_recipe: false, importing_recipe: false,
recipe_json: undefined, recipe_json: undefined,
recipe_tree: undefined, recipe_tree: undefined,
recipe_tree1: undefined,
html_data: undefined,
}, },
directives: { directives: {
tabindex: { tabindex: {
@ -587,7 +579,7 @@
this.recipe_data = undefined this.recipe_data = undefined
this.error = undefined this.error = undefined
this.loading = true this.loading = true
this.$http.post("{% url 'api_recipe_from_json' %}", {'json': this.json_data}, {emulateJSON: true}).then((response) => { this.$http.post("{% url 'api_recipe_from_json' %}", {'json': this.raw_data}, {emulateJSON: true}).then((response) => {
console.log(response.data) console.log(response.data)
this.recipe_data = response.data; this.recipe_data = response.data;
this.loading = false this.loading = false
@ -604,11 +596,13 @@
this.makeToast(gettext('Error'), msg, 'danger') this.makeToast(gettext('Error'), msg, 'danger')
}) })
}, },
loadRecipeJson: function () { loadRecipeHTML: function () {
this.recipe_data = undefined // TODO this needs refactored to get HTML elements
this.error = undefined this.error = undefined
this.loading = true this.loading = true
this.$http.post("{% url 'api_recipe_from_json' %}", {'json': this.json_data}, {emulateJSON: true}).then((response) => { this.parsed = false
this.recipe_json['@type'] = "Recipe"
this.$http.post("{% url 'api_recipe_from_json' %}", {'html': JSON.stringify(this.raw_data)}, {emulateJSON: true}).then((response) => {
console.log(response.data) console.log(response.data)
this.recipe_data = response.data; this.recipe_data = response.data;
this.loading = false this.loading = false
@ -640,10 +634,9 @@
this.error = undefined this.error = undefined
this.loading = true this.loading = true
this.$http.post("{% url 'api_recipe_from_html' %}", {'html_data': this.html_data}, {emulateJSON: true}).then((response) => { this.$http.post("{% url 'api_manual_recipe_from_json' %}", {'data': this.raw_data}, {emulateJSON: true}).then((response) => {
console.log(response.data) console.log(response.data)
this.recipe_json = response.data['recipe_json']; this.recipe_json = response.data['recipe_json'];
this.recipe_tree1 = JSON.stringify(response.data['recipe_tree'], null, 2);
this.recipe_tree = response.data['recipe_tree']; this.recipe_tree = response.data['recipe_tree'];
this.loading = false this.loading = false
this.parsed = true this.parsed = true
@ -655,23 +648,6 @@
this.makeToast(gettext('Error'), gettext('There was an error loading a resource!') + err.bodyText, 'danger') this.makeToast(gettext('Error'), gettext('There was an error loading a resource!') + err.bodyText, 'danger')
}) })
}, },
loadRecipeHTML: function () {
// TODO this needs refactored to get HTML elements
this.error = undefined
this.loading = true
this.parsed = false
this.recipe_json['@type'] = "Recipe"
this.$http.post("{% url 'api_recipe_from_json' %}", {'json': JSON.stringify(this.recipe_json)}, {emulateJSON: true}).then((response) => {
console.log(response.data)
this.recipe_data = response.data;
this.loading = false
}).catch((err) => {
this.error = err.data
this.loading = false
console.log(err)
this.makeToast(gettext('Error'), gettext('There was an error loading a resource!') + err.bodyText, 'danger')
})
},
importRecipe: function () { importRecipe: function () {
if (this.recipe_data.name.length > 128) { if (this.recipe_data.name.length > 128) {
this.makeToast(gettext('Error'), gettext('Recipe name is longer than 128 characters'), 'danger') this.makeToast(gettext('Error'), gettext('Recipe name is longer than 128 characters'), 'danger')

View File

@ -93,7 +93,7 @@ urlpatterns = [
path('api/log_cooking/<int:recipe_id>/', api.log_cooking, name='api_log_cooking'), path('api/log_cooking/<int:recipe_id>/', api.log_cooking, name='api_log_cooking'),
path('api/plan-ical/<slug:from_date>/<slug:to_date>/', api.get_plan_ical, name='api_get_plan_ical'), path('api/plan-ical/<slug:from_date>/<slug:to_date>/', api.get_plan_ical, name='api_get_plan_ical'),
path('api/recipe-from-url/', api.recipe_from_url, name='api_recipe_from_url'), path('api/recipe-from-url/', api.recipe_from_url, name='api_recipe_from_url'),
path('api/recipe-from-html/', api.recipe_from_html, name='api_recipe_from_html'), path('api/recipe-from-html/', api.manual_recipe_from_json, name='api_manual_recipe_from_json'),
path('api/recipe-from-json/', api.recipe_from_json, name='api_recipe_from_json'), path('api/recipe-from-json/', api.recipe_from_json, name='api_recipe_from_json'),
path('api/backup/', api.get_backup, name='api_backup'), path('api/backup/', api.get_backup, name='api_backup'),
path('api/ingredient-from-string/', api.ingredient_from_string, name='api_ingredient_from_string'), path('api/ingredient-from-string/', api.ingredient_from_string, name='api_ingredient_from_string'),

View File

@ -539,9 +539,17 @@ def recipe_from_json(request):
mjson = request.POST['json'] mjson = request.POST['json']
md_json = json.loads(mjson) md_json = json.loads(mjson)
if ('@type' in md_json for ld_json_item in md_json:
and md_json['@type'] == 'Recipe'): # recipes type might be wrapped in @graph type
return JsonResponse(find_recipe_json(md_json, '')) if '@graph' in ld_json_item:
for x in md_json['@graph']:
if '@type' in x and x['@type'] == 'Recipe':
md_json = x
if ('@type' in md_json
and md_json['@type'] == 'Recipe'):
return JsonResponse(find_recipe_json(md_json, '', request.space))
return JsonResponse( return JsonResponse(
{ {
'error': True, 'error': True,
@ -608,14 +616,14 @@ def recipe_from_url_old(request):
@group_required('user') @group_required('user')
def recipe_from_html(request): def manual_recipe_from_json(request):
html_data = request.POST['html_data'] json_data = request.POST['data']
recipe_json, recipe_tree = get_from_raw(html_data) recipe_json, recipe_tree = get_from_raw(json_data)
if len(recipe_tree) == 0 and len(recipe_json) == 0: if len(recipe_tree) == 0 and len(recipe_json) == 0:
return JsonResponse( return JsonResponse(
{ {
'error': True, 'error': True,
'msg': _('The requested page refused to provide any information (Status Code 403).') # noqa: E501 'msg': _('No useable data could be found.') # noqa: E501
}, },
status=400 status=400
) )
@ -631,26 +639,6 @@ def get_backup(request):
if not request.user.is_superuser: if not request.user.is_superuser:
return HttpResponse('', status=403) return HttpResponse('', status=403)
md_json = json.loads(mjson)
for ld_json_item in md_json:
# recipes type might be wrapped in @graph type
if '@graph' in ld_json_item:
for x in md_json['@graph']:
if '@type' in x and x['@type'] == 'Recipe':
md_json = x
if ('@type' in md_json
and md_json['@type'] == 'Recipe'):
return JsonResponse(find_recipe_json(md_json, '', request.space))
return JsonResponse(
{
'error': True,
'msg': _('Could not parse correctly...')
},
status=400
)
@group_required('user') @group_required('user')
def ingredient_from_string(request): def ingredient_from_string(request):

View File

@ -129,10 +129,10 @@ def import_url(request):
recipe.steps.add(step) recipe.steps.add(step)
for kw in data['keywords']: for kw in data['keywords']:
if kw['id'] != "null" and (k := Keyword.objects.filter(id=kw['id'], space=request.space).first()): if k := Keyword.objects.filter(name=kw['text'], space=request.space).first():
recipe.keywords.add(k) recipe.keywords.add(k)
elif data['all_keywords']: elif data['all_keywords']:
k = Keyword.objects.create(name=kw['text'], space=request.space) k = Keyword.objects.create(name=kw['text'])
recipe.keywords.add(k) recipe.keywords.add(k)
for ing in data['recipeIngredient']: for ing in data['recipeIngredient']: