summaryrefslogtreecommitdiff
path: root/src/jwebmail/view.py
blob: d4356865548189cf5661d9e34dd51681dc636a02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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(f"{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(f"{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))