__author__ = 'RemiZOffAlex'
__email__ = 'remizoffalex@mail.ru'

import config
import logging

from flask import Flask
from logging.handlers import RotatingFileHandler

from myapp.core import config


app = Flask(__name__)
app.config.from_object(config.CONFIG)

app.config.from_envvar('FLASKR_SETTINGS', silent=True)

# Логирование
handler = RotatingFileHandler(
    config['LOG_FILE'],
    maxBytes=config['LOG_FILE_SIZE']*1024*1024,
    backupCount=1
)
handler.setLevel(logging.INFO)
formatter = logging.Formatter(config['LONG_LOG_FORMAT'])
handler.setFormatter(formatter)
app.logger.addHandler(handler)


from . import lib, models


@app.context_processor
def inject_data():
    result = {}

    result['STATIC'] = config['STATIC']
    result['ITEMS_ON_PAGE'] = config['ITEMS_ON_PAGE']

    result['user'] = None
    if lib.get_user():
        result['user'] = lib.get_user().as_dict()

    return result


@app.teardown_appcontext
def shutdown_session(exception=None):
    models.db_session.close_all()

# API
from . import ns_api # noqa F401

# Общее
from . import ns_common # noqa F401

from . import views # noqa F401
