Simplify logic for rendering user exports

This commit is contained in:
Bart Schuurmans 2024-03-25 18:13:09 +01:00
parent a51402241b
commit f721289b1d
2 changed files with 11 additions and 21 deletions

View file

@ -123,7 +123,9 @@
</span>
</td>
<td>
{% if export.size %}
<span>{{ export.size|get_file_size }}</span>
{% endif %}
</td>
<td>
{% if export.url %}
@ -133,7 +135,7 @@
{% trans "Download your export" %}
</span>
</a>
{% elif export.job.complete and not export.job.status == "stopped" and not export.job.status == "failed" %}
{% elif export.unavailable %}
{% trans "Archive is no longer available" %}
{% endif %}
</td>

View file

@ -165,28 +165,16 @@ class ExportUser(View):
for job in jobs:
export = {"job": job}
if settings.USE_S3:
storage = S3Boto3Storage(querystring_auth=True, custom_domain=None)
# for s3 we create a new tar file in s3,
# so we need to check the size of _that_ file
if job.export_data:
try:
export["size"] = S3Boto3Storage.size(
storage, f"exports/{job.task_id}.tar.gz"
)
except Exception: # pylint: disable=broad-except
export["size"] = 0
else:
# for local storage export_data is the tar file
try:
export["size"] = job.export_data.size if job.export_data else 0
export["size"] = job.export_data.size
export["url"] = reverse("prefs-export-file", args=[job.task_id])
except FileNotFoundError:
# file no longer exists
export["size"] = 0
if export["size"] > 0:
export["url"] = reverse("prefs-export-file", args=[job.task_id])
# file no longer exists locally
export["unavailable"] = True
except Exception: # pylint: disable=broad-except
# file no longer exists on storage backend
export["unavailable"] = True
exports.append(export)