basic export to zip working
This commit is contained in:
parent
d45adc1688
commit
3e7f96c0b8
@ -1,7 +1,10 @@
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
from io import StringIO, BytesIO
|
||||||
|
from os.path import basename
|
||||||
from zipfile import ZipFile
|
from zipfile import ZipFile
|
||||||
|
|
||||||
|
from django.http import HttpResponse
|
||||||
from rest_framework.renderers import JSONRenderer
|
from rest_framework.renderers import JSONRenderer
|
||||||
|
|
||||||
from cookbook.integration.integration import Integration
|
from cookbook.integration.integration import Integration
|
||||||
@ -12,7 +15,8 @@ class Default(Integration):
|
|||||||
|
|
||||||
def do_export(self, recipes):
|
def do_export(self, recipes):
|
||||||
path = self.get_tmp_dir_path()
|
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:
|
for r in recipes:
|
||||||
if r.internal:
|
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 = open(os.path.join(path, str(r.pk), 'recipe.json'), "w", encoding="utf-8")
|
||||||
f.write(self.get_export(r))
|
f.write(self.get_export(r))
|
||||||
recipe_zip_obj.write(f.name)
|
recipe_zip_obj.write(f.name, basename(f.name))
|
||||||
recipe_zip_obj.write(r.image.path)
|
recipe_zip_obj.write(r.image.path, basename(r.image.path))
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
recipe_zip_obj.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()
|
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):
|
def get_recipe(self, string):
|
||||||
data = json.loads(string)
|
data = json.loads(string)
|
||||||
|
@ -506,8 +506,7 @@ def test2(request):
|
|||||||
form = NewExportForm(request.POST)
|
form = NewExportForm(request.POST)
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
integration = Default(request)
|
integration = Default(request)
|
||||||
integration.do_export(form.cleaned_data['recipes'])
|
return integration.do_export(form.cleaned_data['recipes'])
|
||||||
return render(request, 'test2.html', {'form': form})
|
|
||||||
else:
|
else:
|
||||||
form = NewExportForm()
|
form = NewExportForm()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user