def check_blueprint(self, keep=False):
        prefix = 'testapp.testblueprint.views'
        init_args = () if keep else (prefix, )
        init_kwargs = {'import_prefix': prefix} if keep else {}

        app = create_test_app()
        views = LazyViews(app, 'testapp.views')
        views.add('/', 'home')

        views = LazyViews(**init_kwargs)
        blueprint = Blueprint('test_testblueprint',
                              'testapp.testblueprint',
                              template_folder='templates')
        self.assertEqual(len(app.view_functions), 2)

        views.init_blueprint(blueprint, *init_args)
        views.add('/advanced', 'advanced', methods=('GET', 'POST'))
        views.add('/test', 'test')

        app.register_blueprint(blueprint, url_prefix='/test')
        self.assertEqual(len(app.view_functions), 4)

        with app.test_request_context():
            client = app.test_client()
            response = client.get(url_for('test_testblueprint.test'))
            self.assertEqual(response.status_code, 200)
    def test_init_app(self):
        app = create_test_app()
        self.assertEqual(len(app.view_functions), 1)

        views = LazyViews(app, 'testapp')
        views.add_template('/', 'home.html', endpoint='home')
        views.add('/page/<int:page_id>', 'views.page')
        self.assertEqual(len(app.view_functions), 3)

        self.assertRaises(ValueError,
                          views.add_admin, 'admin.AdminView')

        with app.test_request_context():
            response = app.test_client().get(url_for('page', page_id=1))
            self.assertEqual(response.status_code, 200)
Beispiel #3
0
    def test_init_app_errors(self):
        views = LazyViews()

        self.assertRaises(AssertionError, views.add, '/page/<int:page_id>',
                          'page')
        self.assertRaises(AssertionError, views.add_admin, 'admin.AdminView')
        self.assertRaises(AssertionError, views.add_error, 403, 'error')
        self.assertRaises(AssertionError,
                          views.add_static,
                          '/favicon.ico',
                          defaults={'filename': 'img/favicon.ico'},
                          endpoint='favicon')
        self.assertRaises(AssertionError,
                          views.add_template,
                          '/template',
                          'template.html',
                          endpoint='template')
Beispiel #4
0
    def test_init_app(self):
        app = create_test_app()
        self.assertEqual(len(app.view_functions), 1)

        views = LazyViews(app, 'testapp')
        views.add_template('/', 'home.html', endpoint='home')
        views.add('/page/<int:page_id>', 'views.page')
        self.assertEqual(len(app.view_functions), 3)

        self.assertRaises(ValueError, views.add_admin, 'admin.AdminView')

        with app.test_request_context():
            response = app.test_client().get(url_for('page', page_id=1))
            self.assertEqual(response.status_code, 200)
Beispiel #5
0
# -*- coding: utf-8 -*-
#
# Project name: flask-restfull-example
# File name: urls
# Created: 2019-05-10
#
# Author: Liubov M. <*****@*****.**>
"""
Blueprint registration for full 'api' module
"""
from flask import Blueprint
from flask_lazyviews import LazyViews
from api.items.urls import ITEM_URLS
from api.users.urls import USER_URLS
from api.metrics.urls import METRIC_URLS
from api.status.urls import STATUS_URLS

API_BLUEPRINT = Blueprint('api', __name__)
VIEWS = LazyViews(API_BLUEPRINT)

# Add new urls
URLS = []
URLS.extend(ITEM_URLS)
URLS.extend(USER_URLS)
URLS.extend(METRIC_URLS)
URLS.extend(STATUS_URLS)

# load views
for url_name, view_name in URLS:
    VIEWS.add(url_name, view_name)
Beispiel #6
0
def create_routes(api):
    controllers = LazyViews(api, 'api.controllers')
    controllers.add('/', 'sample.index', methods=['GET'])
Beispiel #7
0
def create_app(**options):
    r"""Factory function to create test application.

    :param \*\*options: Override default settings from given options.
    :type \*\*options: dict
    """
    # Initialize application and configure it from settings and given options
    app = Flask('testapp')
    app.config.from_object(settings)
    app.config.update(options)

    # Put necessary filter & globals to Jinja environment
    app.jinja_env.filters['format'] = format
    app.jinja_env.globals['constants'] = constants
    app.jinja_env.globals.update({
        'constants': constants,
        'float': float,
        'fromtimestamp': datetime.datetime.fromtimestamp,
        'iteritems': iteritems,
        'len': len,
    })

    # Register Redis databases
    Redis(app, 'REDIS_LINKS')
    Redis(app, 'REDIS_CONTENT')

    # Setup all available routes
    views = LazyViews(app, 'views')
    views.add('/', 'index', methods=('GET', 'POST'))
    views.add('/comments/<thread_uid>', 'comment', methods=('POST', ))
    views.add('/comments/<thread_uid>', 'comments')
    views.add('/quit', 'quit')
    views.add('/threads', 'start_thread', methods=('POST', ))
    views.add('/threads', 'threads')
    views.add('/threads/<thread_uid>/delete',
              'delete_thread',
              methods=('GET', 'POST'))
    views.add_error(400, 'error')
    views.add_error(403, 'error')
    views.add_error(404, 'error')
    views.add_error(Exception, 'error')

    # Put username from session to globals before each request
    @app.before_request
    def global_username():
        key = constants.USERNAME_KEY.format(app.config['KEY_PREFIX'])
        g.username = session.get(key) or ''

    return app
Beispiel #8
0
 def test_init_blueprint_admin_error(self):
     blueprint = Blueprint('test_testblueprint',
                           'testapp.testblueprint',
                           template_folder='templates')
     views = LazyViews(blueprint, 'testapp')
     self.assertRaises(ValueError, views.add_admin, 'admin.AdminView')
Beispiel #9
0
    def check_blueprint(self, keep=False):
        prefix = 'testapp.testblueprint.views'
        init_args = () if keep else (prefix, )
        init_kwargs = {'import_prefix': prefix} if keep else {}

        app = create_test_app()
        views = LazyViews(app, 'testapp.views')
        views.add('/', 'home')

        views = LazyViews(**init_kwargs)
        blueprint = Blueprint('test_testblueprint',
                              'testapp.testblueprint',
                              template_folder='templates')
        self.assertEqual(len(app.view_functions), 2)

        views.init_blueprint(blueprint, *init_args)
        views.add('/advanced', 'advanced', methods=('GET', 'POST'))
        views.add('/test', 'test')

        app.register_blueprint(blueprint, url_prefix='/test')
        self.assertEqual(len(app.view_functions), 4)

        with app.test_request_context():
            client = app.test_client()
            response = client.get(url_for('test_testblueprint.test'))
            self.assertEqual(response.status_code, 200)
Beispiel #10
0
def create_app(**options):
    """Factory function to create test application.

    :param \*\*options: Override default settings from given options.
    :type \*\*options: dict
    """
    # Initialize application and configure it from settings and given options
    app = Flask('testapp')
    app.config.from_object(settings)
    app.config.update(options)

    # Put necessary filter & globals to Jinja environment
    app.jinja_env.filters['format'] = format
    app.jinja_env.globals['constants'] = constants
    app.jinja_env.globals.update({
        'constants': constants,
        'float': float,
        'fromtimestamp': datetime.datetime.fromtimestamp,
        'iteritems': iteritems,
        'len': len,
    })

    # Register Redis databases
    Redis(app, 'REDIS_LINKS')
    Redis(app, 'REDIS_CONTENT')

    # Setup all available routes
    views = LazyViews(app, 'views')
    views.add('/', 'index', methods=('GET', 'POST'))
    views.add('/comments/<thread_uid>', 'comment', methods=('POST', ))
    views.add('/comments/<thread_uid>', 'comments')
    views.add('/quit', 'quit')
    views.add('/threads', 'start_thread', methods=('POST', ))
    views.add('/threads', 'threads')
    views.add('/threads/<thread_uid>/delete',
              'delete_thread',
              methods=('GET', 'POST'))
    views.add_error(400, 'error')
    views.add_error(403, 'error')
    views.add_error(404, 'error')
    views.add_error(Exception, 'error')

    # Put username from session to globals before each request
    @app.before_request
    def global_username():
        key = constants.USERNAME_KEY.format(app.config['KEY_PREFIX'])
        g.username = session.get(key) or ''

    return app