-
Notifications
You must be signed in to change notification settings - Fork 0
/
uimethods.py
executable file
·74 lines (64 loc) · 1.85 KB
/
uimethods.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
68
69
70
71
72
73
74
#!/usr/bin/env python
# encoding: utf-8
"""
uimethods.py
Created by 刘 智勇 on 2011-09-24.
Copyright (c) 2011 __MyCompanyName__. All rights reserved.
"""
from tornado import template
#from huwai.apps.perm import PERM_CLASS
#from huwai.apps.tag import Tag
def ifNone(handler, v=None):
return v if v else ''
def truncate(handler, v, length=100):
return v[:length]
def verify(handler, perm, reference):
if isinstance(reference, int):
return perm == reference
elif isinstance(reference, list):
return (perm in reference)
elif isinstance(reference, dict):
s=reference.get('start', 0x00)
e=reference.get('end', 0x99)
return (s <= perm <= e)
return False
def list2txt(handler, v=None):
if isinstance(v, list):
if None in v:v.remove(None)
return ','.join(v)
elif isinstance(v, unicode) or isinstance(v, str):
return v
return ''
def dict2txt(handler, v=None):
if isinstance(v, dict):
return ''.join(v.values())
def list2url(handler, v=None):
if isinstance(v, list):
if None in v:v.remove(None)
l = []
for i, c in v:
href, txt = '/tag/'+i, c
l.append(u'<a class="badge badge-info" href="%s">%s</a>' % (href, txt))
return ' '.join(l)
elif isinstance(v, tuple):
href = '/tag/'+v[0]
txt = v[1]
return u'<a class="badge badge-info" href="%s">%s</a>' % (href, txt)
return ''
def cntDict(handler, l, **kwargs):
cnt = 0
for i in l:
plus = True
for k, v in kwargs.items():
if i.get(k, None) != v:
plus = False
if plus:cnt += 1
return cnt
def abstract(handler, c, n=100):
import re
s = re.sub(r'</?\w+[^>]*>','',c)
s = s.replace(' ', '')
if (len(s) > n):
return s[:n-3] + '...'
else:
return s[:n]