-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecorators.py
118 lines (100 loc) · 3.73 KB
/
decorators.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from functools import wraps
from flask import g, redirect, url_for, request, abort
from utils import json_error_unauthorized_access, json_error_invalid_request
from models import Article, Question, Topic
def login_required(next='/', json=False):
def wrapper(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if g.user is None:
if json:
return json_error_unauthorized_access()
else:
return redirect(url_for('site.page', name='login', next=next))
return f(*args, **kwargs)
return decorated_function
return wrapper
def check_permission(name, next='/', orCheck=None, andCheck=None):
def wrapper(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if g.user is None:
return redirect(url_for('site.page', name='login', next=next))
else:
has_permission = g.user.has_permission(name)
approve = has_permission
if orCheck is not None:
approve = approve or orCheck()
if andCheck is not None:
approve = approve and andCheck()
if not approve:
return json_error_unauthorized_access()
return f(*args, **kwargs)
return decorated_function
return wrapper
def get_article(json=False):
def wrapper(f):
@wraps(f)
def decorated_function(*args, **kwargs):
data = request.get_json()
# support both json and normal request
if not data:
data = request.args
# now kiss
data = dict(request.args.items() + kwargs.items())
article_id = data.get('id')
article_id = data.get('article_id', article_id)
article = Article.find_by_pk(article_id)
if not article:
if json:
return json_error_invalid_request()
else:
return abort(400)
g.article = article
return f(*args, **kwargs)
return decorated_function
return wrapper
def get_question(json=False):
def wrapper(f):
@wraps(f)
def decorated_function(*args, **kwargs):
data = request.get_json()
# support both json and normal request
if not data:
data = request.args
# now kiss
data = dict(request.args.items() + kwargs.items())
question_id = data.get('id')
question_id = data.get('question_id', question_id)
question = Question.find_by_pk(question_id)
if not question:
if json:
return json_error_invalid_request()
else:
return abort(400)
g.question = question
return f(*args, **kwargs)
return decorated_function
return wrapper
def get_topic(json=False):
def wrapper(f):
@wraps(f)
def decorated_function(*args, **kwargs):
data = request.get_json()
# support both json and normal request
if not data:
data = request.args
# now kiss
data = dict(request.args.items() + kwargs.items())
topic_id = data.get('id')
topic_id = data.get('topic_id', topic_id)
topic = Topic.find_by_pk(topic_id)
if not topic:
if json:
return json_error_invalid_request()
else:
return abort(400)
g.topic = topic
return f(*args, **kwargs)
return decorated_function
return wrapper