This repository has been archived by the owner on Jan 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplatetags.py
67 lines (55 loc) · 1.94 KB
/
templatetags.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from django import template
from django.template.defaulttags import register
import datetime
import settings
from apps.utils.utils import error_message_switch, t_log
import logging # only for log levels
#register = template.Library()
@register.filter
def print_timestamp(timestamp):
try:
#assume, that timestamp is given in seconds with decimal point
ts = float(timestamp)
except ValueError:
return None
return datetime.datetime.fromtimestamp(ts/1000.0)
#register.filter(print_timestamp)
@register.filter
def get_item(dictionary, key):
return dictionary.get(key)
@register.filter
def index(List, i):
return List[int(i)]
@register.filter
def coords_for_fimagestore(crop):
return str(crop.get('x'))+"x"+str(crop.get('y'))+"x"+str(crop.get('w'))+"x"+str(crop.get('h'))
@register.filter
def coords_add_commas(coords):
return coords.replace(" ",",")
@register.filter
def coords_for_imagemap(crop):
return str(str(crop.get('tl')[0])+","+str(crop.get('tl')[1])+","+
str(crop.get('tr')[0])+","+str(crop.get('tr')[1])+","+
str(crop.get('br')[0])+","+str(crop.get('br')[1])+","+
str(crop.get('bl')[0])+","+str(crop.get('bl')[1]))
@register.filter
def y_for_typewriterline(crop):
return str(crop.get('tl')[1])
@register.filter
def load_lib(lib):
if settings.USE_CDNS and lib in settings.CDNS:
return str(settings.CDNS.get(lib).get('cdn'))
return str(settings.CDNS.get(lib).get('local'))
@register.filter
def login_error_message(code):
return error_message_switch(None,int(code))
#dates from transkribus are usually YYYY-MM-DD or similar YYYY-MM-DDTHH:MM:SS.SSS (2017-07-27T16:59:54.601)
@register.filter
def str_to_date(s):
try:
return datetime.datetime.strptime(s, "%Y-%m-%d").date()
except ValueError:
try:
return datetime.datetime.strptime(".".join(s.split('.')[:1]), "%Y-%m-%dT%H:%M:%S").date()
except ValueError:
return None