From 0519d1fbeaaa15cddff299db09487c3cbbbe900f Mon Sep 17 00:00:00 2001 From: terorie Date: Fri, 14 Dec 2018 19:58:56 +0100 Subject: [PATCH] Proper downloads page --- .gitignore | 4 +++- app.py | 29 +++++++++++++++++++++++------ static/downloads/README.md | 1 + templates/downloads.html | 15 ++++++++++----- 4 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 static/downloads/README.md diff --git a/.gitignore b/.gitignore index 62c8935..547dce1 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -.idea/ \ No newline at end of file +.idea/ +/static/downloads/ +!/static/downloads/README.md \ No newline at end of file diff --git a/app.py b/app.py index 8eee6b5..11b981a 100644 --- a/app.py +++ b/app.py @@ -82,13 +82,30 @@ def from_timestamp(value): @app.route("/dl") @cache.cached(120) def downloads(): - try: - export_file_stats = os.stat("static/out.csv.lzm4") - except FileNotFoundError: - logger.warning("No export file to display in /dl") - export_file_stats = None + # Get content of downloads directory + dl_dir = "static/downloads/" + dir_content = os.listdir(dl_dir) - return render_template("downloads.html", export_file_stats=export_file_stats) + # Make paths relative to working directory + # Only allow csv files + files = [ + (name, os.path.join(dl_dir, name)) + for name in dir_content + if name.find(".csv") != -1 + ] + + # Stat files + # Remove any dirs placed accidentally + files = [ + (f, full, os.stat(full)) + for f, full in files + if os.path.isfile(full) + ] + + if len(files) == 0: + logger.warning("No export file to display in /dl") + + return render_template("downloads.html", export_file_stats=files) @app.route("/stats") diff --git a/static/downloads/README.md b/static/downloads/README.md new file mode 100644 index 0000000..eba6963 --- /dev/null +++ b/static/downloads/README.md @@ -0,0 +1 @@ +CSV exports of the database will be available here. \ No newline at end of file diff --git a/templates/downloads.html b/templates/downloads.html index f883014..6ee3c77 100644 --- a/templates/downloads.html +++ b/templates/downloads.html @@ -11,6 +11,10 @@

Please let me know if you used the database in a project!

The entire database is exported to CSV regularly

+ {% if not export_file_stats %} +
+

No files available.

+ {% else %} @@ -22,16 +26,17 @@ - {% if export_file_stats %} + {% for name, path, stat in export_file_stats %} - - - + + + - {% endif %} + {% endfor %}
out.csv.xz{{ export_file_stats.st_size |filesizeformat }}{{ export_file_stats.st_mtime|datetime_format }} UTC{{ name }}{{ stat.st_size |filesizeformat }}{{ stat.st_mtime|datetime_format }} UTC
+ {% endif %}