Example #1
0
class SkyLines(Flask):
    def __init__(self, name='skylines', config_file=None, *args, **kw):
        # Create Flask instance
        super(SkyLines, self).__init__(name, *args, **kw)

        # Load default settings and from environment variable
        self.config.from_pyfile(config.DEFAULT_CONF_PATH)

        if 'SKYLINES_CONFIG' in os.environ:
            self.config.from_pyfile(os.environ['SKYLINES_CONFIG'])

        if config_file:
            self.config.from_pyfile(config_file)

    def add_sqlalchemy(self):
        """ Create and configure SQLAlchemy extension """
        from skylines.model import db
        db.init_app(self)

        from flask.ext.migrate import Migrate
        Migrate(self, db)

    def add_cache(self):
        """ Create and attach Cache extension """
        from flask.ext.cache import Cache
        self.cache = Cache(self, with_jinja2_ext=False)

    def add_babel(self):
        """ Create and attach Babel extension """
        from flask.ext.babel import Babel
        self.babel = Babel(self)

    def add_login_manager(self):
        """ Create and attach Login extension """
        from flask.ext.login import LoginManager
        self.login_manager = LoginManager()
        self.login_manager.init_app(self)

    def add_assets(self):
        """ Create and attach Assets extension """
        from skylines.frontend.assets import Environment
        self.assets = Environment(self)
        self.assets.load_bundles('skylines.frontend.assets.bundles')

    def add_tg2_compat(self):
        from skylines.lib import helpers

        @self.context_processor
        def inject_helpers_lib():
            return dict(h=helpers)

    def configure_jinja(self):
        from itertools import izip

        # Configure Jinja2 template engine
        self.jinja_options['extensions'].append('jinja2.ext.do')

        @self.template_filter('add_to_dict')
        def add_to_dict(d, **kw):
            return dict(d, **kw)

        @self.template_global()
        def zip(*args, **kw):
            return izip(*args, **kw)

    def add_logging_handlers(self):
        if self.debug: return

        import logging
        from logging import Formatter, Filter
        from logging.handlers import RotatingFileHandler, SMTPHandler

        # Set general log level
        self.logger.setLevel(logging.INFO)

        # Inject additional log record fields
        class ContextFilter(Filter):
            def __init__(self, app):
                self.app = app

            def filter(self, record):
                record.app_name = self.app.name
                record.url = request.url
                record.ip = request.remote_addr
                if hasattr(g, 'current_user') and g.current_user:
                    user = g.current_user
                    record.user = '******' % (user.name, user.email_address)
                else:
                    record.user = '******'

                def filter_password(k, v):
                    if 'password' in k:
                        v = 'xxxxxxx'

                    return (k, v)

                record.request_args = '\n'.join('%s: %r' % (k, v) for (k, v) in request.args.iteritems())
                record.request_form = '\n'.join('%s: %r' % filter_password(k, v) for (k, v) in request.form.iteritems())

                return True

        self.logger.addFilter(ContextFilter(self))

        # Add SMTP handler
        mail_handler = SMTPHandler(
            'localhost', '*****@*****.**',
            self.config.get('ADMINS', []), 'SkyLines Error Report')
        mail_handler.setLevel(logging.ERROR)

        mail_formatter = Formatter('''
App:                %(app_name)s
Time:               %(asctime)s
URL:                %(url)s
IP:                 %(ip)s
User:               %(user)s

Args:
%(request_args)s

Form:
%(request_form)s

Message:

%(message)s
''')
        mail_handler.setFormatter(mail_formatter)
        self.logger.addHandler(mail_handler)

        # Add log file handler (if configured)
        path = self.config.get('LOGFILE')
        if path:
            file_handler = RotatingFileHandler(path, 'a', 10000, 4)
            file_handler.setLevel(logging.INFO)

            file_formatter = Formatter(
                '%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]')
            file_handler.setFormatter(file_formatter)

            self.logger.addHandler(file_handler)

    def add_debug_toolbar(self):
        try:
            from flask_debugtoolbar import DebugToolbarExtension
            DebugToolbarExtension(self)
        except ImportError:
            pass

    def add_celery(self):
        from skylines.worker.celery import celery
        celery.init_app(self)
        return celery
Example #2
0
 def add_assets(self):
     """ Create and attach Assets extension """
     from skylines.frontend.assets import Environment
     self.assets = Environment(self)
     self.assets.load_bundles('skylines.frontend.assets.bundles')
Example #3
0
 def add_assets(self):
     """ Create and attach Assets extension """
     from skylines.frontend.assets import Environment
     self.assets = Environment(self)
     self.assets.load_bundles('skylines.frontend.assets.bundles')
Example #4
0
class SkyLines(Flask):
    def __init__(self, name='skylines', config_file=None, *args, **kw):
        # Create Flask instance
        super(SkyLines, self).__init__(name, *args, **kw)

        # Load default settings and from environment variable
        self.config.from_pyfile(config.DEFAULT_CONF_PATH)

        if 'SKYLINES_CONFIG' in os.environ:
            self.config.from_pyfile(os.environ['SKYLINES_CONFIG'])

        if config_file:
            self.config.from_pyfile(config_file)

    def add_sqlalchemy(self):
        """ Create and configure SQLAlchemy extension """
        from skylines.model import db
        db.init_app(self)

        from flask.ext.migrate import Migrate
        Migrate(self, db)

    def add_cache(self):
        """ Create and attach Cache extension """
        from flask.ext.cache import Cache
        self.cache = Cache(self, with_jinja2_ext=False)

    def add_babel(self):
        """ Create and attach Babel extension """
        from flask.ext.babel import Babel
        self.babel = Babel(self)

    def add_login_manager(self):
        """ Create and attach Login extension """
        from flask.ext.login import LoginManager
        self.login_manager = LoginManager()
        self.login_manager.init_app(self)

    def add_assets(self):
        """ Create and attach Assets extension """
        from skylines.frontend.assets import Environment
        self.assets = Environment(self)
        self.assets.load_bundles('skylines.frontend.assets.bundles')

    def add_tg2_compat(self):
        from skylines.lib import helpers

        @self.context_processor
        def inject_helpers_lib():
            return dict(h=helpers)

    def configure_jinja(self):
        from itertools import izip

        # Configure Jinja2 template engine
        self.jinja_options['extensions'].append('jinja2.ext.do')

        @self.template_filter('add_to_dict')
        def add_to_dict(d, **kw):
            return dict(d, **kw)

        @self.template_global()
        def zip(*args, **kw):
            return izip(*args, **kw)

    def add_logging_handlers(self):
        if self.debug: return

        import logging
        from logging import Formatter, Filter
        from logging.handlers import RotatingFileHandler, SMTPHandler

        # Set general log level
        self.logger.setLevel(logging.INFO)

        # Inject additional log record fields
        class ContextFilter(Filter):
            def __init__(self, app):
                self.app = app

            def filter(self, record):
                record.app_name = self.app.name
                record.url = request.url
                record.ip = request.remote_addr
                if g.current_user:
                    user = g.current_user
                    record.user = '******' % (user.name, user.email_address)
                else:
                    record.user = '******'

                return True

        self.logger.addFilter(ContextFilter(self))

        # Add SMTP handler
        mail_handler = SMTPHandler(
            'localhost', '*****@*****.**',
            self.config.get('ADMINS', []), 'SkyLines Error Report')
        mail_handler.setLevel(logging.ERROR)

        mail_formatter = Formatter('''
App:                %(app_name)s
Time:               %(asctime)s
URL:                %(url)s
IP:                 %(ip)s
User:               %(user)s

Message:

%(message)s
''')
        mail_handler.setFormatter(mail_formatter)
        self.logger.addHandler(mail_handler)

        # Add log file handler (if configured)
        path = self.config.get('LOGFILE')
        if path:
            file_handler = RotatingFileHandler(path, 'a', 10000, 4)
            file_handler.setLevel(logging.INFO)

            file_formatter = Formatter(
                '%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]')
            file_handler.setFormatter(file_formatter)

            self.logger.addHandler(file_handler)

    def add_debug_toolbar(self):
        try:
            from flask_debugtoolbar import DebugToolbarExtension
            DebugToolbarExtension(self)
        except ImportError:
            pass

    def add_celery(self):
        from skylines.worker.celery import celery
        celery.init_app(self)
        return celery
Example #5
0
class SkyLines(Flask):
    def __init__(self, name="skylines", config_file=None, *args, **kw):
        # Create Flask instance
        super(SkyLines, self).__init__(name, *args, **kw)

        # Load default settings and from environment variable
        self.config.from_pyfile(config.DEFAULT_CONF_PATH)

        if "SKYLINES_CONFIG" in os.environ:
            self.config.from_pyfile(os.environ["SKYLINES_CONFIG"])

        if config_file:
            self.config.from_pyfile(config_file)

    def add_sqlalchemy(self):
        """ Create and configure SQLAlchemy extension """
        from skylines.database import db, migrate

        db.init_app(self)
        migrate.init_app(self, db)

    def add_cache(self):
        """ Create and attach Cache extension """
        from flask.ext.cache import Cache

        self.cache = Cache(self, with_jinja2_ext=False)

    def add_babel(self):
        """ Create and attach Babel extension """
        from flask.ext.babel import Babel

        self.babel = Babel(self)

    def add_login_manager(self):
        """ Create and attach Login extension """
        from flask.ext.login import LoginManager

        self.login_manager = LoginManager()
        self.login_manager.init_app(self)

    def add_assets(self):
        """ Create and attach Assets extension """
        from skylines.frontend.assets import Environment

        self.assets = Environment(self)
        self.assets.load_bundles("skylines.frontend.assets.bundles")

    def add_tg2_compat(self):
        from skylines.lib import helpers

        @self.context_processor
        def inject_helpers_lib():
            return dict(h=helpers)

    def configure_jinja(self):
        from itertools import izip

        # Configure Jinja2 template engine
        self.jinja_options["extensions"].append("jinja2.ext.do")

        @self.template_filter("add_to_dict")
        def add_to_dict(d, **kw):
            return dict(d, **kw)

        @self.template_global()
        def zip(*args, **kw):
            return izip(*args, **kw)

    def add_logging_handlers(self):
        if self.debug:
            return

        import logging
        from logging import Formatter, Filter
        from logging.handlers import RotatingFileHandler, SMTPHandler

        # Set general log level
        self.logger.setLevel(logging.INFO)

        # Inject additional log record fields
        class ContextFilter(Filter):
            def __init__(self, app):
                self.app = app

            def filter(self, record):
                record.app_name = self.app.name
                record.url = request.url
                record.ip = request.remote_addr
                if hasattr(g, "current_user") and g.current_user:
                    user = g.current_user
                    record.user = "******" % (user.name, user.email_address)
                else:
                    record.user = "******"

                def filter_password(k, v):
                    if "password" in k:
                        v = "xxxxxxx"

                    return (k, v)

                record.request_args = "\n".join("%s: %r" % (k, v) for (k, v) in request.args.iteritems())
                record.request_form = "\n".join("%s: %r" % filter_password(k, v) for (k, v) in request.form.iteritems())

                return True

        self.logger.addFilter(ContextFilter(self))

        # Add SMTP handler
        mail_handler = SMTPHandler(
            "localhost", "*****@*****.**", self.config.get("ADMINS", []), "SkyLines Error Report"
        )
        mail_handler.setLevel(logging.ERROR)

        mail_formatter = Formatter(
            """
App:                %(app_name)s
Time:               %(asctime)s
URL:                %(url)s
IP:                 %(ip)s
User:               %(user)s

Args:
%(request_args)s

Form:
%(request_form)s

Message:

%(message)s
"""
        )
        mail_handler.setFormatter(mail_formatter)
        self.logger.addHandler(mail_handler)

        # Add log file handler (if configured)
        path = self.config.get("LOGFILE")
        if path:
            file_handler = RotatingFileHandler(path, "a", 10000, 4)
            file_handler.setLevel(logging.INFO)

            file_formatter = Formatter("%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]")
            file_handler.setFormatter(file_formatter)

            self.logger.addHandler(file_handler)

    def add_debug_toolbar(self):
        try:
            from flask_debugtoolbar import DebugToolbarExtension

            DebugToolbarExtension(self)
        except ImportError:
            pass

    def add_celery(self):
        from skylines.worker.celery import celery

        celery.init_app(self)
        return celery

    def initialize_lib(self):
        from skylines.lib.geoid import load_geoid

        load_geoid(self)