<filename>app/__init__.py
# -*- coding: utf-8 -*-
# coding=utf-8
__author__ = '<EMAIL>'

# coding=utf-8
from flask import Flask
from flask.json import JSONEncoder
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from tornado.options import options, define, parse_command_line
from flask_adminlte import AdminLTE
from flask_babel import Babel, lazy_gettext


app = Flask(__name__)
define('port', type=int, default=80)
define('production', type=bool, default=False)
define('debug', type=bool, default=True)
parse_command_line()

app.debug = options.debug

app.config.from_object('config_web')
db = SQLAlchemy(app)
lm = LoginManager()
lm.init_app(app)
lm.login_view = 'login'
lm.login_message = lazy_gettext(u'请登录.')
babel = Babel(app)


AdminLTE(app)

class CustomJSONEncoder(JSONEncoder):
    """This class adds support for lazy translation texts to Flask's
    JSON encoder. This is necessary when flashing translated texts."""
    def default(self, obj):
        from speaklater import is_lazy_string
        if is_lazy_string(obj):
            try:
                return unicode(obj)  # python 2
            except NameError:
                return str(obj)  # python 3
        return super(CustomJSONEncoder, self).default(obj)

app.json_encoder = CustomJSONEncoder



if app.debug:
    import logging
    from logging.handlers import RotatingFileHandler
    file_handler = RotatingFileHandler('../tmp/web_log.log', 'a',
                                       1 * 1024 * 1024, 10)
    file_handler.setLevel(logging.INFO)
    file_handler.setFormatter(logging.Formatter(
        '%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'))
    app.logger.addHandler(file_handler)
    app.logger.setLevel(logging.INFO)
    app.logger.info('microblog startup')


# from app import rest , model ,view  #使rest.py文件中的路由生效 #TODO 还不清楚报错原因，多次引入了

from app import rest, model, view