Anonymous avatar Anonymous committed 88649a3

config changes

Comments (0)

Files changed (3)

newsmeme/__init__.py

     :license: BSD, see LICENSE for more details.
 """
 
-from newsmeme.application import Application
+from newsmeme.application import create_app
 
-def create_app(config=None):
-
-    return Application(__name__, config).configure()
-
-

newsmeme/application.py

 from flaskext.themes import setup_themes
 from flaskext.principal import Principal, identity_loaded
 
-from newsmeme import views
 from newsmeme import helpers
+from newsmeme.config import DefaultConfig
 from newsmeme.models import User, Tag
 from newsmeme.helpers import render_template
 from newsmeme.extensions import db, mail, oid, cache
 
+__all__ = ["create_app"]
 
-class Application(object):
+DEFAULT_APP_NAME = "newsmeme"
 
-    DEBUG = True
+def create_app(config=None, app_name=None):
 
-    # change this in your production settings !!!
+    if app_name is None:
+        app_name = DEFAULT_APP_NAME
 
-    SECRET_KEY = "secret"
+    app = Flask(app_name)
 
-    # keys for localhost. Change as appropriate.
+    configure_app(app, config)
 
-    RECAPTCHA_PUBLIC_KEY = '6LeYIbsSAAAAACRPIllxA7wvXjIE411PfdB2gt2J'
-    RECAPTCHA_PRIVATE_KEY = '6LeYIbsSAAAAAJezaIq3Ft_hSTo0YtyeFG-JgRtu'
+    configure_logging(app)
+    configure_errorhandlers(app)
+    configure_extensions(app)
+    configure_before_handlers(app)
+    configure_filters(app)
+    configure_context_processors(app)
+    # configure_after_handlers(app)
+    configure_modules(app)
 
-    SQLALCHEMY_DATABASE_URI = "sqlite:///newsmeme.db"
+    return app
 
-    SQLALCHEMY_ECHO = False
 
-    MAIL_DEBUG = DEBUG
+def configure_app(app, config):
+    
+    app.config.from_object(DefaultConfig)
 
-    ADMINS = ()
+    if config is not None:
+        app.config.from_object(config)
 
-    DEFAULT_MAIL_SENDER = "support@thenewsmeme.com"
+    app.config.from_envvar('APP_CONFIG', silent=True)
 
-    ACCEPT_LANGUAGES = ['en', 'fi']
 
-    DEBUG_LOG = 'logs/debug.log'
-    ERROR_LOG = 'logs/error.log'
+def configure_modules(app):
+    
+    for module, url_prefix in app.config['MODULES']:
+        app.register_module(module, url_prefix=url_prefix)
 
-    THEME = 'newsmeme'
 
-    CACHE_TYPE = "simple"
-    CACHE_DEFAULT_TIMEOUT = 300
+def configure_filters(app):
 
-    MODULES = (
-        (views.frontend, ""),
-        (views.post, "/post"),
-        (views.user, "/user"),
-        (views.comment, "/comment"),
-        (views.account, "/acct"),
-        (views.feeds, "/feeds"),
-        (views.openid, "/openid"),
-        (views.api, "/api"),
-    )
+    @app.template_filter()
+    def timesince(value):
+        return helpers.timesince(value)
 
-    def __init__(self, app_name, config=None):
 
-        self.config = config
-        self.app = Flask(app_name)
+def configure_before_handlers(app):
 
-    def configure(self):
+    @app.before_request
+    def authenticate():
+        g.user = getattr(g.identity, 'user', None)
 
-        self.configure_app()
-        self.configure_logging()
-        self.configure_errorhandlers()
-        self.configure_extensions()
-        self.configure_before_handlers()
-        self.configure_filters()
-        self.configure_context_processors()
-        # self.configure_after_handlers()
-        self.configure_modules()
 
-        return self.app
+def configure_context_processors(app):
+
+    @app.context_processor
+    def get_tags():
+        tags = cache.get("tags")
+        if tags is None:
+            tags = Tag.query.order_by(Tag.num_posts.desc()).limit(10).all()
+            cache.set("tags", tags)
+
+        return dict(tags=tags)
+
+    @app.context_processor
+    def config():
+        return dict(config=app.config)
+
+
+def configure_extensions(app):
+
+    mail.init_app(app)
+    db.init_app(app)
+    oid.init_app(app)
+    cache.init_app(app)
+
+    setup_themes(app)
+
+    # more complicated setups
+
+    configure_identity(app)
+    configure_i18n(app)
     
-    def configure_app(self):
-        
-        self.app.config.from_object(self)
 
-        if self.config is not None:
-            self.app.config.from_object(self.config)
+def configure_identity(app):
 
-        self.app.config.from_envvar('APP_CONFIG', silent=True)
+    Principal(app)
 
-    def configure_modules(self):
-        
-        for module, url_prefix in self.app.config['MODULES']:
-            self.app.register_module(module, url_prefix=url_prefix)
+    @identity_loaded.connect_via(app)
+    def on_identity_loaded(sender, identity):
+        g.user = User.query.from_identity(identity)
 
-    def configure_filters(self):
 
-        @self.app.template_filter()
-        def timesince(value):
-            return helpers.timesince(value)
+def configure_i18n(app):
 
-    def configure_before_handlers(self):
+    babel = Babel(app)
 
-        @self.app.before_request
-        def authenticate():
-            g.user = getattr(g.identity, 'user', None)
+    @babel.localeselector
+    def get_locale():
+        accept_languages = app.config.get('ACCEPT_LANGUAGES', 
+                                               ['en_gb'])
 
-    def configure_context_processors(self):
+        return request.accept_languages.best_match(accept_languages)
 
-        @self.app.context_processor
-        def get_tags():
-            tags = cache.get("tags")
-            if tags is None:
-                tags = Tag.query.order_by(Tag.num_posts.desc()).limit(10).all()
-                cache.set("tags", tags)
 
-            return dict(tags=tags)
+def configure_errorhandlers(app):
 
-        @self.app.context_processor
-        def config():
-            return dict(config=self.app.config)
+    if app.testing:
+        return
 
-    def configure_extensions(self):
+    @app.errorhandler(404)
+    def page_not_found(error):
+        if request.is_xhr:
+            return jsonify(error=_('Sorry, page not found'))
+        return render_template("errors/404.html", error=error)
 
-        mail.init_app(self.app)
-        db.init_app(self.app)
-        oid.init_app(self.app)
-        cache.init_app(self.app)
+    @app.errorhandler(403)
+    def forbidden(error):
+        if request.is_xhr:
+            return jsonify(error=_('Sorry, not allowed'))
+        return render_template("errors/403.html", error=error)
 
-        setup_themes(self.app)
+    @app.errorhandler(500)
+    def server_error(error):
+        if request.is_xhr:
+            return jsonify(error=_('Sorry, an error has occurred'))
+        return render_template("errors/500.html", error=error)
 
-        # more complicated setups
+    @app.errorhandler(401)
+    def unauthorized(error):
+        if request.is_xhr:
+            return jsonfiy(error=_("Login required"))
+        flash(_("Please login to see this page"), "error")
+        return redirect(url_for("account.login", next=request.path))
 
-        self.configure_identity()
-        self.configure_i18n()
-        
-    def configure_identity(self):
 
-        Principal(self.app)
+def configure_logging(app):
+    if app.debug or app.testing:
+        return
 
-        @identity_loaded.connect_via(self.app)
-        def on_identity_loaded(sender, identity):
-            g.user = User.query.from_identity(identity)
+    mail_handler = \
+        SMTPHandler(app.config['MAIL_SERVER'],
+                    'error@newsmeme.com',
+                    app.config['ADMINS'], 
+                    'application error',
+                    (
+                        app.config['MAIL_USERNAME'],
+                        app.config['MAIL_PASSWORD'],
+                    ))
 
-    def configure_i18n(self):
+    mail_handler.setLevel(logging.ERROR)
+    app.logger.addHandler(mail_handler)
 
-        babel = Babel(self.app)
+    formatter = logging.Formatter(
+        '%(asctime)s %(levelname)s: %(message)s '
+        '[in %(pathname)s:%(lineno)d]')
 
-        @babel.localeselector
-        def get_locale():
-            accept_languages = self.app.config.get('ACCEPT_LANGUAGES', 
-                                                   ['en_gb'])
+    debug_log = os.path.join(app.root_path, 
+                             app.config['DEBUG_LOG'])
 
-            return request.accept_languages.best_match(accept_languages)
+    debug_file_handler = \
+        RotatingFileHandler(debug_log,
+                            maxBytes=100000,
+                            backupCount=10)
 
-    def configure_errorhandlers(self):
-        if self.app.testing:
-            return
+    debug_file_handler.setLevel(logging.DEBUG)
+    debug_file_handler.setFormatter(formatter)
+    app.logger.addHandler(debug_file_handler)
 
-        @self.app.errorhandler(404)
-        def page_not_found(error):
-            if request.is_xhr:
-                return jsonify(error=_('Sorry, page not found'))
-            return render_template("errors/404.html", error=error)
+    error_log = os.path.join(app.root_path, 
+                             app.config['ERROR_LOG'])
 
-        @self.app.errorhandler(403)
-        def forbidden(error):
-            if request.is_xhr:
-                return jsonify(error=_('Sorry, not allowed'))
-            return render_template("errors/403.html", error=error)
+    error_file_handler = \
+        RotatingFileHandler(error_log,
+                            maxBytes=100000,
+                            backupCount=10)
 
-        @self.app.errorhandler(500)
-        def server_error(error):
-            if request.is_xhr:
-                return jsonify(error=_('Sorry, an error has occurred'))
-            return render_template("errors/500.html", error=error)
+    error_file_handler.setLevel(logging.ERROR)
+    error_file_handler.setFormatter(formatter)
+    app.logger.addHandler(error_file_handler)
 
-        @self.app.errorhandler(401)
-        def unauthorized(error):
-            if request.is_xhr:
-                return jsonfiy(error=_("Login required"))
-            flash(_("Please login to see this page"), "error")
-            return redirect(url_for("account.login", next=request.path))
 
 
-    def configure_logging(self):
-        if self.app.debug or self.app.testing:
-            return
-
-        mail_handler = \
-            SMTPHandler(self.app.config['MAIL_SERVER'],
-                       'error@newsmeme.com',
-                       self.app.config['ADMINS'], 
-                        'self.application error',
-                        (
-                            self.app.config['MAIL_USERNAME'],
-                            self.app.config['MAIL_PASSWORD'],
-                        ))
-
-        mail_handler.setLevel(logging.ERROR)
-        self.app.logger.addHandler(mail_handler)
-
-        formatter = logging.Formatter(
-            '%(asctime)s %(levelname)s: %(message)s '
-            '[in %(pathname)s:%(lineno)d]')
-
-        debug_log = os.path.join(self.app.root_path, 
-                                 self.app.config['DEBUG_LOG'])
-
-        debug_file_handler = \
-            RotatingFileHandler(debug_log,
-                                maxBytes=100000,
-                                backupCount=10)
-
-        debug_file_handler.setLevel(logging.DEBUG)
-        debug_file_handler.setFormatter(formatter)
-        self.app.logger.addHandler(debug_file_handler)
-
-        error_log = os.path.join(self.app.root_path, 
-                                 self.app.config['ERROR_LOG'])
-
-        error_file_handler = \
-            RotatingFileHandler(error_log,
-                                maxBytes=100000,
-                                backupCount=10)
-
-        error_file_handler.setLevel(logging.ERROR)
-        error_file_handler.setFormatter(formatter)
-        self.app.logger.addHandler(error_file_handler)
-
-
-

newsmeme/config.py

+# -*- coding: utf-8 -*-
+"""
+    config.py
+    ~~~~~~~~~~~
+
+    Default configuration
+
+    :copyright: (c) 2010 by Dan Jacob.
+    :license: BSD, see LICENSE for more details.
+"""
+
+from newsmeme import views
+
+class DefaultConfig(object):
+    """
+    Default configuration for a newsmeme application.
+    """
+
+    DEBUG = True
+
+    # change this in your production settings !!!
+
+    SECRET_KEY = "secret"
+
+    # keys for localhost. Change as appropriate.
+
+    RECAPTCHA_PUBLIC_KEY = '6LeYIbsSAAAAACRPIllxA7wvXjIE411PfdB2gt2J'
+    RECAPTCHA_PRIVATE_KEY = '6LeYIbsSAAAAAJezaIq3Ft_hSTo0YtyeFG-JgRtu'
+
+    SQLALCHEMY_DATABASE_URI = "sqlite:///newsmeme.db"
+
+    SQLALCHEMY_ECHO = False
+
+    MAIL_DEBUG = DEBUG
+
+    ADMINS = ()
+
+    DEFAULT_MAIL_SENDER = "support@thenewsmeme.com"
+
+    ACCEPT_LANGUAGES = ['en', 'fi']
+
+    DEBUG_LOG = 'logs/debug.log'
+    ERROR_LOG = 'logs/error.log'
+
+    THEME = 'newsmeme'
+
+    CACHE_TYPE = "simple"
+    CACHE_DEFAULT_TIMEOUT = 300
+
+    MODULES = (
+        (views.frontend, ""),
+        (views.post, "/post"),
+        (views.user, "/user"),
+        (views.comment, "/comment"),
+        (views.account, "/acct"),
+        (views.feeds, "/feeds"),
+        (views.openid, "/openid"),
+        (views.api, "/api"),
+    )
+
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.