Python flask.Blueprint() Examples

The following are code examples for showing how to use flask.Blueprint(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: basic.py    Apache License 2.0 7 votes vote down vote up
def test_inject_blueprint_url_defaults(self):
        app = flask.Flask(__name__)
        bp = flask.Blueprint('foo.bar.baz', __name__,
                       template_folder='template')

        @bp.url_defaults
        def bp_defaults(endpoint, values):
            values['page'] = 'login'
        @bp.route('/<page>')
        def view(page): pass

        app.register_blueprint(bp)

        values = dict()
        app.inject_url_defaults('foo.bar.baz.view', values)
        expected = dict(page='login')
        self.assert_equal(values, expected)

        with app.test_request_context('/somepage'):
            url = flask.url_for('foo.bar.baz.view')
        expected = '/login'
        self.assert_equal(url, expected) 
Example 2
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 6 votes vote down vote up
def test_blueprint_url_definitions(self):
        bp = flask.Blueprint('test', __name__)

        @bp.route('/foo', defaults={'baz': 42})
        def foo(bar, baz):
            return '%s/%d' % (bar, baz)

        @bp.route('/bar')
        def bar(bar):
            return text_type(bar)

        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/1', url_defaults={'bar': 23})
        app.register_blueprint(bp, url_prefix='/2', url_defaults={'bar': 19})

        c = app.test_client()
        self.assert_equal(c.get('/1/foo').data, b'23/42')
        self.assert_equal(c.get('/2/foo').data, b'19/42')
        self.assert_equal(c.get('/1/bar').data, b'23')
        self.assert_equal(c.get('/2/bar').data, b'19') 
Example 3
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 6 votes vote down vote up
def test_default_static_cache_timeout(self):
        app = flask.Flask(__name__)
        class MyBlueprint(flask.Blueprint):
            def get_send_file_max_age(self, filename):
                return 100

        blueprint = MyBlueprint('blueprint', __name__, static_folder='static')
        app.register_blueprint(blueprint)

        # try/finally, in case other tests use this app for Blueprint tests.
        max_age_default = app.config['SEND_FILE_MAX_AGE_DEFAULT']
        try:
            with app.test_request_context():
                unexpected_max_age = 3600
                if app.config['SEND_FILE_MAX_AGE_DEFAULT'] == unexpected_max_age:
                    unexpected_max_age = 7200
                app.config['SEND_FILE_MAX_AGE_DEFAULT'] = unexpected_max_age
                rv = blueprint.send_static_file('index.html')
                cc = parse_cache_control_header(rv.headers['Cache-Control'])
                self.assert_equal(cc.max_age, 100)
                rv.close()
        finally:
            app.config['SEND_FILE_MAX_AGE_DEFAULT'] = max_age_default 
Example 4
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 6 votes vote down vote up
def test_dotted_names_from_app(self):
        app = flask.Flask(__name__)
        app.testing = True
        test = flask.Blueprint('test', __name__)

        @app.route('/')
        def app_index():
            return flask.url_for('test.index')

        @test.route('/test/')
        def index():
            return flask.url_for('app_index')

        app.register_blueprint(test)

        with app.test_client() as c:
            rv = c.get('/')
            self.assert_equal(rv.data, b'/test/') 
Example 5
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 6 votes vote down vote up
def test_blueprint_url_definitions(self):
        bp = flask.Blueprint('test', __name__)

        @bp.route('/foo', defaults={'baz': 42})
        def foo(bar, baz):
            return '%s/%d' % (bar, baz)

        @bp.route('/bar')
        def bar(bar):
            return text_type(bar)

        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/1', url_defaults={'bar': 23})
        app.register_blueprint(bp, url_prefix='/2', url_defaults={'bar': 19})

        c = app.test_client()
        self.assert_equal(c.get('/1/foo').data, b'23/42')
        self.assert_equal(c.get('/2/foo').data, b'19/42')
        self.assert_equal(c.get('/1/bar').data, b'23')
        self.assert_equal(c.get('/2/bar').data, b'19') 
Example 6
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 6 votes vote down vote up
def test_default_static_cache_timeout(self):
        app = flask.Flask(__name__)
        class MyBlueprint(flask.Blueprint):
            def get_send_file_max_age(self, filename):
                return 100

        blueprint = MyBlueprint('blueprint', __name__, static_folder='static')
        app.register_blueprint(blueprint)

        # try/finally, in case other tests use this app for Blueprint tests.
        max_age_default = app.config['SEND_FILE_MAX_AGE_DEFAULT']
        try:
            with app.test_request_context():
                unexpected_max_age = 3600
                if app.config['SEND_FILE_MAX_AGE_DEFAULT'] == unexpected_max_age:
                    unexpected_max_age = 7200
                app.config['SEND_FILE_MAX_AGE_DEFAULT'] = unexpected_max_age
                rv = blueprint.send_static_file('index.html')
                cc = parse_cache_control_header(rv.headers['Cache-Control'])
                self.assert_equal(cc.max_age, 100)
                rv.close()
        finally:
            app.config['SEND_FILE_MAX_AGE_DEFAULT'] = max_age_default 
Example 7
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 6 votes vote down vote up
def test_dotted_names(self):
        frontend = flask.Blueprint('myapp.frontend', __name__)
        backend = flask.Blueprint('myapp.backend', __name__)

        @frontend.route('/fe')
        def frontend_index():
            return flask.url_for('myapp.backend.backend_index')

        @frontend.route('/fe2')
        def frontend_page2():
            return flask.url_for('.frontend_index')

        @backend.route('/be')
        def backend_index():
            return flask.url_for('myapp.frontend.frontend_index')

        app = flask.Flask(__name__)
        app.register_blueprint(frontend)
        app.register_blueprint(backend)

        c = app.test_client()
        self.assert_equal(c.get('/fe').data.strip(), b'/be')
        self.assert_equal(c.get('/fe2').data.strip(), b'/fe')
        self.assert_equal(c.get('/be').data.strip(), b'/fe') 
Example 8
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: basic.py    Apache License 2.0 6 votes vote down vote up
def test_inject_blueprint_url_defaults(self):
        app = flask.Flask(__name__)
        bp = flask.Blueprint('foo.bar.baz', __name__,
                       template_folder='template')

        @bp.url_defaults
        def bp_defaults(endpoint, values):
            values['page'] = 'login'
        @bp.route('/<page>')
        def view(page): pass

        app.register_blueprint(bp)

        values = dict()
        app.inject_url_defaults('foo.bar.baz.view', values)
        expected = dict(page='login')
        self.assert_equal(values, expected)

        with app.test_request_context('/somepage'):
            url = flask.url_for('foo.bar.baz.view')
        expected = '/login'
        self.assert_equal(url, expected) 
Example 9
Project: flasky   Author: RoseOu   File: blueprints.py    MIT License 6 votes vote down vote up
def test_blueprint_url_definitions(self):
        bp = flask.Blueprint('test', __name__)

        @bp.route('/foo', defaults={'baz': 42})
        def foo(bar, baz):
            return '%s/%d' % (bar, baz)

        @bp.route('/bar')
        def bar(bar):
            return text_type(bar)

        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/1', url_defaults={'bar': 23})
        app.register_blueprint(bp, url_prefix='/2', url_defaults={'bar': 19})

        c = app.test_client()
        self.assert_equal(c.get('/1/foo').data, b'23/42')
        self.assert_equal(c.get('/2/foo').data, b'19/42')
        self.assert_equal(c.get('/1/bar').data, b'23')
        self.assert_equal(c.get('/2/bar').data, b'19') 
Example 10
Project: flasky   Author: RoseOu   File: blueprints.py    MIT License 6 votes vote down vote up
def test_default_static_cache_timeout(self):
        app = flask.Flask(__name__)
        class MyBlueprint(flask.Blueprint):
            def get_send_file_max_age(self, filename):
                return 100

        blueprint = MyBlueprint('blueprint', __name__, static_folder='static')
        app.register_blueprint(blueprint)

        # try/finally, in case other tests use this app for Blueprint tests.
        max_age_default = app.config['SEND_FILE_MAX_AGE_DEFAULT']
        try:
            with app.test_request_context():
                unexpected_max_age = 3600
                if app.config['SEND_FILE_MAX_AGE_DEFAULT'] == unexpected_max_age:
                    unexpected_max_age = 7200
                app.config['SEND_FILE_MAX_AGE_DEFAULT'] = unexpected_max_age
                rv = blueprint.send_static_file('index.html')
                cc = parse_cache_control_header(rv.headers['Cache-Control'])
                self.assert_equal(cc.max_age, 100)
                rv.close()
        finally:
            app.config['SEND_FILE_MAX_AGE_DEFAULT'] = max_age_default 
Example 11
Project: flasky   Author: RoseOu   File: blueprints.py    MIT License 6 votes vote down vote up
def test_dotted_names_from_app(self):
        app = flask.Flask(__name__)
        app.testing = True
        test = flask.Blueprint('test', __name__)

        @app.route('/')
        def app_index():
            return flask.url_for('test.index')

        @test.route('/test/')
        def index():
            return flask.url_for('app_index')

        app.register_blueprint(test)

        with app.test_client() as c:
            rv = c.get('/')
            self.assert_equal(rv.data, b'/test/') 
Example 12
Project: flasky   Author: RoseOu   File: basic.py    MIT License 6 votes vote down vote up
def test_inject_blueprint_url_defaults(self):
        app = flask.Flask(__name__)
        bp = flask.Blueprint('foo.bar.baz', __name__,
                       template_folder='template')

        @bp.url_defaults
        def bp_defaults(endpoint, values):
            values['page'] = 'login'
        @bp.route('/<page>')
        def view(page): pass

        app.register_blueprint(bp)

        values = dict()
        app.inject_url_defaults('foo.bar.baz.view', values)
        expected = dict(page='login')
        self.assert_equal(values, expected)

        with app.test_request_context('/somepage'):
            url = flask.url_for('foo.bar.baz.view')
        expected = '/login'
        self.assert_equal(url, expected) 
Example 13
Project: adh6   Author: bonnetn   File: proxy.py    GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, controller: ProxyController):
        self.blueprint = Blueprint('proxy_blueprint', __name__)

        @self.blueprint.route('/', defaults={'path': ''})
        @self.blueprint.route('/<path:path>', methods=['GET', 'OPTIONS', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE'])
        def proxy(path):
            return controller.proxy(
                path,
                Request(
                    method=request.method,
                    args=request.args,
                    headers=request.headers,
                    raw_content=request.stream.read(),
                ),
                session.get(SESSION_TOKEN)
            ) 
Example 14
Project: gitlab-tools   Author: Salamek   File: blueprints.py    GNU General Public License v3.0 6 votes vote down vote up
def _factory(partial_module_string: str, url_prefix: str=None) -> Blueprint:
    """Generates blueprint objects for view modules.

    Positional arguments:
    partial_module_string -- string representing a view module without the absolute path (e.g. 'home.index' for
        gitlab-tools.views.home.index).
    url_prefix -- URL prefix passed to the blueprint.

    Returns:
    Blueprint instance for a view module.
    """
    name = partial_module_string
    import_name = 'gitlab_tools.views.{}'.format(partial_module_string)
    template_folder = 'templates'
    blueprint = Blueprint(name, import_name, template_folder=template_folder, url_prefix=url_prefix)
    return blueprint 
Example 15
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_blueprint_specific_error_handling(self):
        frontend = flask.Blueprint('frontend', __name__)
        backend = flask.Blueprint('backend', __name__)
        sideend = flask.Blueprint('sideend', __name__)

        @frontend.errorhandler(403)
        def frontend_forbidden(e):
            return 'frontend says no', 403

        @frontend.route('/frontend-no')
        def frontend_no():
            flask.abort(403)

        @backend.errorhandler(403)
        def backend_forbidden(e):
            return 'backend says no', 403

        @backend.route('/backend-no')
        def backend_no():
            flask.abort(403)

        @sideend.route('/what-is-a-sideend')
        def sideend_no():
            flask.abort(403)

        app = flask.Flask(__name__)
        app.register_blueprint(frontend)
        app.register_blueprint(backend)
        app.register_blueprint(sideend)

        @app.errorhandler(403)
        def app_forbidden(e):
            return 'application itself says no', 403

        c = app.test_client()

        self.assert_equal(c.get('/frontend-no').data, b'frontend says no')
        self.assert_equal(c.get('/backend-no').data, b'backend says no')
        self.assert_equal(c.get('/what-is-a-sideend').data, b'application itself says no') 
Example 16
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_blueprint_url_processors(self):
        bp = flask.Blueprint('frontend', __name__, url_prefix='/<lang_code>')

        @bp.url_defaults
        def add_language_code(endpoint, values):
            values.setdefault('lang_code', flask.g.lang_code)

        @bp.url_value_preprocessor
        def pull_lang_code(endpoint, values):
            flask.g.lang_code = values.pop('lang_code')

        @bp.route('/')
        def index():
            return flask.url_for('.about')

        @bp.route('/about')
        def about():
            return flask.url_for('.index')

        app = flask.Flask(__name__)
        app.register_blueprint(bp)

        c = app.test_client()

        self.assert_equal(c.get('/de/').data, b'/de/about')
        self.assert_equal(c.get('/de/about').data, b'/de/') 
Example 17
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_empty_url_defaults(self):
        bp = flask.Blueprint('bp', __name__)

        @bp.route('/', defaults={'page': 1})
        @bp.route('/page/<int:page>')
        def something(page):
            return str(page)

        app = flask.Flask(__name__)
        app.register_blueprint(bp)

        c = app.test_client()
        self.assert_equal(c.get('/').data, b'1')
        self.assert_equal(c.get('/page/2').data, b'2') 
Example 18
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_route_decorator_custom_endpoint(self):

        bp = flask.Blueprint('bp', __name__)

        @bp.route('/foo')
        def foo():
            return flask.request.endpoint

        @bp.route('/bar', endpoint='bar')
        def foo_bar():
            return flask.request.endpoint

        @bp.route('/bar/123', endpoint='123')
        def foo_bar_foo():
            return flask.request.endpoint

        @bp.route('/bar/foo')
        def bar_foo():
            return flask.request.endpoint

        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/py')

        @app.route('/')
        def index():
            return flask.request.endpoint

        c = app.test_client()
        self.assertEqual(c.get('/').data, b'index')
        self.assertEqual(c.get('/py/foo').data, b'bp.foo')
        self.assertEqual(c.get('/py/bar').data, b'bp.bar')
        self.assertEqual(c.get('/py/bar/123').data, b'bp.123')
        self.assertEqual(c.get('/py/bar/foo').data, b'bp.bar_foo') 
Example 19
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_template_filter(self):
        bp = flask.Blueprint('bp', __name__)
        @bp.app_template_filter()
        def my_reverse(s):
            return s[::-1]
        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/py')
        self.assert_in('my_reverse', app.jinja_env.filters.keys())
        self.assert_equal(app.jinja_env.filters['my_reverse'], my_reverse)
        self.assert_equal(app.jinja_env.filters['my_reverse']('abcd'), 'dcba') 
Example 20
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_template_filter_with_name(self):
        bp = flask.Blueprint('bp', __name__)
        @bp.app_template_filter('strrev')
        def my_reverse(s):
            return s[::-1]
        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/py')
        self.assert_in('strrev', app.jinja_env.filters.keys())
        self.assert_equal(app.jinja_env.filters['strrev'], my_reverse)
        self.assert_equal(app.jinja_env.filters['strrev']('abcd'), 'dcba') 
Example 21
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_add_template_filter_with_name(self):
        bp = flask.Blueprint('bp', __name__)
        def my_reverse(s):
            return s[::-1]
        bp.add_app_template_filter(my_reverse, 'strrev')
        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/py')
        self.assert_in('strrev', app.jinja_env.filters.keys())
        self.assert_equal(app.jinja_env.filters['strrev'], my_reverse)
        self.assert_equal(app.jinja_env.filters['strrev']('abcd'), 'dcba') 
Example 22
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_template_filter_with_template(self):
        bp = flask.Blueprint('bp', __name__)
        @bp.app_template_filter()
        def super_reverse(s):
            return s[::-1]
        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/py')
        @app.route('/')
        def index():
            return flask.render_template('template_filter.html', value='abcd')
        rv = app.test_client().get('/')
        self.assert_equal(rv.data, b'dcba') 
Example 23
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_template_filter_after_route_with_template(self):
        app = flask.Flask(__name__)
        @app.route('/')
        def index():
            return flask.render_template('template_filter.html', value='abcd')
        bp = flask.Blueprint('bp', __name__)
        @bp.app_template_filter()
        def super_reverse(s):
            return s[::-1]
        app.register_blueprint(bp, url_prefix='/py')
        rv = app.test_client().get('/')
        self.assert_equal(rv.data, b'dcba') 
Example 24
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_add_template_filter_with_template(self):
        bp = flask.Blueprint('bp', __name__)
        def super_reverse(s):
            return s[::-1]
        bp.add_app_template_filter(super_reverse)
        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/py')
        @app.route('/')
        def index():
            return flask.render_template('template_filter.html', value='abcd')
        rv = app.test_client().get('/')
        self.assert_equal(rv.data, b'dcba') 
Example 25
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_add_template_filter_with_name_and_template(self):
        bp = flask.Blueprint('bp', __name__)
        def my_reverse(s):
            return s[::-1]
        bp.add_app_template_filter(my_reverse, 'super_reverse')
        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/py')
        @app.route('/')
        def index():
            return flask.render_template('template_filter.html', value='abcd')
        rv = app.test_client().get('/')
        self.assert_equal(rv.data, b'dcba') 
Example 26
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_template_test(self):
        bp = flask.Blueprint('bp', __name__)
        @bp.app_template_test()
        def is_boolean(value):
            return isinstance(value, bool)
        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/py')
        self.assert_in('is_boolean', app.jinja_env.tests.keys())
        self.assert_equal(app.jinja_env.tests['is_boolean'], is_boolean)
        self.assert_true(app.jinja_env.tests['is_boolean'](False)) 
Example 27
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_add_template_test(self):
        bp = flask.Blueprint('bp', __name__)
        def is_boolean(value):
            return isinstance(value, bool)
        bp.add_app_template_test(is_boolean)
        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/py')
        self.assert_in('is_boolean', app.jinja_env.tests.keys())
        self.assert_equal(app.jinja_env.tests['is_boolean'], is_boolean)
        self.assert_true(app.jinja_env.tests['is_boolean'](False)) 
Example 28
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_template_test_with_name(self):
        bp = flask.Blueprint('bp', __name__)
        @bp.app_template_test('boolean')
        def is_boolean(value):
            return isinstance(value, bool)
        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/py')
        self.assert_in('boolean', app.jinja_env.tests.keys())
        self.assert_equal(app.jinja_env.tests['boolean'], is_boolean)
        self.assert_true(app.jinja_env.tests['boolean'](False)) 
Example 29
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_add_template_test_with_name(self):
        bp = flask.Blueprint('bp', __name__)
        def is_boolean(value):
            return isinstance(value, bool)
        bp.add_app_template_test(is_boolean, 'boolean')
        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/py')
        self.assert_in('boolean', app.jinja_env.tests.keys())
        self.assert_equal(app.jinja_env.tests['boolean'], is_boolean)
        self.assert_true(app.jinja_env.tests['boolean'](False)) 
Example 30
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_template_test_after_route_with_template(self):
        app = flask.Flask(__name__)
        @app.route('/')
        def index():
            return flask.render_template('template_test.html', value=False)
        bp = flask.Blueprint('bp', __name__)
        @bp.app_template_test()
        def boolean(value):
            return isinstance(value, bool)
        app.register_blueprint(bp, url_prefix='/py')
        rv = app.test_client().get('/')
        self.assert_in(b'Success!', rv.data) 
Example 31
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_add_template_test_with_template(self):
        bp = flask.Blueprint('bp', __name__)
        def boolean(value):
            return isinstance(value, bool)
        bp.add_app_template_test(boolean)
        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/py')
        @app.route('/')
        def index():
            return flask.render_template('template_test.html', value=False)
        rv = app.test_client().get('/')
        self.assert_in(b'Success!', rv.data) 
Example 32
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_template_test_with_name_and_template(self):
        bp = flask.Blueprint('bp', __name__)
        @bp.app_template_test('boolean')
        def is_boolean(value):
            return isinstance(value, bool)
        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/py')
        @app.route('/')
        def index():
            return flask.render_template('template_test.html', value=False)
        rv = app.test_client().get('/')
        self.assert_in(b'Success!', rv.data) 
Example 33
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_add_template_test_with_name_and_template(self):
        bp = flask.Blueprint('bp', __name__)
        def is_boolean(value):
            return isinstance(value, bool)
        bp.add_app_template_test(is_boolean, 'boolean')
        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/py')
        @app.route('/')
        def index():
            return flask.render_template('template_test.html', value=False)
        rv = app.test_client().get('/')
        self.assert_in(b'Success!', rv.data) 
Example 34
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_blueprint_url_processors(self):
        bp = flask.Blueprint('frontend', __name__, url_prefix='/<lang_code>')

        @bp.url_defaults
        def add_language_code(endpoint, values):
            values.setdefault('lang_code', flask.g.lang_code)

        @bp.url_value_preprocessor
        def pull_lang_code(endpoint, values):
            flask.g.lang_code = values.pop('lang_code')

        @bp.route('/')
        def index():
            return flask.url_for('.about')

        @bp.route('/about')
        def about():
            return flask.url_for('.index')

        app = flask.Flask(__name__)
        app.register_blueprint(bp)

        c = app.test_client()

        self.assert_equal(c.get('/de/').data, b'/de/about')
        self.assert_equal(c.get('/de/about').data, b'/de/') 
Example 35
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_empty_url_defaults(self):
        bp = flask.Blueprint('bp', __name__)

        @bp.route('/', defaults={'page': 1})
        @bp.route('/page/<int:page>')
        def something(page):
            return str(page)

        app = flask.Flask(__name__)
        app.register_blueprint(bp)

        c = app.test_client()
        self.assert_equal(c.get('/').data, b'1')
        self.assert_equal(c.get('/page/2').data, b'2') 
Example 36
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_route_decorator_custom_endpoint(self):

        bp = flask.Blueprint('bp', __name__)

        @bp.route('/foo')
        def foo():
            return flask.request.endpoint

        @bp.route('/bar', endpoint='bar')
        def foo_bar():
            return flask.request.endpoint

        @bp.route('/bar/123', endpoint='123')
        def foo_bar_foo():
            return flask.request.endpoint

        @bp.route('/bar/foo')
        def bar_foo():
            return flask.request.endpoint

        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/py')

        @app.route('/')
        def index():
            return flask.request.endpoint

        c = app.test_client()
        self.assertEqual(c.get('/').data, b'index')
        self.assertEqual(c.get('/py/foo').data, b'bp.foo')
        self.assertEqual(c.get('/py/bar').data, b'bp.bar')
        self.assertEqual(c.get('/py/bar/123').data, b'bp.123')
        self.assertEqual(c.get('/py/bar/foo').data, b'bp.bar_foo') 
Example 37
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_template_filter(self):
        bp = flask.Blueprint('bp', __name__)
        @bp.app_template_filter()
        def my_reverse(s):
            return s[::-1]
        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/py')
        self.assert_in('my_reverse', app.jinja_env.filters.keys())
        self.assert_equal(app.jinja_env.filters['my_reverse'], my_reverse)
        self.assert_equal(app.jinja_env.filters['my_reverse']('abcd'), 'dcba') 
Example 38
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_add_template_filter(self):
        bp = flask.Blueprint('bp', __name__)
        def my_reverse(s):
            return s[::-1]
        bp.add_app_template_filter(my_reverse)
        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/py')
        self.assert_in('my_reverse', app.jinja_env.filters.keys())
        self.assert_equal(app.jinja_env.filters['my_reverse'], my_reverse)
        self.assert_equal(app.jinja_env.filters['my_reverse']('abcd'), 'dcba') 
Example 39
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_add_template_filter_with_name(self):
        bp = flask.Blueprint('bp', __name__)
        def my_reverse(s):
            return s[::-1]
        bp.add_app_template_filter(my_reverse, 'strrev')
        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/py')
        self.assert_in('strrev', app.jinja_env.filters.keys())
        self.assert_equal(app.jinja_env.filters['strrev'], my_reverse)
        self.assert_equal(app.jinja_env.filters['strrev']('abcd'), 'dcba') 
Example 40
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_template_filter_with_template(self):
        bp = flask.Blueprint('bp', __name__)
        @bp.app_template_filter()
        def super_reverse(s):
            return s[::-1]
        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/py')
        @app.route('/')
        def index():
            return flask.render_template('template_filter.html', value='abcd')
        rv = app.test_client().get('/')
        self.assert_equal(rv.data, b'dcba') 
Example 41
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_template_filter_after_route_with_template(self):
        app = flask.Flask(__name__)
        @app.route('/')
        def index():
            return flask.render_template('template_filter.html', value='abcd')
        bp = flask.Blueprint('bp', __name__)
        @bp.app_template_filter()
        def super_reverse(s):
            return s[::-1]
        app.register_blueprint(bp, url_prefix='/py')
        rv = app.test_client().get('/')
        self.assert_equal(rv.data, b'dcba') 
Example 42
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_add_template_filter_with_template(self):
        bp = flask.Blueprint('bp', __name__)
        def super_reverse(s):
            return s[::-1]
        bp.add_app_template_filter(super_reverse)
        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/py')
        @app.route('/')
        def index():
            return flask.render_template('template_filter.html', value='abcd')
        rv = app.test_client().get('/')
        self.assert_equal(rv.data, b'dcba') 
Example 43
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_template_filter_with_name_and_template(self):
        bp = flask.Blueprint('bp', __name__)
        @bp.app_template_filter('super_reverse')
        def my_reverse(s):
            return s[::-1]
        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/py')
        @app.route('/')
        def index():
            return flask.render_template('template_filter.html', value='abcd')
        rv = app.test_client().get('/')
        self.assert_equal(rv.data, b'dcba') 
Example 44
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_template_test(self):
        bp = flask.Blueprint('bp', __name__)
        @bp.app_template_test()
        def is_boolean(value):
            return isinstance(value, bool)
        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/py')
        self.assert_in('is_boolean', app.jinja_env.tests.keys())
        self.assert_equal(app.jinja_env.tests['is_boolean'], is_boolean)
        self.assert_true(app.jinja_env.tests['is_boolean'](False)) 
Example 45
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_add_template_test(self):
        bp = flask.Blueprint('bp', __name__)
        def is_boolean(value):
            return isinstance(value, bool)
        bp.add_app_template_test(is_boolean)
        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/py')
        self.assert_in('is_boolean', app.jinja_env.tests.keys())
        self.assert_equal(app.jinja_env.tests['is_boolean'], is_boolean)
        self.assert_true(app.jinja_env.tests['is_boolean'](False)) 
Example 46
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_template_test_with_name(self):
        bp = flask.Blueprint('bp', __name__)
        @bp.app_template_test('boolean')
        def is_boolean(value):
            return isinstance(value, bool)
        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/py')
        self.assert_in('boolean', app.jinja_env.tests.keys())
        self.assert_equal(app.jinja_env.tests['boolean'], is_boolean)
        self.assert_true(app.jinja_env.tests['boolean'](False)) 
Example 47
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_add_template_test_with_name(self):
        bp = flask.Blueprint('bp', __name__)
        def is_boolean(value):
            return isinstance(value, bool)
        bp.add_app_template_test(is_boolean, 'boolean')
        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/py')
        self.assert_in('boolean', app.jinja_env.tests.keys())
        self.assert_equal(app.jinja_env.tests['boolean'], is_boolean)
        self.assert_true(app.jinja_env.tests['boolean'](False)) 
Example 48
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_template_test_with_template(self):
        bp = flask.Blueprint('bp', __name__)
        @bp.app_template_test()
        def boolean(value):
            return isinstance(value, bool)
        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/py')
        @app.route('/')
        def index():
            return flask.render_template('template_test.html', value=False)
        rv = app.test_client().get('/')
        self.assert_in(b'Success!', rv.data) 
Example 49
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_add_template_test_with_template(self):
        bp = flask.Blueprint('bp', __name__)
        def boolean(value):
            return isinstance(value, bool)
        bp.add_app_template_test(boolean)
        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/py')
        @app.route('/')
        def index():
            return flask.render_template('template_test.html', value=False)
        rv = app.test_client().get('/')
        self.assert_in(b'Success!', rv.data) 
Example 50
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: blueprints.py    Apache License 2.0 5 votes vote down vote up
def test_template_test_with_name_and_template(self):
        bp = flask.Blueprint('bp', __name__)
        @bp.app_template_test('boolean')
        def is_boolean(value):
            return isinstance(value, bool)
        app = flask.Flask(__name__)
        app.register_blueprint(bp, url_prefix='/py')
        @app.route('/')
        def index():
            return flask.render_template('template_test.html', value=False)
        rv = app.test_client().get('/')
        self.assert_in(b'Success!', rv.data)