Commits

Wilson Xu committed 7c4198e

Updates.

Comments (0)

Files changed (2)

 
 from fbone import utils
 from fbone.models import User
-from fbone.config import DefaultConfig
+from fbone.config import DefaultConfig, APP_NAME
 from fbone.views import frontend, user, api
 from fbone.extensions import db, mail, cache, login_manager
 
 # For import *
 __all__ = ['create_app']
 
-DEFAULT_APP_NAME = 'fbone'
-
 DEFAULT_BLUEPRINTS = (
     frontend,
     user,
 
 
 def create_app(config=None, app_name=None, blueprints=None):
+    """Create a Flask app."""
+
     if app_name is None:
-        app_name = DEFAULT_APP_NAME
+        app_name = APP_NAME
     if blueprints is None:
         blueprints = DEFAULT_BLUEPRINTS
 
     configure_hook(app)
     configure_blueprints(app, blueprints)
     configure_extensions(app)
-    configure_login_manager(app)
     configure_logging(app)
     configure_template_filters(app)
-    configure_i18n(app)
     configure_error_handlers(app)
 
     return app
 
 
 def configure_app(app, config):
+    """Configure app from object, parameter and env."""
+
     app.config.from_object(DefaultConfig)
     if config is not None:
         app.config.from_object(config)
 
 
 def configure_extensions(app):
+    # sqlalchemy
     db.init_app(app)
+    # mail
     mail.init_app(app)
+    # cache
     cache.init_app(app)
 
+    # babel
+    babel = Babel(app)
+    @babel.localeselector
+    def get_locale():
+        accept_languages = app.config.get('ACCEPT_LANGUAGES')
+        return request.accept_languages.best_match(accept_languages)
 
-def configure_login_manager(app):
+    # login.
     login_manager.login_view = 'frontend.login'
     login_manager.refresh_view = 'frontend.reauth'
     @login_manager.user_loader
 
 
 def configure_blueprints(app, blueprints):
+    """Configure blueprints in views."""
+
     for blueprint in blueprints:
         app.register_blueprint(blueprint)
 
         return utils.pretty_date(value)
 
 
-def configure_i18n(app):
-    babel = Babel(app)
-
-    @babel.localeselector
-    def get_locale():
-        accept_languages = app.config.get('ACCEPT_LANGUAGES')
-        return request.accept_languages.best_match(accept_languages)
-
-
 def configure_logging(app):
-    """Add log file handler and email handler."""
+    """Configure file(info) and email(error) logging."""
 
     if app.debug or app.testing:
+        # skip debug and test mode.
         return
 
     import logging
     mail_handler = SMTPHandler(app.config['MAIL_SERVER'],
                                app.config['MAIL_USERNAME'],
                                ADMINS,
-                               '52hz Failed',
+                               'O_ops... Fbone failed!',
                                (app.config['MAIL_USERNAME'],
                                 app.config['MAIL_PASSWORD']))
     mail_handler.setLevel(logging.ERROR)
 # -*- coding: utf-8 -*-
 
+APP_NAME = 'fbone'
+
 class BaseConfig(object):
+
     DEBUG = False
     TESTING = False
-    SECRET_KEY = '\xa4D\x08\xd0\x9a\xcfQl_\\\xa3\xd3\xdf\xdd$\t\x88}\xe0\xd7\xe4Un\x8b'
+
+    # os.urandom(24)
+    SECRET_KEY = 'secret key'
 
 
 class DefaultConfig(BaseConfig):
+
     DEBUG = True
 
     SQLALCHEMY_ECHO = True
+    # Sqlite
     SQLALCHEMY_DATABASE_URI = 'sqlite:////tmp/fbone.db'
+    # Mysql: 'mysql://dbusername:dbpassword@dbhost/dbname'
 
     # To create log folder.
     # $ sudo mkdir -p /var/log/fbone
     CACHE_TYPE = 'simple'
     CACHE_DEFAULT_TIMEOUT = 60
 
-    DEFAULT_MAIL_SENDER = 'imwilsonxu@gmail.com'
+    # Email (Flask-email)
+    # https://bitbucket.org/danjac/flask-mail/issue/3/problem-with-gmails-smtp-server
+    MAIL_DEBUG = DEBUG
+    MAIL_SERVER = 'smtp.gmail.com'
+    MAIL_USE_TLS = True
+    MAIL_USE_SSL = False
+    MAIL_USERNAME = 'imwilsonxu'
+    MAIL_PASSWORD = 'Nicorobin'
+    DEFAULT_MAIL_SENDER = '%s@gmail.com' % MAIL_USERNAME
 
 
 class TestConfig(BaseConfig):