Merge pull request #3091 from hughrun/notification-and-download-links

show filesize on user downloads page
This commit is contained in:
Mouse Reeve 2023-11-06 16:27:14 -08:00 committed by GitHub
commit 1b958a9b31
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 2 deletions

View file

@ -11,5 +11,5 @@
{% block description %} {% block description %}
{% url 'prefs-user-export' as url %} {% url 'prefs-user-export' as url %}
{% blocktrans %}Your <a download href="{{ url }}">user export</a> is ready.{% endblocktrans %} {% blocktrans %}Your <a href="{{ url }}">user export</a> is ready.{% endblocktrans %}
{% endblock %} {% endblock %}

View file

@ -1,5 +1,6 @@
{% extends 'preferences/layout.html' %} {% extends 'preferences/layout.html' %}
{% load i18n %} {% load i18n %}
{% load utilities %}
{% block title %}{% trans "User Export" %}{% endblock %} {% block title %}{% trans "User Export" %}{% endblock %}
@ -40,9 +41,12 @@
<th> <th>
{% trans "Date" %} {% trans "Date" %}
</th> </th>
<th colspan="2"> <th>
{% trans "Status" %} {% trans "Status" %}
</th> </th>
<th colspan="2">
{% trans "Size" %}
</th>
</tr> </tr>
{% if not jobs %} {% if not jobs %}
<tr> <tr>
@ -76,6 +80,9 @@
{% endif %} {% endif %}
</span> </span>
</td> </td>
<td>
<span>{{ job.export_data|get_file_size }}</span>
</td>
<td> <td>
{% if job.complete and not job.status == "stopped" and not job.status == "failed" %} {% if job.complete and not job.status == "stopped" and not job.status == "failed" %}
<p> <p>

View file

@ -125,3 +125,20 @@ def id_to_username(user_id):
value = f"{name}@{domain}" value = f"{name}@{domain}"
return value return value
@register.filter(name="get_file_size")
def get_file_size(file):
"""display the size of a file in human readable terms"""
try:
raw_size = os.stat(file.path).st_size
if raw_size < 1024:
return f"{raw_size} bytes"
if raw_size < 1024**2:
return f"{raw_size/1024:.2f} KB"
if raw_size < 1024**3:
return f"{raw_size/1024**2:.2f} MB"
return f"{raw_size/1024**3:.2f} GB"
except Exception: # pylint: disable=broad-except
return ""