diff --git a/partitioncloud/__init__.py b/partitioncloud/__init__.py index f3512bd..e313f98 100644 --- a/partitioncloud/__init__.py +++ b/partitioncloud/__init__.py @@ -82,9 +82,9 @@ def add_user(): return render_template("auth/register.html", albums=get_all_albums(), user=current_user) -# Automatically close db after each request @app.after_request def after_request(response): + """Automatically close db after each request""" if ('db' in g) and (g.db is not None): g.db.close() return response diff --git a/partitioncloud/modules/auth.py b/partitioncloud/modules/auth.py index ad41d89..ad2bc01 100644 --- a/partitioncloud/modules/auth.py +++ b/partitioncloud/modules/auth.py @@ -38,6 +38,19 @@ def login_required(view): return wrapped_view +def anon_required(view): + """View decorator that redirects authenticated users to the index.""" + + @functools.wraps(view) + def wrapped_view(**kwargs): + if g.user is not None: + return redirect(url_for("albums.index")) + + return view(**kwargs) + + return wrapped_view + + def admin_required(view): """View decorator that redirects anonymous users to the login page.""" @@ -72,6 +85,7 @@ def load_logged_in_user(): @bp.route("/register", methods=("GET", "POST")) +@anon_required def register(): """Register a new user. Validates that the username is not already taken. Hashes the @@ -115,6 +129,7 @@ def register(): @bp.route("/login", methods=("GET", "POST")) +@anon_required def login(): """Log in a registered user by adding the user id to the session.""" if request.method == "POST":