This repository was archived by the owner on Mar 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.py
More file actions
90 lines (74 loc) · 2.98 KB
/
util.py
File metadata and controls
90 lines (74 loc) · 2.98 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import re
from flask_restful import reqparse
def jsonDict(success, msg, **kwargs):
"""
A helper function to regularize the response of ReSTful API
:param success: A boolean value to indicate the result.
:param msg: Error/Success message to send.
:param kwargs: Other necessary fields, like data/username...
:return: Structured JSON dict.
"""
r = kwargs
r['success'] = success
r['msg'] = msg
return r
def dict_filter(d):
"""
A helper function that filter the **_id** field from MongoDB doc recursively.
:param d: The MongoDB doc need to be filtered.
:return: A MongoDB doc with no **_id** field
"""
if isinstance(d, dict):
return {k: dict_filter(v) for k, v in d.items() if v is not None and k != '_id'}
elif isinstance(d, list):
return [dict_filter(x) for x in d]
else:
return d
def email_verify(func):
"""
A decorator function to verify whether EMail belongs to SUSTC.
:param func: A function with EMail parameter.
:return: Result of func
"""
def decorated_fun(self, *args, **kwargs):
pattern = r'''[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@((mail\.sustech\.edu\.cn)|(sustech\.edu\.cn))'''
if re.match(pattern, kwargs['email']) is None:
return jsonDict(False, '邮箱格式错误')
return func(self, *args, **kwargs)
return decorated_fun
def args(*args_list, require=None):
"""
Parser decorator, transform the JSON request into the arguments that API can understand, like list, bool, etc.
:param args_list: [Tuple(arg, type) | arg] A list contains optional arguments.
:param require: [Tuple(arg, type) | arg] A list contains necessary arguments.
:return: Result of func
"""
if require is None:
require = []
def args_decorator(func):
def decorated_func(self, *args, **kwargs):
self.parser = reqparse.RequestParser()
new_require = []
for arg in list(args_list) + require:
if isinstance(arg, tuple):
if arg[1] is list:
self.parser.add_argument(arg[0], action='append')
else:
self.parser.add_argument(arg[0], type=arg[1])
if arg in require:
new_require.append(arg[0])
else:
self.parser.add_argument(arg)
for k, v in self.parser.parse_args().items():
if kwargs.get(k) is not None:
continue
kwargs[k] = v
if k in new_require + require and v is None:
return jsonDict(False, f'缺少必须参数{k},请不要伪造请求。')
kwargs = dict_filter(kwargs)
try:
return dict_filter(func(self, *args, **kwargs))
except Exception as e:
return jsonDict(False, e.__str__())
return decorated_func
return args_decorator