from functools import wraps
from flask import Markup, flash, url_for, redirect, abort
from flask_login import current_user
def confirm_required(func):
@wraps(func)
def decorated_function(*args, *kwargs):
if not current_user.confirmed:
message = Markup(
'Please confirm your account first.'
'Not receive the email?'
'<a class="alert-link" href="%s">Resend Confirm Email</a>' %
url_for('auth.resend_confirm_email'))
flash(message, 'warning')
return redirect(url_for('main.index'))
return func(args, **kwargs)
return decorated_function
如果用户没有邮箱确认,则需要重新确认
def permission_required(permission_name):
def decorator(func):
@wraps(func)
def decorated_function(*args, *kwargs):
if not current_user.can(permission_name):
abort(403)
return func(args, **kwargs)
return decorated_function
return decorator
权限鉴定
def admin_required(func):
return permission_required('ADMINISTER')(func)