diff options
Diffstat (limited to 'utils.py')
-rw-r--r-- | utils.py | 27 |
1 files changed, 16 insertions, 11 deletions
@@ -1,15 +1,17 @@ # -*- coding: utf-8 -*- from functools import wraps -from flask import request, render_template, session +from flask import flash, request, redirect, render_template, session, url_for from random import randint from Crypto.Cipher import AES +from werkzeug.exceptions import Forbidden -# from http://flask.pocoo.org/docs/patterns/viewdecorators/#templating-decorator + +# using http://flask.pocoo.org/docs/patterns/viewdecorators/ def templated(template=None): - def decorator(f): + def templated_(f): @wraps(f) - def decorated_function(*args, **kwargs): + def templated__(*args, **kwargs): template_name = template if template_name is None: template_name = request.endpoint \ @@ -20,8 +22,16 @@ def templated(template=None): elif not isinstance(ctx, dict): return ctx return render_template(template_name, **ctx) - return decorated_function - return decorator + return templated__ + return templated_ + +def login_required(f): + @wraps(f) + def login_required_(*args, **kwargs): + if 'username' not in session: + raise Forbidden + return f(*args, **kwargs) + return login_required_ def login_user(username, password): @@ -64,10 +74,5 @@ def decrypt_password(ciphertext): return encryptor.decrypt(ciphertext[16:]).rstrip('\0') -def login_required(func): - #TODO - return func - - # circular import from app import app |