from flask import Blueprint, render_template, redirect, g, url_for, flash
from forms.login import LoginForm
from ... import app, db, login_manager
from flask_login import login_user, logout_user, current_user
from ...lib.models import *


session = Blueprint('session', __name__, template_folder='templates', static_url_path='/static', static_folder='../../views/static')

@login_manager.user_loader
def load_user(id):
    return User.query.get(int(id))

@app.before_first_request
def setup_db():
    # make sure user exists
    user = User.query.filter_by(username='root').first()
    if user is None:
        user = User('root','toor')
        db.session.add(user)
        db.session.commit()

@app.before_request
def add_session_user():
    g.user = current_user

@session.route('/login.html',methods=['GET','POST'])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        username = form.username.data.lower()
        password = form.password.data
        remember_me = form.remember_me.data
        registered_user = User.query.filter_by(username=username).first()
        if registered_user is None or registered_user.check_password(password) != True:
            flash('Username or Password is invalid' , 'danger')
            return render_template('login.html',form=form)
        else:
            login_user(registered_user, remember=remember_me)
            if password == "<PASSWORD>":
                return redirect(url_for('system.passwords'))
            flash('Logged in successfully','success')
            return redirect(request.args.get('next') or url_for('map.live'))
    return render_template('login.html',form=form)

@session.route('/logout.html')
def logout():
    logout_user()
    return redirect(url_for('session.login'))

from flask import Blueprint, render_template, redirect, g, url_for, flash
from forms.login import LoginForm
from ... import app, db, login_manager
from flask_login import login_user, logout_user, current_user
from ...lib.models import *


session = Blueprint('session', __name__, template_folder='templates', static_url_path='/static', static_folder='../../views/static')

@login_manager.user_loader
def load_user(id):
    return User.query.get(int(id))

@app.before_first_request
def setup_db():
    # make sure user exists
    user = User.query.filter_by(username='root').first()
    if user is None:
        user = User('root','toor')
        db.session.add(user)
        db.session.commit()

@app.before_request
def add_session_user():
    g.user = current_user

@session.route('/login.html',methods=['GET','POST'])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        username = form.username.data.lower()
        password = form.password.data
        remember_me = form.remember_me.data
        registered_user = User.query.filter_by(username=username).first()
        if registered_user is None or registered_user.check_password(password) != True:
            flash('Username or Password is invalid' , 'danger')
            return render_template('login.html',form=form)
        else:
            login_user(registered_user, remember=remember_me)
            if password == "<PASSWORD>":
                return redirect(url_for('system.passwords'))
            flash('Logged in successfully','success')
            return redirect(request.args.get('next') or url_for('map.live'))
    return render_template('login.html',form=form)

@session.route('/logout.html')
def logout():
    logout_user()
    return redirect(url_for('session.login'))

# make sure user exists