-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
73 lines (64 loc) · 2.27 KB
/
util.py
File metadata and controls
73 lines (64 loc) · 2.27 KB
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
# -*- coding: utf-8 -*-
import time
import hashlib
import uuid
import functools
import urlparse
import urllib
from tornado.web import HTTPError
def truncate(data,length=300,append="…"):
return data[:length] + append if len(data) > length else data
def fetch_str(strObj):
if strObj == None:
return ''
else :
return strObj
def now():
return long(time.time()*1000)
def timeformat(ctime,long_form=False):
if not ctime:
return 'no time'
ctime = int(ctime)
if long_form :
return time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(ctime/1000))
return time.strftime('%Y-%m-%d',time.localtime(ctime/1000))
def makepwd(pwd):
salt = str(uuid.uuid4()).replace('-','')
return salt+'$'+ str(hashlib.sha1(pwd+salt).hexdigest())
def validpwd(ipwd,pwd):
[salt,cryptograph] = pwd.split('$')
return hashlib.sha1(ipwd+salt).hexdigest() == cryptograph
def admin_authenticated(method):
"""装饰必须要使用admin身份访问的方法 ."""
@functools.wraps(method)
def wrapper(self, *args, **kwargs):
if not self.current_user or (self.current_user.mtype != 'admin' and self.current_user.mtype != 'superadmin'):
if self.request.method in ("GET", "HEAD"):
url = self.get_login_url()
if "?" not in url:
if urlparse.urlsplit(url).scheme:
# if login url is absolute, make next absolute too
next_url = self.request.full_url()
else:
next_url = self.request.uri
url += "?" + urllib.urlencode(dict(next=next_url))
self.redirect(url)
return
raise HTTPError(403)
else :
return method(self, *args, **kwargs)
return wrapper
def not_authenticated(method):
"""装饰必须要在未登录情况下访问的方法 ."""
@functools.wraps(method)
def wrapper(self, *args, **kwargs):
if self.current_user :
self.json_write('4')
self.redirect('/home')
return
else :
return method(self, *args, **kwargs)
return wrapper
#常见正则表达式
class REGEX():
url = '^[a-zA-Z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\s*)?$'