2017-09-15 07:03:20 +00:00
|
|
|
import logging
|
|
|
|
from logging.handlers import SMTPHandler, RotatingFileHandler
|
|
|
|
import os
|
2017-09-30 07:21:17 +00:00
|
|
|
from flask import Flask, request
|
2017-09-11 21:30:21 +00:00
|
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
|
|
from flask_migrate import Migrate
|
2017-09-13 06:31:39 +00:00
|
|
|
from flask_login import LoginManager
|
2017-09-26 07:16:46 +00:00
|
|
|
from flask_mail import Mail
|
2017-09-27 06:43:28 +00:00
|
|
|
from flask_bootstrap import Bootstrap
|
2017-09-29 05:28:20 +00:00
|
|
|
from flask_moment import Moment
|
2017-09-30 07:21:17 +00:00
|
|
|
from flask_babel import Babel, lazy_gettext as _l
|
2017-09-05 07:04:56 +00:00
|
|
|
from config import Config
|
2017-09-05 06:23:07 +00:00
|
|
|
|
|
|
|
app = Flask(__name__)
|
2017-09-05 07:04:56 +00:00
|
|
|
app.config.from_object(Config)
|
2017-09-11 21:30:21 +00:00
|
|
|
db = SQLAlchemy(app)
|
|
|
|
migrate = Migrate(app, db)
|
2017-09-13 06:31:39 +00:00
|
|
|
login = LoginManager(app)
|
|
|
|
login.login_view = 'login'
|
2017-09-30 07:21:17 +00:00
|
|
|
login.login_message = _l('Please log in to access this page.')
|
2017-09-26 07:16:46 +00:00
|
|
|
mail = Mail(app)
|
2017-09-27 06:43:28 +00:00
|
|
|
bootstrap = Bootstrap(app)
|
2017-09-29 05:28:20 +00:00
|
|
|
moment = Moment(app)
|
2017-09-30 07:21:17 +00:00
|
|
|
babel = Babel(app)
|
2017-09-05 06:23:07 +00:00
|
|
|
|
2017-09-15 07:03:20 +00:00
|
|
|
if not app.debug:
|
|
|
|
if app.config['MAIL_SERVER']:
|
|
|
|
auth = None
|
|
|
|
if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWORD']:
|
|
|
|
auth = (app.config['MAIL_USERNAME'], app.config['MAIL_PASSWORD'])
|
|
|
|
secure = None
|
|
|
|
if app.config['MAIL_USE_TLS']:
|
|
|
|
secure = ()
|
|
|
|
mail_handler = SMTPHandler(
|
|
|
|
mailhost=(app.config['MAIL_SERVER'], app.config['MAIL_PORT']),
|
|
|
|
fromaddr='no-reply@' + app.config['MAIL_SERVER'],
|
|
|
|
toaddrs=app.config['ADMINS'], subject='Microblog Failure',
|
|
|
|
credentials=auth, secure=secure)
|
|
|
|
mail_handler.setLevel(logging.ERROR)
|
|
|
|
app.logger.addHandler(mail_handler)
|
|
|
|
|
|
|
|
if not os.path.exists('logs'):
|
|
|
|
os.mkdir('logs')
|
|
|
|
file_handler = RotatingFileHandler('logs/microblog.log', maxBytes=10240,
|
|
|
|
backupCount=10)
|
|
|
|
file_handler.setFormatter(logging.Formatter(
|
|
|
|
'%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'))
|
|
|
|
file_handler.setLevel(logging.INFO)
|
|
|
|
app.logger.addHandler(file_handler)
|
|
|
|
|
|
|
|
app.logger.setLevel(logging.INFO)
|
|
|
|
app.logger.info('Microblog startup')
|
|
|
|
|
2017-09-30 07:21:17 +00:00
|
|
|
|
|
|
|
@babel.localeselector
|
|
|
|
def get_locale():
|
|
|
|
return request.accept_languages.best_match(app.config['LANGUAGES'])
|
|
|
|
|
|
|
|
|
2017-09-15 07:03:20 +00:00
|
|
|
from app import routes, models, errors
|