-
Notifications
You must be signed in to change notification settings - Fork 0
/
req.py
93 lines (70 loc) · 2.43 KB
/
req.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import json
import msgpack
import types
import datetime
import collections
import tornado.template
import tornado.gen
import tornado.web
import tornado.websocket
class Service:
pass
class RequestHandler(tornado.web.RequestHandler):
def __init__(self,*args,**kwargs):
self.db = kwargs.pop('db')
self.rs = kwargs.pop('rs')
self.ars = kwargs.pop('ars')
super().__init__(*args,**kwargs)
try:
self.get_argument('json')
self.res_json = True
except tornado.web.HTTPError:
self.res_json = False
def error(self,err):
self.finish(err)
return
def render(self,templ,**kwargs):
from user import UserConst
class _encoder(json.JSONEncoder):
def default(self,obj):
if isinstance(obj,datetime.datetime):
return obj.isoformat()
else:
return json.JSONEncoder.default(self,obj)
def _mp_encoder(obj):
if isinstance(obj,datetime.datetime):
return obj.isoformat()
return obj
if self.acct['acct_id'] != UserConst.ACCTID_GUEST:
kwargs['acct_id'] = self.acct['acct_id']
else:
kwargs['acct_id'] = ''
if self.res_json == True:
self.finish(json.dumps(kwargs,cls = _encoder))
else:
key = 'render@%d-%d'%(
hash(msgpack.packb(kwargs,default = _mp_encoder)),
hash(self.request.path))
data = self.rs.get(key)
if data == None or True:
tpldr = tornado.template.Loader('templ')
data = tpldr.load(templ + '.templ').generate(**kwargs)
self.rs.set(key,data,datetime.timedelta(hours = 24))
self.finish(data)
return
class WebSocketHandler(tornado.websocket.WebSocketHandler):
def __init__(self,*args,**kwargs):
self.db = kwargs.pop('db')
self.rs = kwargs.pop('rs')
self.ars = kwargs.pop('ars')
super().__init__(*args,**kwargs)
def reqenv(func):
@tornado.gen.coroutine
def wrap(self,*args,**kwargs):
err,acct_id,ip = yield from Service.Acct.info_sign(self)
err,self.acct = yield from Service.Acct.info_acct(acct_id)
ret = func(self,*args,**kwargs)
if isinstance(ret,types.GeneratorType):
ret = yield from ret
return ret
return wrap