-
Notifications
You must be signed in to change notification settings - Fork 0
/
decorators.py
89 lines (68 loc) · 2.52 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
# BSD license
# Written by Santiego([email protected])
from functools import wraps
from flask import flash, redirect, session, url_for, request, g, abort, jsonify
from config import MESSAGE_BOX_CN, USER_CODE
import urllib
import datetime
def login_required(func):
@wraps(func)
def wrapper(*args, **kwargs):
if session.get('user_id'):
return func(*args, **kwargs)
else:
flash(MESSAGE_BOX_CN['login_required'])
return redirect(url_for('login', from_=urllib.quote(request.url)))
return wrapper
def login_required_ajax(func):
@wraps(func)
def wrapper(*args, **kwargs):
if session.get('user_id'):
return func(*args, **kwargs)
else:
return jsonify({'state': 'LOGIN_REQUIRED'})
return wrapper
def login_required_only_post(func):
@wraps(func)
def wrapper(*args, **kwargs):
if session.get('user_id') or request.method == 'GET':
return func(*args, **kwargs)
else:
flash(MESSAGE_BOX_CN['login_required'])
return redirect(url_for('login', from_=urllib.quote(request.url)))
return wrapper
def confirm_required(func):
@wraps(func)
def wrapper(*args, **kwargs):
if session.get('user_id') is None:
flash(MESSAGE_BOX_CN['login_required'])
return redirect(url_for('login', from_=urllib.quote(request.url)))
if not g.user.confirmed:
flash(MESSAGE_BOX_CN['email_not_pass'])
return redirect(url_for('view_confirm'))
return func(*args, **kwargs)
return wrapper
def email_protected(func):
@wraps(func)
def wrapper(*args, **kwargs):
if session.get('email_protected') is None:
session['email_protected'] = datetime.datetime.now()
session.permanent = True
return func(*args, **kwargs)
else:
if (datetime.datetime.now()-session.get('email_protected')).seconds <= 60:
flash(MESSAGE_BOX_CN['email_too_frequently_fail'])
return redirect(request.referrer) or redirect(url_for('index'))
return func(*args, **kwargs)
return wrapper
def admin_required(func):
@wraps(func)
def wrapper(*args, **kwargs):
if session.get('user_id') and \
(g.user.role.type == USER_CODE['admin'] or g.user.role.type == USER_CODE['root']):
return func(*args, **kwargs)
else:
abort(403)
return wrapper
# BSD license
# Written by Santiego([email protected])