2014-10-22 00:06:27 +03:00
|
|
|
from flask import current_app, render_template, request, redirect, Blueprint, flash, url_for
|
2014-08-20 18:28:25 +03:00
|
|
|
from realms.modules.auth.models import User
|
|
|
|
from realms.modules.auth.forms import LoginForm, RegistrationForm
|
2013-12-03 22:09:57 +02:00
|
|
|
|
2014-10-22 00:06:27 +03:00
|
|
|
blueprint = Blueprint('auth', __name__)
|
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")
|
2014-10-22 00:06:27 +03:00
|
|
|
return redirect(url_for(current_app.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-10-22 00:06:27 +03:00
|
|
|
return redirect(request.args.get("next") or url_for(current_app.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-09-14 06:47:17 +03:00
|
|
|
|
2014-10-22 00:06:27 +03:00
|
|
|
if not current_app.config['REGISTRATION_ENABLED']:
|
2014-09-14 06:47:17 +03:00
|
|
|
flash("Registration is disabled")
|
2014-10-22 00:06:27 +03:00
|
|
|
return redirect(url_for(current_app.config['ROOT_ENDPOINT']))
|
2014-09-14 06:47:17 +03:00
|
|
|
|
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-10-22 00:06:27 +03:00
|
|
|
return redirect(request.args.get("next") or url_for(current_app.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")
|
|
|
|
|
2014-09-14 06:47:17 +03:00
|
|
|
|
2014-08-30 18:06:12 +03:00
|
|
|
@blueprint.route("/logout")
|
|
|
|
def logout():
|
|
|
|
User.logout()
|
2014-11-17 19:25:26 +02:00
|
|
|
return redirect("/")
|