show filesize on user downloads page

- add column to user download page to display filesize
- adds a filter to display file sizes
- don't download the user downloads page from notifications ;)
This commit is contained in:
Hugh Rundle 2023-11-07 11:04:11 +11:00
parent 3f038b4d67
commit 282f7dd8d6
No known key found for this signature in database
GPG key ID: A7E35779918253F9
3 changed files with 26 additions and 2 deletions

View file

@ -11,5 +11,5 @@
{% block description %}
{% 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 %}

View file

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

View file

@ -125,3 +125,20 @@ def id_to_username(user_id):
value = f"{name}@{domain}"
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 ""