realms-wiki/realms/modules/auth/views.py

67 lines
2.1 KiB
Python
Raw Normal View History

2014-08-20 18:28:25 +03:00
from flask import g, render_template, request, redirect, Blueprint, flash, url_for
from realms.modules.auth.models import User
from realms.modules.auth.forms import LoginForm, RegistrationForm
from realms import config
2013-12-03 22:09:57 +02:00
2014-08-20 18:28:25 +03:00
blueprint = Blueprint('auth', __name__, url_prefix=config.RELATIVE_PATH)
2013-12-03 22:09:57 +02:00
2014-08-20 18:28:25 +03:00
@blueprint.route("/logout")
def logout_page():
2013-12-03 22:09:57 +02:00
User.logout()
2014-08-20 18:28:25 +03:00
flash("You are now logged out")
return redirect(url_for(config.ROOT_ENDPOINT))
2013-12-03 22:09:57 +02:00
2014-08-30 18:06:12 +03:00
@blueprint.route("/login", methods=['GET', 'POST'])
2014-08-20 18:28:25 +03:00
def login():
2014-08-30 18:06:12 +03:00
form = LoginForm()
2013-12-03 22:09:57 +02:00
2014-08-30 18:06:12 +03:00
if request.method == "POST":
2014-08-20 18:28:25 +03:00
if not form.validate():
2014-08-30 18:06:12 +03:00
flash('Form invalid', 'warning')
2014-08-20 18:28:25 +03:00
return redirect(url_for('auth.login'))
2013-12-03 22:09:57 +02:00
if User.auth(request.form['email'], request.form['password']):
2014-08-20 18:28:25 +03:00
return redirect(request.args.get("next") or url_for(config.ROOT_ENDPOINT))
2014-08-30 18:06:12 +03:00
else:
flash('Email or Password Incorrect', 'warning')
return redirect(url_for('auth.login'))
2014-08-20 18:28:25 +03:00
2014-08-30 18:06:12 +03:00
return render_template("auth/login.html", form=form)
2014-08-20 18:28:25 +03:00
2014-08-30 18:06:12 +03:00
@blueprint.route("/register", methods=['GET', 'POST'])
2014-08-20 18:28:25 +03:00
def register():
2014-08-30 18:06:12 +03:00
form = RegistrationForm()
2014-08-20 18:28:25 +03:00
if request.method == "POST":
2014-08-30 18:06:12 +03:00
if not form.validate():
flash('Form invalid', 'warning')
return redirect(url_for('auth.register'))
if User.get_by_username(request.form['username']):
flash('Username is taken', 'warning')
return redirect(url_for('auth.register'))
if User.get_by_email(request.form['email']):
flash('Email is taken', 'warning')
return redirect(url_for('auth.register'))
User.create(request.form['username'], request.form['email'], request.form['password'])
User.auth(request.form['email'], request.form['password'])
2014-08-20 18:28:25 +03:00
return redirect(request.args.get("next") or url_for(config.ROOT_ENDPOINT))
2014-08-30 18:06:12 +03:00
return render_template("auth/register.html", form=form)
@blueprint.route("/settings", methods=['GET', 'POST'])
def settings():
return render_template("auth/settings.html")
@blueprint.route("/logout")
def logout():
User.logout()
return redirect("/")