summaryrefslogtreecommitdiff
path: root/src/jwebmail/view.py
diff options
context:
space:
mode:
authorJannis M. Hoffmann <jannis@fehcom.de>2023-12-03 19:22:12 +0100
committerJannis M. Hoffmann <jannis@fehcom.de>2023-12-03 19:22:12 +0100
commit2cf2a68bd1c25d8fe4f3126f40bd57982cc6b2a4 (patch)
treeb5c8ed0e1cfe8eac311829296a9aca062bb1abc1 /src/jwebmail/view.py
initial commit
Diffstat (limited to 'src/jwebmail/view.py')
-rw-r--r--src/jwebmail/view.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/jwebmail/view.py b/src/jwebmail/view.py
new file mode 100644
index 0000000..7983c81
--- /dev/null
+++ b/src/jwebmail/view.py
@@ -0,0 +1,46 @@
+from datetime import datetime
+from math import floor, log2, log10
+
+from markupsafe import Markup
+
+
+def print_sizes10(var):
+ i = floor(log10(var) / 3)
+ expo = i * 3
+
+ PREFIX = [
+ "Byte",
+ "kByte",
+ "MByte",
+ "GByte",
+ "TByte",
+ "PByte",
+ ]
+
+ return Markup("{}&nbsp;{}".format(round(var / (10**expo)), PREFIX[i]))
+
+
+def print_sizes2(var):
+ i = floor(log2(var) / 10)
+ expo = i * 10
+
+ PREFIX = [
+ "Byte",
+ "KiByte",
+ "MiByte",
+ "GiByte",
+ "TiByte",
+ "PiByte",
+ ]
+
+ return Markup("{}&nbsp;{}".format(round(var / (2**expo)), PREFIX[i]))
+
+
+def parse_iso_date(inp):
+ return datetime.fromisoformat(inp)
+
+
+def add_view_funcs(app):
+ app.jinja_env.filters["byte_size2"] = print_sizes2
+ app.jinja_env.filters["byte_size10"] = print_sizes10
+ app.context_processor(lambda: dict(parse_iso_date=parse_iso_date))