microblog2/app/__init__.py

73 lines
2.5 KiB
Python
Raw Normal View History

2012-12-16 08:30:41 +00:00
import os
2012-12-16 08:24:07 +00:00
from flask import Flask
from flask.json import JSONEncoder
2012-12-16 08:29:49 +00:00
from flask.ext.sqlalchemy import SQLAlchemy
2012-12-16 08:30:41 +00:00
from flask.ext.login import LoginManager
from flask.ext.openid import OpenID
2012-12-16 08:36:04 +00:00
from flask.ext.mail import Mail
2013-02-01 05:48:20 +00:00
from flask.ext.babel import Babel, lazy_gettext
from config import basedir, ADMINS, MAIL_SERVER, MAIL_PORT, MAIL_USERNAME, \
MAIL_PASSWORD
from .momentjs import momentjs
2012-12-16 08:24:07 +00:00
app = Flask(__name__)
2012-12-16 08:28:52 +00:00
app.config.from_object('config')
2012-12-16 08:29:49 +00:00
db = SQLAlchemy(app)
2012-12-16 08:30:41 +00:00
lm = LoginManager()
lm.init_app(app)
2012-12-16 08:30:41 +00:00
lm.login_view = 'login'
2013-02-01 05:48:20 +00:00
lm.login_message = lazy_gettext('Please log in to access this page.')
2012-12-16 08:30:41 +00:00
oid = OpenID(app, os.path.join(basedir, 'tmp'))
2012-12-16 08:36:04 +00:00
mail = Mail(app)
2013-02-01 05:48:20 +00:00
babel = Babel(app)
2012-12-16 08:29:49 +00:00
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
2013-04-15 06:13:17 +00:00
if not app.debug and MAIL_SERVER != '':
2012-12-16 08:32:38 +00:00
import logging
from logging.handlers import SMTPHandler
credentials = None
if MAIL_USERNAME or MAIL_PASSWORD:
credentials = (MAIL_USERNAME, MAIL_PASSWORD)
mail_handler = SMTPHandler((MAIL_SERVER, MAIL_PORT),
'no-reply@' + MAIL_SERVER, ADMINS,
'microblog failure', credentials)
2012-12-16 08:32:38 +00:00
mail_handler.setLevel(logging.ERROR)
app.logger.addHandler(mail_handler)
2013-04-23 05:19:12 +00:00
if not app.debug and os.environ.get('HEROKU') is None:
2012-12-16 08:32:38 +00:00
import logging
from logging.handlers import RotatingFileHandler
file_handler = RotatingFileHandler('tmp/microblog.log', 'a',
1 * 1024 * 1024, 10)
2012-12-16 08:32:38 +00:00
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(logging.Formatter(
'%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'))
2012-12-16 08:32:38 +00:00
app.logger.addHandler(file_handler)
app.logger.setLevel(logging.INFO)
app.logger.info('microblog startup')
2013-04-23 05:19:12 +00:00
if os.environ.get('HEROKU') is not None:
import logging
stream_handler = logging.StreamHandler()
app.logger.addHandler(stream_handler)
app.logger.setLevel(logging.INFO)
app.logger.info('microblog startup')
2013-01-08 07:18:12 +00:00
app.jinja_env.globals['momentjs'] = momentjs
2012-12-16 08:29:49 +00:00
from app import views, models