Use a temporary file for exports for more stable writing

This commit is contained in:
Raphael Michel
2022-10-05 12:26:36 +02:00
parent b7ec372ebc
commit 370e4eafc2

View File

@@ -19,10 +19,10 @@
# You should have received a copy of the GNU Affero General Public License along with this program. If not, see # You should have received a copy of the GNU Affero General Public License along with this program. If not, see
# <https://www.gnu.org/licenses/>. # <https://www.gnu.org/licenses/>.
# #
import tempfile
from typing import Any, Dict from typing import Any, Dict
from django.conf import settings from django.conf import settings
from django.core.files.base import ContentFile
from django.utils.timezone import override from django.utils.timezone import override
from django.utils.translation import gettext from django.utils.translation import gettext
@@ -61,14 +61,14 @@ def export(self, event: Event, fileid: str, provider: str, form_data: Dict[str,
continue continue
ex = response(event, event.organizer, set_progress) ex = response(event, event.organizer, set_progress)
if ex.identifier == provider: if ex.identifier == provider:
d = ex.render(form_data) with tempfile.TemporaryFile() as f:
if d is None: d = ex.render(form_data, output_file=f)
raise ExportError( if d is None:
gettext('Your export did not contain any data.') raise ExportError(
) gettext('Your export did not contain any data.')
file.filename, file.type, data = d )
file.file.save(cachedfile_name(file, file.filename), ContentFile(data)) file.filename, file.type, data = d
file.save() file.file.save(cachedfile_name(file, file.filename), f)
return file.pk return file.pk
@@ -129,12 +129,12 @@ def multiexport(self, organizer: Organizer, user: User, device: int, token: int,
gettext('You do not have sufficient permission to perform this export.') gettext('You do not have sufficient permission to perform this export.')
) )
d = ex.render(form_data) with tempfile.TemporaryFile() as f:
if d is None: d = ex.render(form_data, output_file=f)
raise ExportError( if d is None:
gettext('Your export did not contain any data.') raise ExportError(
) gettext('Your export did not contain any data.')
file.filename, file.type, data = d )
file.file.save(cachedfile_name(file, file.filename), ContentFile(data)) file.filename, file.type, data = d
file.save() file.file.save(cachedfile_name(file, file.filename), f)
return file.pk return file.pk