2013-09-25 20:46:28 +00:00
|
|
|
import logging
|
|
|
|
|
2013-10-01 17:48:37 +00:00
|
|
|
from flask import (abort, send_file, redirect, request, url_for,
|
2013-10-02 18:35:21 +00:00
|
|
|
render_template, make_response)
|
2013-09-27 00:18:36 +00:00
|
|
|
from flask.ext.login import login_user, UserMixin, login_required, logout_user
|
|
|
|
from flask.ext.principal import identity_changed, Identity, AnonymousIdentity
|
2013-09-25 16:45:12 +00:00
|
|
|
|
|
|
|
from data import model
|
2013-09-25 20:46:28 +00:00
|
|
|
from app import app, login_manager
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class _LoginWrappedDBUser(UserMixin):
|
|
|
|
def __init__(self, db_user):
|
|
|
|
self.db_user = db_user
|
|
|
|
|
|
|
|
def is_active(self):
|
|
|
|
return self.db_user.verified
|
|
|
|
|
|
|
|
def get_id(self):
|
|
|
|
return unicode(self.db_user.username)
|
|
|
|
|
|
|
|
|
|
|
|
@login_manager.user_loader
|
|
|
|
def load_user(username):
|
2013-10-01 19:43:16 +00:00
|
|
|
logger.debug('Loading user: %s' % username)
|
2013-09-25 20:46:28 +00:00
|
|
|
db_user = model.get_user(username)
|
|
|
|
if db_user:
|
|
|
|
return _LoginWrappedDBUser(db_user)
|
|
|
|
else:
|
|
|
|
return None
|
2013-09-25 16:45:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/', methods=['GET'])
|
|
|
|
def index():
|
|
|
|
return send_file('templates/index.html')
|
|
|
|
|
|
|
|
|
2013-10-02 18:35:21 +00:00
|
|
|
@app.route('/status', methods=['GET'])
|
|
|
|
def status():
|
|
|
|
return make_response('Healthy')
|
|
|
|
|
|
|
|
|
2013-10-01 21:44:13 +00:00
|
|
|
@app.route('/tos', methods=['GET'])
|
|
|
|
def tos():
|
|
|
|
return send_file('templates/tos.html')
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/privacy', methods=['GET'])
|
|
|
|
def privacy():
|
|
|
|
return send_file('templates/privacy.html')
|
|
|
|
|
|
|
|
|
2013-09-27 23:55:04 +00:00
|
|
|
def common_login(db_user):
|
2013-10-01 19:43:16 +00:00
|
|
|
if login_user(_LoginWrappedDBUser(db_user)):
|
|
|
|
logger.debug('Successfully signed in as: %s' % db_user.username)
|
|
|
|
identity_changed.send(app,
|
|
|
|
identity=Identity(db_user.username, 'username'))
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
logger.debug('User could not be logged in, inactive?.');
|
|
|
|
return False
|
2013-09-27 23:55:04 +00:00
|
|
|
|
|
|
|
|
2013-10-01 17:48:37 +00:00
|
|
|
@app.route('/signin', methods=['GET'])
|
|
|
|
def render_signin_page():
|
|
|
|
return render_template('signin.html')
|
|
|
|
|
|
|
|
|
2013-09-25 16:45:12 +00:00
|
|
|
@app.route('/signin', methods=['POST'])
|
|
|
|
def signin():
|
|
|
|
username = request.form['username']
|
|
|
|
password = request.form['password']
|
|
|
|
|
|
|
|
#TODO Allow email login
|
|
|
|
verified = model.verify_user(username, password)
|
|
|
|
if verified:
|
2013-10-01 19:43:16 +00:00
|
|
|
if common_login(verified):
|
|
|
|
return redirect(request.args.get('next') or url_for('index'))
|
|
|
|
else:
|
|
|
|
return render_template('signin.html',
|
|
|
|
needs_email_verification=True)
|
2013-09-25 16:45:12 +00:00
|
|
|
|
2013-10-01 17:48:37 +00:00
|
|
|
else:
|
|
|
|
return render_template('signin.html',
|
|
|
|
username=username,
|
2013-10-01 19:43:16 +00:00
|
|
|
invalid_credentials=True)
|
2013-09-25 16:45:12 +00:00
|
|
|
|
|
|
|
|
2013-09-27 23:29:01 +00:00
|
|
|
@app.route('/confirm', methods=['GET'])
|
|
|
|
def confirm_email():
|
2013-09-27 23:55:04 +00:00
|
|
|
code = request.values['code']
|
|
|
|
user = model.confirm_user_email(code)
|
|
|
|
|
|
|
|
common_login(user)
|
|
|
|
|
|
|
|
return redirect(url_for('index'))
|
2013-09-27 23:29:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/reset', methods=['GET'])
|
|
|
|
def password_reset():
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2013-09-27 00:18:36 +00:00
|
|
|
@app.route("/signout")
|
|
|
|
@login_required
|
|
|
|
def logout():
|
|
|
|
logout_user()
|
|
|
|
|
|
|
|
identity_changed.send(app, identity=AnonymousIdentity())
|
|
|
|
|
|
|
|
return redirect(url_for('index'))
|