basic export to zip working

This commit is contained in:
vabene1111 2021-02-07 13:43:24 +01:00
parent d45adc1688
commit 3e7f96c0b8
2 changed files with 13 additions and 7 deletions

View File

@ -1,7 +1,10 @@
import json
import os
from io import StringIO, BytesIO
from os.path import basename
from zipfile import ZipFile
from django.http import HttpResponse
from rest_framework.renderers import JSONRenderer
from cookbook.integration.integration import Integration
@ -12,7 +15,8 @@ class Default(Integration):
def do_export(self, recipes):
path = self.get_tmp_dir_path()
export_zip_obj = ZipFile(os.path.join(path, 'export.zip'), 'w')
s = BytesIO()
export_zip_obj = ZipFile(s, 'w')
for r in recipes:
if r.internal:
@ -22,15 +26,18 @@ class Default(Integration):
f = open(os.path.join(path, str(r.pk), 'recipe.json'), "w", encoding="utf-8")
f.write(self.get_export(r))
recipe_zip_obj.write(f.name)
recipe_zip_obj.write(r.image.path)
recipe_zip_obj.write(f.name, basename(f.name))
recipe_zip_obj.write(r.image.path, basename(r.image.path))
f.close()
recipe_zip_obj.close()
export_zip_obj.write(recipe_zip_obj.filename)
export_zip_obj.write(recipe_zip_obj.filename, basename(recipe_zip_obj.filename))
export_zip_obj.close()
return export_zip_obj.filename
response = HttpResponse(s.getvalue(), content_type='application/force-download')
response['Content-Disposition'] = 'attachment; filename="export.zip"'
return response
def get_recipe(self, string):
data = json.loads(string)

View File

@ -506,8 +506,7 @@ def test2(request):
form = NewExportForm(request.POST)
if form.is_valid():
integration = Default(request)
integration.do_export(form.cleaned_data['recipes'])
return render(request, 'test2.html', {'form': form})
return integration.do_export(form.cleaned_data['recipes'])
else:
form = NewExportForm()