#!/usr/bin/python3
# -*- coding:utf-8 -*-

'''
Author    : sckn <seckinalan@gmail.com>
Desription: flask initialize module
'''

from flask import Flask, redirect
from flask.ext.babel import Babel
from flask.ext.login import LoginManager
from flask.ext.sqlalchemy import SQLAlchemy
import builtins
app = Flask(__name__)
app.config.from_object('config')

builtins.db = SQLAlchemy(app)
babel = Babel(app)
builtins.login_manager = LoginManager(app)


from .model import Login, Action
app.jinja_env.globals.update(shortAlist=Action.query.filter_by(shortList=True))
@login_manager.user_loader
def user_loader(user_id):
    user = Login.query.filter_by(id=user_id)
    if user.count() == 1:
        return user.one()
    return None

@login_manager.unauthorized_handler
def unauthorized_callback():
    return redirect('/auth/signin')

from app.modules.panel import panel
app.register_blueprint(panel)

from app.modules.auth import auth
app.register_blueprint(auth)

from app.modules.api import api
app.register_blueprint(api)

db.create_all()
