Merge pull request #13 from terorie/proper-dl

Better downloads page
This commit is contained in:
Simon Fortier 2018-12-14 22:49:03 -05:00 committed by GitHub
commit 38dfb657ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 12 deletions

2
.gitignore vendored
View File

@ -1 +1,3 @@
.idea/ .idea/
/static/downloads/
!/static/downloads/README.md

29
app.py
View File

@ -82,13 +82,30 @@ def from_timestamp(value):
@app.route("/dl") @app.route("/dl")
@cache.cached(120) @cache.cached(120)
def downloads(): def downloads():
try: # Get content of downloads directory
export_file_stats = os.stat("static/out.csv.lzm4") dl_dir = "static/downloads/"
except FileNotFoundError: dir_content = os.listdir(dl_dir)
logger.warning("No export file to display in /dl")
export_file_stats = None
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") @app.route("/stats")

View File

@ -0,0 +1 @@
CSV exports of the database will be available here.

View File

@ -11,6 +11,10 @@
<p>Please let me know if you used the database in a project!</p> <p>Please let me know if you used the database in a project!</p>
<p>The entire database is exported to CSV regularly</p> <p>The entire database is exported to CSV regularly</p>
{% if not export_file_stats %}
<br/>
<p><em>No files available.</em></p>
{% else %}
<table class="table table-striped"> <table class="table table-striped">
<thead> <thead>
@ -22,16 +26,17 @@
</thead> </thead>
<tbody> <tbody>
{% if export_file_stats %} {% for name, path, stat in export_file_stats %}
<tr> <tr>
<td><a href="static/out.csv.xz">out.csv.xz</a></td> <td><a href="{{ path }}">{{ name }}</a></td>
<td>{{ export_file_stats.st_size |filesizeformat }}</td> <td>{{ stat.st_size |filesizeformat }}</td>
<td>{{ export_file_stats.st_mtime|datetime_format }} UTC</td> <td>{{ stat.st_mtime|datetime_format }} UTC</td>
</tr> </tr>
{% endif %} {% endfor %}
</tbody> </tbody>
</table> </table>
{% endif %}
</div> </div>
</div> </div>
</div> </div>