Make get_file_size robust against typing errors

This commit is contained in:
Adeodato Simó 2024-03-18 14:56:29 -03:00
parent 518da3b9cf
commit a6dc5bd13f
No known key found for this signature in database
GPG key ID: 98EF291323013F6E

View file

@ -130,10 +130,14 @@ def id_to_username(user_id):
@register.filter(name="get_file_size") @register.filter(name="get_file_size")
def get_file_size(raw_size): def get_file_size(nbytes):
"""display the size of a file in human readable terms""" """display the size of a file in human readable terms"""
try: try:
raw_size = float(nbytes)
except (ValueError, TypeError):
return repr(nbytes)
else:
if raw_size < 1024: if raw_size < 1024:
return f"{raw_size} bytes" return f"{raw_size} bytes"
if raw_size < 1024**2: if raw_size < 1024**2:
@ -142,10 +146,6 @@ def get_file_size(raw_size):
return f"{raw_size/1024**2:.2f} MB" return f"{raw_size/1024**2:.2f} MB"
return f"{raw_size/1024**3:.2f} GB" return f"{raw_size/1024**3:.2f} GB"
except Exception as error: # pylint: disable=broad-except
print(error)
return ""
@register.filter(name="get_user_permission") @register.filter(name="get_user_permission")
def get_user_permission(user): def get_user_permission(user):