Allow recipes to be imported from json directly

This commit is contained in:
Patrick Pirker
2021-03-03 21:37:39 +01:00
committed by smilerz
parent d1b9d15816
commit 7414033495
3 changed files with 51 additions and 19 deletions

View File

@@ -88,6 +88,19 @@
id="id_btn_app"><i class="fas fa-file-archive"></i> {% trans 'Import' %} id="id_btn_app"><i class="fas fa-file-archive"></i> {% trans 'Import' %}
</button> </button>
</div> </div>
</div>
<div class="row">
<div class="col-md-12">
<div class="input-group mb-3">
<input class="form-control" v-model="json_data" placeholder="{% trans 'Enter json directly' %}">
<div class="input-group-append">
<button @click="loadRecipeJson()" class="btn btn-primary shadow-none" type="button"
id="id_btn_search"><i class="fas fa-search"></i>
</button>
</div>
</div>
</div>
</div>
<!-- Import JSON or HTML --> <!-- Import JSON or HTML -->
<div class=" tab-pane fade show" id="nav-source" role="tabpanel"> <div class=" tab-pane fade show" id="nav-source" role="tabpanel">
@@ -695,9 +708,20 @@
this.makeToast(gettext('Error'), msg, 'danger') this.makeToast(gettext('Error'), msg, 'danger')
}) })
}, },
showRecipe: function() { loadRecipeJson: function () {
this.preview = false this.recipe_data = undefined
this.recipe_data = this.recipe_json this.error = undefined
this.loading = true
this.$http.post("{% url 'api_recipe_from_json' %}", {'json': this.json_data}, {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) {

View File

@@ -93,7 +93,8 @@ urlpatterns = [
path('api/sync_all/', api.sync_all, name='api_sync'), path('api/sync_all/', api.sync_all, name='api_sync'),
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-source/', api.recipe_from_source, name='api_recipe_from_source'), path('api/recipe-from-url/', api.recipe_from_url, name='api_recipe_from_url'),
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

@@ -34,15 +34,8 @@ from cookbook.helper.ingredient_parser import parse
from cookbook.helper.permission_helper import (CustomIsAdmin, CustomIsGuest, from cookbook.helper.permission_helper import (CustomIsAdmin, CustomIsGuest,
CustomIsOwner, CustomIsShare, CustomIsOwner, CustomIsShare,
CustomIsShared, CustomIsUser, CustomIsShared, CustomIsUser,
group_required, share_link_valid)
from cookbook.helper.recipe_html_import import get_recipe_from_source
from cookbook.helper.recipe_url_import import get_from_scraper
group_required) group_required)
from cookbook.helper.recipe_search import search_recipes from cookbook.helper.recipe_url_import import get_from_html, find_recipe_json
from cookbook.helper.recipe_url_import import get_from_html, get_from_scraper, find_recipe_json
from cookbook.models import (CookLog, Food, Ingredient, Keyword, MealPlan, from cookbook.models import (CookLog, Food, Ingredient, Keyword, MealPlan,
MealType, Recipe, RecipeBook, ShoppingList, MealType, Recipe, RecipeBook, ShoppingList,
ShoppingListEntry, ShoppingListRecipe, Step, ShoppingListEntry, ShoppingListRecipe, Step,
@@ -609,14 +602,28 @@ def get_plan_ical(request, from_date, to_date):
@group_required('user') @group_required('user')
def recipe_from_source(request): def recipe_from_json(request):
url = request.POST.get('url', None) mjson = request.POST['json']
data = request.POST.get('data', None)
mode = request.POST.get('mode', None)
auto = request.POST.get('auto', 'true')
HEADERS = { md_json = json.loads(mjson)
"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7" if ('@type' in md_json
and md_json['@type'] == 'Recipe'):
return JsonResponse(find_recipe_json(md_json, ''))
return JsonResponse(
{
'error': True,
'msg': _('Could not parse correctly...')
},
status=400
)
@group_required('user')
def recipe_from_url(request):
url = request.POST['url']
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36' # noqa: E501
} }
if (not url and not data) or (mode == 'url' and not url) or (mode == 'source' and not data): if (not url and not data) or (mode == 'url' and not url) or (mode == 'source' and not data):