Skip to content

Commit 118696f

Browse files
committed
Undo usage of tempfile feature requiring Python 3.11.
1 parent 74eed9a commit 118696f

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/gened/admin.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#
33
# SPDX-License-Identifier: AGPL-3.0-only
44

5+
import platform
56
from collections.abc import Callable, Iterator
67
from dataclasses import dataclass
78
from datetime import date
@@ -334,13 +335,24 @@ def main() -> str:
334335
@register_admin_link("Download DB", right=True)
335336
@bp.route("/get_db")
336337
def get_db_file() -> Response:
337-
db_backup_file = NamedTemporaryFile(delete_on_close=False)
338-
db_backup_file.close() # avoid double-open in the backup_db function (fails on Windows)
339-
backup_db(Path(db_backup_file.name))
340338
db_name = current_app.config['DATABASE_NAME']
341339
db_basename = Path(db_name).stem
342340
dl_name = f"{db_basename}_{date.today().strftime('%Y%m%d')}.db"
343-
return send_file(db_backup_file.name, mimetype='application/vnd.sqlite3', as_attachment=True, download_name=dl_name)
341+
342+
if platform.system() == "Windows":
343+
# Slightly unsafe way to do it, because the file may be written while
344+
# send_file is sending it. Temp file issues make it hard to do
345+
# otherwise on Windows, though, and no one should run a production
346+
# server for this on Windows, anyway.
347+
return send_file(current_app.config['DATABASE'],
348+
mimetype='application/vnd.sqlite3',
349+
as_attachment=True, download_name=dl_name)
350+
else:
351+
db_backup_file = NamedTemporaryFile()
352+
backup_db(Path(db_backup_file.name))
353+
return send_file(db_backup_file,
354+
mimetype='application/vnd.sqlite3',
355+
as_attachment=True, download_name=dl_name)
344356

345357

346358
@bp.route("/consumer/new")

0 commit comments

Comments
 (0)