mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-25 11:01:12 +00:00
Make get_file_size
robust against typing errors
This commit is contained in:
parent
518da3b9cf
commit
a6dc5bd13f
1 changed files with 5 additions and 5 deletions
|
@ -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):
|
||||||
|
|
Loading…
Reference in a new issue