# -*- coding:utf-8 -*-

from flask import Flask, render_template, redirect, url_for, flash, request, session
from flask.ext.assets import Bundle
from flask.ext.login import login_user, logout_user

from .extensions import assets, login_manager
from .models import User
from .forms import SigninForm


def __import_blueprint(blueprint_str):
    split = blueprint_str.split(".")
    module_path = ".".join(split[0: len(split) - 1])
    variable_name = split[-1]
    mod = __import__(module_path, fromlist=[variable_name])
    return getattr(mod, variable_name)


def config_str_to_obj(cfg):
    if isinstance(cfg, basestring):
        module = __import__("config", fromlist=[cfg])
        return getattr(module, cfg)
    return cfg


def app_factory(config, app_name=None, blueprints=None):
    app_name = app_name or __name__
    app = Flask(app_name)

    config = config_str_to_obj(config)
    configure_app(app, config)
    configure_blueprints(app, blueprints or config.BLUEPRINTS)
    configure_error_handlers(app)
    configure_database(app)
    configure_context_processors(app)
    configure_template_filters(app)
    configure_extensions(app)
    configure_before_request(app)
    configure_bundles(app)
    configure_views(app)

    return app


def configure_app(app, config):
    app.config.from_object(config)
    app.config.from_envvar("APP_CONFIG", silent=True)  # avaiable in the server


def configure_blueprints(app, blueprints):
    for blueprint_config in blueprints:
        blueprint = None
        kw = {}

        if (isinstance(blueprint_config, basestring)):
            blueprint = blueprint_config
        elif (isinstance(blueprint_config, tuple)):
            blueprint = blueprint_config[0]
            kw = blueprint_config[1]

        blueprint = __import_blueprint(blueprint)
        app.register_blueprint(blueprint, **kw)


def configure_error_handlers(app):

    @app.errorhandler(403)
    def forbidden_page(error):
        """
        The server understood the request, but is refusing to fulfill it.
        Authorization will not help and the request SHOULD NOT be repeated.
        If the request method was not HEAD and the server wishes to make public
        why the request has not been fulfilled, it SHOULD describe the reason
        for the refusal in the entity. If the server does not wish to make this
        information available to the client, the status code 404 (Not Found)
        can be used instead.
        """
        return render_template("access_forbidden.html"), 403

    @app.errorhandler(404)
    def page_not_found(error):
        """
        The server has not found anything matching the Request-URI. No
        indication is given of whether the condition is temporary or permanent.
        The 410 (Gone) status code SHOULD be used if the server knows, through
        some internally configurable mechanism, that an old resource is
        permanently unavailable and has no forwarding address. This status code
        is commonly used when the server does not wish to reveal exactly why
        the request has been refused, or when no other response is applicable.
        """
        return render_template("page_not_found.html"), 404

    @app.errorhandler(405)
    def method_not_allowed_page(error):
        """
        The method specified in the Request-Line is not allowed for the
        resource identified by the Request-URI. The response MUST include an
        Allow header containing a list of valid methods for the requested
        resource.
        """
        return render_template("method_not_allowed.html"), 405

    @app.errorhandler(500)
    def server_error_page(error):
        return render_template("server_error.html"), 500


def configure_database(app):
    "Database configuration should be set here"
    # uncomment for sqlalchemy support
    from database import db
    db.app = app
    db.init_app(app)


def configure_context_processors(app):
    "Modify templates context here"
    pass


def configure_template_filters(app):
    "Configure filters and tags for jinja2"
    pass


def configure_extensions(app):
    "Configure extensions like mail and login here"
    # Flask-Assets
    assets.init_app(app)

    # Flask-Login
    login_manager.init_app(app)
    login_manager.login_view = "signin"
    login_manager.login_message = "Authentication required to access this page."

    @login_manager.user_loader
    def load_user(userid):
        return User.query.filter_by(id=userid).first()


def configure_before_request(app):
    pass


def configure_bundles(app):
    css = Bundle(
        'less/base.less',
        filters='less',
        output='dist/base.css'
    )

    if "css_base" not in assets:
        assets.register("css_base", css)


def configure_views(app):
    "Add some simple views here like index_view"
    @app.route("/")
    def index():
        return render_template("index.html")

    @app.route("/signin", methods=["GET", "POST"])
    def signin():
        form = SigninForm()
        if form.validate_on_submit():
            user, authenticated = User.authenticate(
                form.email.data, form.password.data)
            if user is not None and authenticated == True:
                login_user(user)
                flash("Signed in successfully.", "success")
                return redirect(request.args.get("next") or
                                url_for("index"))
            else:
                flash("Incorrect email or password", "error")
        elif request.method == "POST":
            flash("Unable to sign you in.", "error")
        return render_template("signin.html", form=form)

    @app.route("/signout")
    def signout():
        logout_user()
        session.clear()
        return redirect(url_for("index"))

087a5034af2bd49219eff5bf0d492282d807a773
<mask token>


def config_str_to_obj(cfg):
    if isinstance(cfg, basestring):
        module = __import__('config', fromlist=[cfg])
        return getattr(module, cfg)
    return cfg


def app_factory(config, app_name=None, blueprints=None):
    app_name = app_name or __name__
    app = Flask(app_name)
    config = config_str_to_obj(config)
    configure_app(app, config)
    configure_blueprints(app, blueprints or config.BLUEPRINTS)
    configure_error_handlers(app)
    configure_database(app)
    configure_context_processors(app)
    configure_template_filters(app)
    configure_extensions(app)
    configure_before_request(app)
    configure_bundles(app)
    configure_views(app)
    return app


<mask token>


def configure_blueprints(app, blueprints):
    for blueprint_config in blueprints:
        blueprint = None
        kw = {}
        if isinstance(blueprint_config, basestring):
            blueprint = blueprint_config
        elif isinstance(blueprint_config, tuple):
            blueprint = blueprint_config[0]
            kw = blueprint_config[1]
        blueprint = __import_blueprint(blueprint)
        app.register_blueprint(blueprint, **kw)


def configure_error_handlers(app):

    @app.errorhandler(403)
    def forbidden_page(error):
        """
        The server understood the request, but is refusing to fulfill it.
        Authorization will not help and the request SHOULD NOT be repeated.
        If the request method was not HEAD and the server wishes to make public
        why the request has not been fulfilled, it SHOULD describe the reason
        for the refusal in the entity. If the server does not wish to make this
        information available to the client, the status code 404 (Not Found)
        can be used instead.
        """
        return render_template('access_forbidden.html'), 403

    @app.errorhandler(404)
    def page_not_found(error):
        """
        The server has not found anything matching the Request-URI. No
        indication is given of whether the condition is temporary or permanent.
        The 410 (Gone) status code SHOULD be used if the server knows, through
        some internally configurable mechanism, that an old resource is
        permanently unavailable and has no forwarding address. This status code
        is commonly used when the server does not wish to reveal exactly why
        the request has been refused, or when no other response is applicable.
        """
        return render_template('page_not_found.html'), 404

    @app.errorhandler(405)
    def method_not_allowed_page(error):
        """
        The method specified in the Request-Line is not allowed for the
        resource identified by the Request-URI. The response MUST include an
        Allow header containing a list of valid methods for the requested
        resource.
        """
        return render_template('method_not_allowed.html'), 405

    @app.errorhandler(500)
    def server_error_page(error):
        return render_template('server_error.html'), 500


def configure_database(app):
    """Database configuration should be set here"""
    from database import db
    db.app = app
    db.init_app(app)


def configure_context_processors(app):
    """Modify templates context here"""
    pass


<mask token>


def configure_before_request(app):
    pass


<mask token>

<mask token>


def config_str_to_obj(cfg):
    if isinstance(cfg, basestring):
        module = __import__('config', fromlist=[cfg])
        return getattr(module, cfg)
    return cfg


def app_factory(config, app_name=None, blueprints=None):
    app_name = app_name or __name__
    app = Flask(app_name)
    config = config_str_to_obj(config)
    configure_app(app, config)
    configure_blueprints(app, blueprints or config.BLUEPRINTS)
    configure_error_handlers(app)
    configure_database(app)
    configure_context_processors(app)
    configure_template_filters(app)
    configure_extensions(app)
    configure_before_request(app)
    configure_bundles(app)
    configure_views(app)
    return app


<mask token>


def configure_blueprints(app, blueprints):
    for blueprint_config in blueprints:
        blueprint = None
        kw = {}
        if isinstance(blueprint_config, basestring):
            blueprint = blueprint_config
        elif isinstance(blueprint_config, tuple):
            blueprint = blueprint_config[0]
            kw = blueprint_config[1]
        blueprint = __import_blueprint(blueprint)
        app.register_blueprint(blueprint, **kw)


def configure_error_handlers(app):

    @app.errorhandler(403)
    def forbidden_page(error):
        """
        The server understood the request, but is refusing to fulfill it.
        Authorization will not help and the request SHOULD NOT be repeated.
        If the request method was not HEAD and the server wishes to make public
        why the request has not been fulfilled, it SHOULD describe the reason
        for the refusal in the entity. If the server does not wish to make this
        information available to the client, the status code 404 (Not Found)
        can be used instead.
        """
        return render_template('access_forbidden.html'), 403

    @app.errorhandler(404)
    def page_not_found(error):
        """
        The server has not found anything matching the Request-URI. No
        indication is given of whether the condition is temporary or permanent.
        The 410 (Gone) status code SHOULD be used if the server knows, through
        some internally configurable mechanism, that an old resource is
        permanently unavailable and has no forwarding address. This status code
        is commonly used when the server does not wish to reveal exactly why
        the request has been refused, or when no other response is applicable.
        """
        return render_template('page_not_found.html'), 404

    @app.errorhandler(405)
    def method_not_allowed_page(error):
        """
        The method specified in the Request-Line is not allowed for the
        resource identified by the Request-URI. The response MUST include an
        Allow header containing a list of valid methods for the requested
        resource.
        """
        return render_template('method_not_allowed.html'), 405

    @app.errorhandler(500)
    def server_error_page(error):
        return render_template('server_error.html'), 500


def configure_database(app):
    """Database configuration should be set here"""
    from database import db
    db.app = app
    db.init_app(app)


def configure_context_processors(app):
    """Modify templates context here"""
    pass


<mask token>


def configure_extensions(app):
    """Configure extensions like mail and login here"""
    assets.init_app(app)
    login_manager.init_app(app)
    login_manager.login_view = 'signin'
    login_manager.login_message = (
        'Authentication required to access this page.')

    @login_manager.user_loader
    def load_user(userid):
        return User.query.filter_by(id=userid).first()


def configure_before_request(app):
    pass


<mask token>

<mask token>


def __import_blueprint(blueprint_str):
    split = blueprint_str.split('.')
    module_path = '.'.join(split[0:len(split) - 1])
    variable_name = split[-1]
    mod = __import__(module_path, fromlist=[variable_name])
    return getattr(mod, variable_name)


def config_str_to_obj(cfg):
    if isinstance(cfg, basestring):
        module = __import__('config', fromlist=[cfg])
        return getattr(module, cfg)
    return cfg


def app_factory(config, app_name=None, blueprints=None):
    app_name = app_name or __name__
    app = Flask(app_name)
    config = config_str_to_obj(config)
    configure_app(app, config)
    configure_blueprints(app, blueprints or config.BLUEPRINTS)
    configure_error_handlers(app)
    configure_database(app)
    configure_context_processors(app)
    configure_template_filters(app)
    configure_extensions(app)
    configure_before_request(app)
    configure_bundles(app)
    configure_views(app)
    return app


<mask token>


def configure_blueprints(app, blueprints):
    for blueprint_config in blueprints:
        blueprint = None
        kw = {}
        if isinstance(blueprint_config, basestring):
            blueprint = blueprint_config
        elif isinstance(blueprint_config, tuple):
            blueprint = blueprint_config[0]
            kw = blueprint_config[1]
        blueprint = __import_blueprint(blueprint)
        app.register_blueprint(blueprint, **kw)


def configure_error_handlers(app):

    @app.errorhandler(403)
    def forbidden_page(error):
        """
        The server understood the request, but is refusing to fulfill it.
        Authorization will not help and the request SHOULD NOT be repeated.
        If the request method was not HEAD and the server wishes to make public
        why the request has not been fulfilled, it SHOULD describe the reason
        for the refusal in the entity. If the server does not wish to make this
        information available to the client, the status code 404 (Not Found)
        can be used instead.
        """
        return render_template('access_forbidden.html'), 403

    @app.errorhandler(404)
    def page_not_found(error):
        """
        The server has not found anything matching the Request-URI. No
        indication is given of whether the condition is temporary or permanent.
        The 410 (Gone) status code SHOULD be used if the server knows, through
        some internally configurable mechanism, that an old resource is
        permanently unavailable and has no forwarding address. This status code
        is commonly used when the server does not wish to reveal exactly why
        the request has been refused, or when no other response is applicable.
        """
        return render_template('page_not_found.html'), 404

    @app.errorhandler(405)
    def method_not_allowed_page(error):
        """
        The method specified in the Request-Line is not allowed for the
        resource identified by the Request-URI. The response MUST include an
        Allow header containing a list of valid methods for the requested
        resource.
        """
        return render_template('method_not_allowed.html'), 405

    @app.errorhandler(500)
    def server_error_page(error):
        return render_template('server_error.html'), 500


def configure_database(app):
    """Database configuration should be set here"""
    from database import db
    db.app = app
    db.init_app(app)


def configure_context_processors(app):
    """Modify templates context here"""
    pass


<mask token>


def configure_extensions(app):
    """Configure extensions like mail and login here"""
    assets.init_app(app)
    login_manager.init_app(app)
    login_manager.login_view = 'signin'
    login_manager.login_message = (
        'Authentication required to access this page.')

    @login_manager.user_loader
    def load_user(userid):
        return User.query.filter_by(id=userid).first()


def configure_before_request(app):
    pass


def configure_bundles(app):
    css = Bundle('less/base.less', filters='less', output='dist/base.css')
    if 'css_base' not in assets:
        assets.register('css_base', css)


<mask token>

from flask import Flask, render_template, redirect, url_for, flash, request, session
from flask.ext.assets import Bundle
from flask.ext.login import login_user, logout_user
from .extensions import assets, login_manager
from .models import User
from .forms import SigninForm


def __import_blueprint(blueprint_str):
    split = blueprint_str.split('.')
    module_path = '.'.join(split[0:len(split) - 1])
    variable_name = split[-1]
    mod = __import__(module_path, fromlist=[variable_name])
    return getattr(mod, variable_name)


def config_str_to_obj(cfg):
    if isinstance(cfg, basestring):
        module = __import__('config', fromlist=[cfg])
        return getattr(module, cfg)
    return cfg


def app_factory(config, app_name=None, blueprints=None):
    app_name = app_name or __name__
    app = Flask(app_name)
    config = config_str_to_obj(config)
    configure_app(app, config)
    configure_blueprints(app, blueprints or config.BLUEPRINTS)
    configure_error_handlers(app)
    configure_database(app)
    configure_context_processors(app)
    configure_template_filters(app)
    configure_extensions(app)
    configure_before_request(app)
    configure_bundles(app)
    configure_views(app)
    return app


def configure_app(app, config):
    app.config.from_object(config)
    app.config.from_envvar('APP_CONFIG', silent=True)


def configure_blueprints(app, blueprints):
    for blueprint_config in blueprints:
        blueprint = None
        kw = {}
        if isinstance(blueprint_config, basestring):
            blueprint = blueprint_config
        elif isinstance(blueprint_config, tuple):
            blueprint = blueprint_config[0]
            kw = blueprint_config[1]
        blueprint = __import_blueprint(blueprint)
        app.register_blueprint(blueprint, **kw)


def configure_error_handlers(app):

    @app.errorhandler(403)
    def forbidden_page(error):
        """
        The server understood the request, but is refusing to fulfill it.
        Authorization will not help and the request SHOULD NOT be repeated.
        If the request method was not HEAD and the server wishes to make public
        why the request has not been fulfilled, it SHOULD describe the reason
        for the refusal in the entity. If the server does not wish to make this
        information available to the client, the status code 404 (Not Found)
        can be used instead.
        """
        return render_template('access_forbidden.html'), 403

    @app.errorhandler(404)
    def page_not_found(error):
        """
        The server has not found anything matching the Request-URI. No
        indication is given of whether the condition is temporary or permanent.
        The 410 (Gone) status code SHOULD be used if the server knows, through
        some internally configurable mechanism, that an old resource is
        permanently unavailable and has no forwarding address. This status code
        is commonly used when the server does not wish to reveal exactly why
        the request has been refused, or when no other response is applicable.
        """
        return render_template('page_not_found.html'), 404

    @app.errorhandler(405)
    def method_not_allowed_page(error):
        """
        The method specified in the Request-Line is not allowed for the
        resource identified by the Request-URI. The response MUST include an
        Allow header containing a list of valid methods for the requested
        resource.
        """
        return render_template('method_not_allowed.html'), 405

    @app.errorhandler(500)
    def server_error_page(error):
        return render_template('server_error.html'), 500


def configure_database(app):
    """Database configuration should be set here"""
    from database import db
    db.app = app
    db.init_app(app)


def configure_context_processors(app):
    """Modify templates context here"""
    pass


def configure_template_filters(app):
    """Configure filters and tags for jinja2"""
    pass


def configure_extensions(app):
    """Configure extensions like mail and login here"""
    assets.init_app(app)
    login_manager.init_app(app)
    login_manager.login_view = 'signin'
    login_manager.login_message = (
        'Authentication required to access this page.')

    @login_manager.user_loader
    def load_user(userid):
        return User.query.filter_by(id=userid).first()


def configure_before_request(app):
    pass


def configure_bundles(app):
    css = Bundle('less/base.less', filters='less', output='dist/base.css')
    if 'css_base' not in assets:
        assets.register('css_base', css)


def configure_views(app):
    """Add some simple views here like index_view"""

    @app.route('/')
    def index():
        return render_template('index.html')

    @app.route('/signin', methods=['GET', 'POST'])
    def signin():
        form = SigninForm()
        if form.validate_on_submit():
            user, authenticated = User.authenticate(form.email.data, form.
                password.data)
            if user is not None and authenticated == True:
                login_user(user)
                flash('Signed in successfully.', 'success')
                return redirect(request.args.get('next') or url_for('index'))
            else:
                flash('Incorrect email or password', 'error')
        elif request.method == 'POST':
            flash('Unable to sign you in.', 'error')
        return render_template('signin.html', form=form)

    @app.route('/signout')
    def signout():
        logout_user()
        session.clear()
        return redirect(url_for('index'))

# -*- coding:utf-8 -*-

from flask import Flask, render_template, redirect, url_for, flash, request, session
from flask.ext.assets import Bundle
from flask.ext.login import login_user, logout_user

from .extensions import assets, login_manager
from .models import User
from .forms import SigninForm


def __import_blueprint(blueprint_str):
    split = blueprint_str.split(".")
    module_path = ".".join(split[0: len(split) - 1])
    variable_name = split[-1]
    mod = __import__(module_path, fromlist=[variable_name])
    return getattr(mod, variable_name)


def config_str_to_obj(cfg):
    if isinstance(cfg, basestring):
        module = __import__("config", fromlist=[cfg])
        return getattr(module, cfg)
    return cfg


def app_factory(config, app_name=None, blueprints=None):
    app_name = app_name or __name__
    app = Flask(app_name)

    config = config_str_to_obj(config)
    configure_app(app, config)
    configure_blueprints(app, blueprints or config.BLUEPRINTS)
    configure_error_handlers(app)
    configure_database(app)
    configure_context_processors(app)
    configure_template_filters(app)
    configure_extensions(app)
    configure_before_request(app)
    configure_bundles(app)
    configure_views(app)

    return app


def configure_app(app, config):
    app.config.from_object(config)
    app.config.from_envvar("APP_CONFIG", silent=True)  # avaiable in the server


def configure_blueprints(app, blueprints):
    for blueprint_config in blueprints:
        blueprint = None
        kw = {}

        if (isinstance(blueprint_config, basestring)):
            blueprint = blueprint_config
        elif (isinstance(blueprint_config, tuple)):
            blueprint = blueprint_config[0]
            kw = blueprint_config[1]

        blueprint = __import_blueprint(blueprint)
        app.register_blueprint(blueprint, **kw)


def configure_error_handlers(app):

    @app.errorhandler(403)
    def forbidden_page(error):
        """
        The server understood the request, but is refusing to fulfill it.
        Authorization will not help and the request SHOULD NOT be repeated.
        If the request method was not HEAD and the server wishes to make public
        why the request has not been fulfilled, it SHOULD describe the reason
        for the refusal in the entity. If the server does not wish to make this
        information available to the client, the status code 404 (Not Found)
        can be used instead.
        """
        return render_template("access_forbidden.html"), 403

    @app.errorhandler(404)
    def page_not_found(error):
        """
        The server has not found anything matching the Request-URI. No
        indication is given of whether the condition is temporary or permanent.
        The 410 (Gone) status code SHOULD be used if the server knows, through
        some internally configurable mechanism, that an old resource is
        permanently unavailable and has no forwarding address. This status code
        is commonly used when the server does not wish to reveal exactly why
        the request has been refused, or when no other response is applicable.
        """
        return render_template("page_not_found.html"), 404

    @app.errorhandler(405)
    def method_not_allowed_page(error):
        """
        The method specified in the Request-Line is not allowed for the
        resource identified by the Request-URI. The response MUST include an
        Allow header containing a list of valid methods for the requested
        resource.
        """
        return render_template("method_not_allowed.html"), 405

    @app.errorhandler(500)
    def server_error_page(error):
        return render_template("server_error.html"), 500


def configure_database(app):
    "Database configuration should be set here"
    # uncomment for sqlalchemy support
    from database import db
    db.app = app
    db.init_app(app)


def configure_context_processors(app):
    "Modify templates context here"
    pass


def configure_template_filters(app):
    "Configure filters and tags for jinja2"
    pass


def configure_extensions(app):
    "Configure extensions like mail and login here"
    # Flask-Assets
    assets.init_app(app)

    # Flask-Login
    login_manager.init_app(app)
    login_manager.login_view = "signin"
    login_manager.login_message = "Authentication required to access this page."

    @login_manager.user_loader
    def load_user(userid):
        return User.query.filter_by(id=userid).first()


def configure_before_request(app):
    pass


def configure_bundles(app):
    css = Bundle(
        'less/base.less',
        filters='less',
        output='dist/base.css'
    )

    if "css_base" not in assets:
        assets.register("css_base", css)


def configure_views(app):
    "Add some simple views here like index_view"
    @app.route("/")
    def index():
        return render_template("index.html")

    @app.route("/signin", methods=["GET", "POST"])
    def signin():
        form = SigninForm()
        if form.validate_on_submit():
            user, authenticated = User.authenticate(
                form.email.data, form.password.data)
            if user is not None and authenticated == True:
                login_user(user)
                flash("Signed in successfully.", "success")
                return redirect(request.args.get("next") or
                                url_for("index"))
            else:
                flash("Incorrect email or password", "error")
        elif request.method == "POST":
            flash("Unable to sign you in.", "error")
        return render_template("signin.html", form=form)

    @app.route("/signout")
    def signout():
        logout_user()
        session.clear()
        return redirect(url_for("index"))
