using new import/export module
This commit is contained in:
@ -1,76 +1,24 @@
|
||||
import base64
|
||||
import json
|
||||
import re
|
||||
from json import JSONDecodeError
|
||||
|
||||
from django.contrib import messages
|
||||
from django.core.files.base import ContentFile
|
||||
from django.http import HttpResponse, HttpResponseRedirect
|
||||
from django.shortcuts import render
|
||||
from django.urls import reverse_lazy
|
||||
from django.utils.translation import gettext as _
|
||||
from rest_framework.renderers import JSONRenderer
|
||||
|
||||
from cookbook.forms import ExportForm, ImportForm
|
||||
from cookbook.forms import ExportForm, ExportForm, ImportForm
|
||||
from cookbook.helper.permission_helper import group_required
|
||||
from cookbook.integration.default import Default
|
||||
from cookbook.models import Recipe
|
||||
from cookbook.serializer import RecipeSerializer, RecipeExportSerializer
|
||||
|
||||
|
||||
def get_integration(request, export_type):
|
||||
return Default(request)
|
||||
|
||||
|
||||
@group_required('user')
|
||||
def import_recipe(request):
|
||||
if request.method == "POST":
|
||||
form = ImportForm(request.POST)
|
||||
form = ImportForm(request.POST, request.FILES)
|
||||
if form.is_valid():
|
||||
try:
|
||||
data = json.loads(
|
||||
re.sub(r'"id":([0-9]+),', '', re.sub(r',(\s)*"recipe":([0-9]+)', '', form.cleaned_data['recipe']))
|
||||
)
|
||||
|
||||
sr = RecipeExportSerializer(data=data, context={'request': request})
|
||||
if sr.is_valid():
|
||||
sr.validated_data['created_by'] = request.user
|
||||
recipe = sr.save()
|
||||
|
||||
if data['image']:
|
||||
try:
|
||||
fmt, img = data['image'].split(';base64,')
|
||||
ext = fmt.split('/')[-1]
|
||||
# TODO possible security risk,
|
||||
# maybe some checks needed
|
||||
recipe.image = (ContentFile(
|
||||
base64.b64decode(img),
|
||||
name=f'{recipe.pk}.{ext}')
|
||||
)
|
||||
recipe.save()
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
messages.add_message(
|
||||
request,
|
||||
messages.SUCCESS,
|
||||
_('Recipe imported successfully!')
|
||||
)
|
||||
return HttpResponseRedirect(
|
||||
reverse_lazy('view_recipe', args=[recipe.pk])
|
||||
)
|
||||
else:
|
||||
messages.add_message(
|
||||
request,
|
||||
messages.ERROR,
|
||||
_('Something went wrong during the import!')
|
||||
)
|
||||
messages.add_message(
|
||||
request, messages.WARNING, sr.errors
|
||||
)
|
||||
except JSONDecodeError as e:
|
||||
print(e)
|
||||
messages.add_message(
|
||||
request,
|
||||
messages.ERROR,
|
||||
_('Could not parse the supplied JSON!')
|
||||
)
|
||||
|
||||
integration = Default(request)
|
||||
return integration.do_import(request.FILES.getlist('files'))
|
||||
else:
|
||||
form = ImportForm()
|
||||
|
||||
@ -79,41 +27,17 @@ def import_recipe(request):
|
||||
|
||||
@group_required('user')
|
||||
def export_recipe(request):
|
||||
context = {}
|
||||
if request.method == "POST":
|
||||
form = ExportForm(request.POST)
|
||||
if form.is_valid():
|
||||
recipe = form.cleaned_data['recipe']
|
||||
if recipe.internal:
|
||||
export = RecipeExportSerializer(recipe).data
|
||||
|
||||
if recipe.image and form.cleaned_data['image']:
|
||||
with open(recipe.image.path, 'rb') as img_f:
|
||||
export['image'] = f'data:image/png;base64,{base64.b64encode(img_f.read()).decode("utf-8")}' # noqa: E501
|
||||
|
||||
json_string = JSONRenderer().render(export).decode("utf-8")
|
||||
|
||||
if form.cleaned_data['download']:
|
||||
response = HttpResponse(
|
||||
json_string, content_type='text/plain'
|
||||
)
|
||||
response['Content-Disposition'] = f'attachment; filename={recipe.name}.json' # noqa: E501
|
||||
return response
|
||||
|
||||
context['export'] = re.sub(r'"id":([0-9])+,', '', json_string)
|
||||
else:
|
||||
form.add_error(
|
||||
'recipe',
|
||||
_('External recipes cannot be exported, please share the file directly or select an internal recipe.') # noqa: E501
|
||||
)
|
||||
integration = Default(request)
|
||||
return integration.do_export(form.cleaned_data['recipes'])
|
||||
else:
|
||||
form = ExportForm()
|
||||
recipe = request.GET.get('r')
|
||||
if recipe:
|
||||
if re.match(r'^([0-9])+$', recipe):
|
||||
if recipe := Recipe.objects.filter(pk=int(recipe)).first():
|
||||
form = ExportForm(initial={'recipe': recipe})
|
||||
form = ExportForm(initial={'recipes': recipe})
|
||||
|
||||
context['form'] = form
|
||||
|
||||
return render(request, 'export.html', context)
|
||||
return render(request, 'export.html', {'form': form})
|
||||
|
Reference in New Issue
Block a user