gitscope
https://dev.kronoz.win/r/marzneshin.git
app/templates/filters.py · v0.3.0-alpha8
Source
1 a47eb4c Dawsh 2y ago import os
2 a47eb4c Dawsh 2y ago from datetime import datetime
3 a47eb4c Dawsh 2y ago
4 a47eb4c Dawsh 2y ago import yaml
5 a47eb4c Dawsh 2y ago
6 a47eb4c Dawsh 2y ago from app.utils.system import readable_size
7 a47eb4c Dawsh 2y ago
8 a47eb4c Dawsh 2y ago
9 a47eb4c Dawsh 2y ago def to_yaml(obj):
10 a47eb4c Dawsh 2y ago if not obj:
11 a47eb4c Dawsh 2y ago return ""
12 a47eb4c Dawsh 2y ago
13 a47eb4c Dawsh 2y ago return yaml.dump(obj, allow_unicode=True, indent=2)
14 a47eb4c Dawsh 2y ago
15 a47eb4c Dawsh 2y ago
16 a47eb4c Dawsh 2y ago def exclude_keys(obj, *target_keys):
17 a47eb4c Dawsh 2y ago return {key: val for key, val in obj.items() if key not in target_keys}
18 a47eb4c Dawsh 2y ago
19 a47eb4c Dawsh 2y ago
20 a47eb4c Dawsh 2y ago def only_keys(obj, *target_keys):
21 a47eb4c Dawsh 2y ago return {key: val for key, val in obj.items() if key in target_keys}
22 a47eb4c Dawsh 2y ago
23 a47eb4c Dawsh 2y ago
24 a47eb4c Dawsh 2y ago def datetimeformat(dt):
25 a47eb4c Dawsh 2y ago if isinstance(dt, int):
26 a47eb4c Dawsh 2y ago dt = datetime.fromtimestamp(dt)
27 01cdf2e Dawsh 2y ago formatted_datetime = dt.strftime("%Y-%m-%d %H:%M:%S")
28 a47eb4c Dawsh 2y ago return formatted_datetime
29 a47eb4c Dawsh 2y ago
30 a47eb4c Dawsh 2y ago
31 a47eb4c Dawsh 2y ago def env_override(value, key):
32 a47eb4c Dawsh 2y ago return os.getenv(key, value)
33 a47eb4c Dawsh 2y ago
34 a47eb4c Dawsh 2y ago
35 a47eb4c Dawsh 2y ago CUSTOM_FILTERS = {
36 a47eb4c Dawsh 2y ago "yaml": to_yaml,
37 a47eb4c Dawsh 2y ago "except": exclude_keys,
38 a47eb4c Dawsh 2y ago "only": only_keys,
39 a47eb4c Dawsh 2y ago "datetime": datetimeformat,
40 01cdf2e Dawsh 2y ago "bytesformat": readable_size,
41 a47eb4c Dawsh 2y ago }