From edb9c883f7ac0a66c8d105cb11d85bfb428d3fe4 Mon Sep 17 00:00:00 2001 From: Tobias Lindenberg Date: Sun, 10 Jan 2021 12:19:39 +0100 Subject: [PATCH] views/import_export --- cookbook/views/import_export.py | 50 +++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/cookbook/views/import_export.py b/cookbook/views/import_export.py index 1312f3e99..921455f72 100644 --- a/cookbook/views/import_export.py +++ b/cookbook/views/import_export.py @@ -23,7 +23,9 @@ def import_recipe(request): form = ImportForm(request.POST) if form.is_valid(): try: - data = json.loads(re.sub(r'"id":([0-9])+,', '', form.cleaned_data['recipe'])) + data = json.loads( + re.sub(r'"id":([0-9])+,', '', form.cleaned_data['recipe']) + ) sr = RecipeSerializer(data=data) if sr.is_valid(): @@ -34,18 +36,39 @@ def import_recipe(request): try: fmt, img = data['image'].split(';base64,') ext = fmt.split('/')[-1] - recipe.image = ContentFile(base64.b64decode(img), name=f'{recipe.pk}.{ext}') # TODO possible security risk, maybe some checks needed + # 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])) + 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) + messages.add_message( + request, + messages.ERROR, + _('Something went wrong during the import!') + ) + messages.add_message( + request, messages.WARNING, sr.errors + ) except JSONDecodeError: - messages.add_message(request, messages.ERROR, _('Could not parse the supplied JSON!')) + messages.add_message( + request, + messages.ERROR, + _('Could not parse the supplied JSON!') + ) else: form = ImportForm() @@ -65,18 +88,23 @@ def export_recipe(request): 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")}' + 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' + 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.')) + form.add_error( + 'recipe', + _('External recipes cannot be exported, please share the file directly or select an internal recipe.') # noqa: E501 + ) else: form = ExportForm() recipe = request.GET.get('r')