export recipe without wrting files

This commit is contained in:
vabene1111 2021-02-07 14:01:37 +01:00
parent 3e7f96c0b8
commit 585c31490a
2 changed files with 11 additions and 20 deletions

View File

@ -14,28 +14,27 @@ from cookbook.serializer import RecipeExportSerializer
class Default(Integration):
def do_export(self, recipes):
path = self.get_tmp_dir_path()
s = BytesIO()
export_zip_obj = ZipFile(s, 'w')
export_zip_stream = BytesIO()
export_zip_obj = ZipFile(export_zip_stream, 'w')
for r in recipes:
if r.internal:
base_path = os.path.join(path, str(r.pk))
os.makedirs(base_path, exist_ok=True)
recipe_zip_obj = ZipFile(base_path + '.zip', 'w')
recipe_zip_stream = BytesIO()
recipe_zip_obj = ZipFile(recipe_zip_stream, 'w')
recipe_json_stream = StringIO()
recipe_json_stream.write(self.get_export(r))
recipe_zip_obj.writestr('recipe.json', recipe_json_stream.getvalue())
recipe_json_stream.close()
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, 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, basename(recipe_zip_obj.filename))
export_zip_obj.writestr(str(r.pk) + '.zip', recipe_zip_stream.getvalue())
export_zip_obj.close()
response = HttpResponse(s.getvalue(), content_type='application/force-download')
response = HttpResponse(export_zip_stream.getvalue(), content_type='application/force-download')
response['Content-Disposition'] = 'attachment; filename="export.zip"'
return response

View File

@ -26,11 +26,3 @@ class Integration:
return img_f
except:
return None
def get_tmp_dir_path(self):
path = os.path.join(tempfile.gettempdir(), 'recipe_io', str(self.request.user.pk))
os.makedirs(path, exist_ok=True)
return path
def delete_temp_dir_path(self):
os.remove(self.get_tmp_dir_path())