-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
56 lines (39 loc) · 1.25 KB
/
utils.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
import json
import random
import string
from functools import wraps
from tornado.web import HTTPError
def random_string(length=16):
return ''.join([random.choice(string.ascii_lowercase) for _ in range(length)])
def json_content(func):
@wraps(func)
def wrapped(self, *args, **kwargs):
result = func(self, *args, **kwargs)
self.add_header('Content-Type', 'application/json')
self.write(json.dumps(result, indent=4, ensure_ascii=False))
self.write('\n')
return wrapped
def wrap_with_success_value(func):
@wraps(func)
def wrapped(self, *args, **kwargs):
result = func(self, *args, **kwargs)
success = {'success': 1}
if result is None:
return success
else:
return {'success': 1, 'response': result}
return wrapped
def get_handler(func):
@wraps(func)
def wrapped(self, *args, **kwargs):
if not self.request.method == 'GET':
raise HTTPError(404)
return func(self, *args, **kwargs)
return wrapped
def post_handler(func):
@wraps(func)
def wrapped(self, *args, **kwargs):
if not self.request.method == 'POST':
raise HTTPError(404)
return func(self, *args, **kwargs)
return wrapped