def test_error_config_app(self): views = LazyViews(self.app, import_prefix='weird.path') views.add('/default-page', 'views.page', defaults={'page_id': 1}, endpoint='default_page') self.assertIn('default_page', app.view_functions) client = self.app.test_client() self.assertRaises(ImportStringError, client.get, self.url('default_page')) views = LazyViews(app, import_prefix='testapp.views') views.add('/another-default-page', 'does_not_exist', endpoint='another_default_page') self.assertIn('another_default_page', app.view_functions) client = app.test_client() self.assertRaises(ImportStringError, client.get, self.url('another_default_page'))
def test_init_blueprint(self): views = LazyViews() self.assertRaises(AssertionError, views.add, '/more-advanced', 'views.advanced', endpoint='more_advanced', methods=('GET', 'POST', 'PUT')) name = self.get_blueprint_name() blueprint = init_blueprint(name) views.init_blueprint(blueprint, import_prefix=blueprint.import_name) views.add('/more-advanced', 'views.advanced', endpoint='more_advanced', methods=('GET', 'POST', 'PUT')) self.assertRaises(ValueError, views.add_admin, AdminView(name='Admin View')) # Don't forget to re-register blueprint self.app.register_blueprint(blueprint, url_prefix='/test') self.assertIn('{0}.more_advanced'.format(name), self.app.view_functions) response = self.client.put(self.url('{0}.more_advanced'.format(name))) self.assert200(response) self.assertContains(response, 'Advanced test page')
def test_error_config_blueprint(self): views = LazyViews(blueprint, import_prefix='weird.path') views.add('/more-advanced', 'views.advanced', endpoint='more_advanced') app.blueprints.pop('test') app.register_blueprint(blueprint, url_prefix='/test') self.assertIn('test.more_advanced', app.view_functions) test_app = app.test_client() self.assertRaises(ImportStringError, test_app.get, self.url('test.more_advanced')) views = LazyViews(blueprint, blueprint.import_name) views.add('/more-more-advanced', 'views.does_not_exist', endpoint='more_more_advanced') app.blueprints.pop('test') app.register_blueprint(blueprint, url_prefix='/test') self.assertIn('test.more_more_advanced', app.view_functions) test_app = app.test_client() self.assertRaises(ImportStringError, test_app.get, self.url('test.more_more_advanced'))
def test_init_blueprint(self): views = LazyViews() self.assertRaises(AssertionError, views.add, '/more-advanced', 'views.advanced', endpoint='more_advanced', methods=('GET', 'POST', 'PUT')) views.init_blueprint(blueprint, import_prefix=blueprint.import_name) views.add('/more-advanced', 'views.advanced', endpoint='more_advanced', methods=('GET', 'POST', 'PUT')) self.assertRaises( ValueError, views.add_admin, AdminView(name='Admin View') ) # Don't forget to re-register blueprint self.app.blueprints.pop('test') self.app.register_blueprint(blueprint, url_prefix='/test') self.assertIn('test.more_advanced', app.view_functions) client = self.app.test_client() response = client.put(self.url('test.more_advanced')) self.assert200(response) self.assertIn('Advanced test page', response.data)
def init_app(): """ Factory function to init Flask applictaion. """ # Init Flask application and necessary extensions app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://' admin = Admin(app) db = SQLAlchemy(app) # Add url rules to application views = LazyViews(app) views.add('/', 'views.home') views.add('/db', 'views.database_page') views.add('/error', 'views.server_error') views.add('/page/<int:page_id>', 'views.page', endpoint='flatpage') views.add('/page/<int:page_id>/cls', 'views.PageView', endpoint='flatpage_cls') views.add_admin('admin.AdminView', endpoint='app_admin', name='App View') views.add_error(404, 'views.error') views.add_error(500, 'views.error') views.add_static('/favicon.ico', defaults={'filename': 'img/favicon.ico'}, endpoint='favicon') # Register test blueprint app.register_blueprint(test_blueprint, url_prefix='/test') return (app, admin, db)
def test_init_blueprint(self): views = LazyViews() self.assertRaises(AssertionError, views.add, '/more-advanced', 'views.advanced', endpoint='more_advanced', methods=('GET', 'POST', 'PUT')) name = self.get_blueprint_name() blueprint = init_blueprint(name) views.init_blueprint(blueprint, import_prefix=blueprint.import_name) views.add('/more-advanced', 'views.advanced', endpoint='more_advanced', methods=('GET', 'POST', 'PUT')) self.assertRaises(ValueError, views.add_admin, AdminView(name='Admin View')) # Don't forget to re-register blueprint self.app.register_blueprint(blueprint, url_prefix='/test') self.assertIn('{0}.more_advanced'.format(name), self.app.view_functions) response = self.client.put(self.url('{0}.more_advanced'.format(name))) self.assert200(response) self.assertContains(response, 'Advanced test page')
def test_error_config_blueprint(self): name = self.get_blueprint_name() blueprint = init_blueprint(name) views = LazyViews(blueprint, import_prefix='weird.path') views.add('/more-advanced', 'views.advanced', endpoint='more_advanced') self.app.register_blueprint(blueprint, url_prefix='/test') self.assertIn('{0}.more_advanced'.format(name), self.app.view_functions) self.assertRaises(ImportStringError, self.client.get, self.url('{0}.more_advanced'.format(name))) name = self.get_blueprint_name() blueprint = init_blueprint(name) views = LazyViews(blueprint, blueprint.import_name) views.add('/more-more-advanced', 'views.does_not_exist', endpoint='more_more_advanced') self.app.register_blueprint(blueprint, url_prefix='/test') self.assertIn('{0}.more_more_advanced'.format(name), self.app.view_functions) self.assertRaises(ImportStringError, self.client.get, self.url('{0}.more_more_advanced'.format(name)))
def test_error_config_blueprint(self): name = self.get_blueprint_name() blueprint = init_blueprint(name) views = LazyViews(blueprint, import_prefix='weird.path') views.add('/more-advanced', 'views.advanced', endpoint='more_advanced') self.app.register_blueprint(blueprint, url_prefix='/test') self.assertIn('{0}.more_advanced'.format(name), self.app.view_functions) self.assertRaises(ImportStringError, self.client.get, self.url('{0}.more_advanced'.format(name))) name = self.get_blueprint_name() blueprint = init_blueprint(name) views = LazyViews(blueprint, blueprint.import_name) views.add('/more-more-advanced', 'views.does_not_exist', endpoint='more_more_advanced') self.app.register_blueprint(blueprint, url_prefix='/test') self.assertIn('{0}.more_more_advanced'.format(name), self.app.view_functions) self.assertRaises(ImportStringError, self.client.get, self.url('{0}.more_more_advanced'.format(name)))
def init_app(): """ Factory function to init Flask applictaion. """ # Init Flask application and necessary extensions app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://' admin = Admin(app) db = SQLAlchemy(app) # Add url rules to application views = LazyViews(app) views.add('/', 'views.home') views.add('/db', 'views.database_page') views.add('/error', 'views.server_error') views.add('/page/<int:page_id>', 'views.page', endpoint='flatpage') views.add('/page/<int:page_id>/cls', 'views.PageView', endpoint='flatpage_cls') views.add_admin('admin.AdminView', endpoint='app_admin', name='App View') views.add_error(404, 'views.error') views.add_error(500, 'views.error') views.add_static('/favicon.ico', defaults={'filename': 'img/favicon.ico'}, endpoint='favicon') # Register test blueprint app.register_blueprint(test_blueprint, url_prefix='/test') return (app, admin, db)
def test_init_app(self): self.app.blueprints.pop('app_admin') views = LazyViews() self.assertRaises(AssertionError, views.add, '/default-page', 'page', defaults={'page_id': 1}, endpoint='default_page') self.assertRaises(AssertionError, views.add_admin, AdminView(name='Admin View')) self.assertRaises(AssertionError, views.add_error, 404, 'error') self.assertRaises(AssertionError, views.add_static, '/more-static/<path:filename>', endpoint='more_static') views.init_app(self.app) views.add('/default-page', page_view, defaults={'page_id': 1}, endpoint='default_page') views.add('/advanced-page', PageView.as_view('page'), defaults={'page_id': 2}, endpoint='advanced_page') views.add_admin(AdminView(name='Admin View')) views.add_static('/more-static/<path:filename>', endpoint='more_static') self.assertIn('advanced_page', self.app.view_functions) self.assertIn('default_page', self.app.view_functions) self.assertIn('more_static', self.app.view_functions) client = self.app.test_client() response = client.get(self.url('default_page')) self.assert200(response) self.assertIn('Page #1', response.data) response = client.get(self.url('advanced_page')) self.assert200(response) self.assertIn('Page #2', response.data) response = client.get(self.url('admin.index')) self.assert200(response) self.assertIn('Admin View', response.data) favicon_url = self.url('more_static', filename='img/favicon.ico') response = client.get(favicon_url) self.assert200(response)
def test_custom_config_app(self): views = LazyViews(self.app, import_prefix='testapp.views') views.add('/default-page', 'page', defaults={'page_id': 1}, endpoint='default_page') self.assertIn('default_page', self.app.view_functions) response = self.client.get(self.url('default_page')) self.assert200(response) self.assertContains(response, 'Page #1')
def test_custom_config_app(self): views = LazyViews(self.app, import_prefix='testapp.views') views.add('/default-page', 'page', defaults={'page_id': 1}, endpoint='default_page') self.assertIn('default_page', self.app.view_functions) response = self.client.get(self.url('default_page')) self.assert200(response) self.assertContains(response, 'Page #1')
def init_blueprint(name=None): """ Factory for initialize blueprint. """ blueprint = Blueprint(name or 'test', 'testapp.test', template_folder='templates') views = LazyViews(blueprint, import_prefix='.views') views.add('/', 'test') views.add('/advanced', 'advanced', methods=('GET', 'POST')) return blueprint
def test_custom_config_app(self): views = LazyViews(app, 'testapp.views') views.add('/default-page', 'page', defaults={'page_id': 1}, endpoint='default_page') self.assertIn('default_page', app.view_functions) test_app = app.test_client() response = test_app.get(self.url('default_page')) self.assertEqual(response.status_code, 200) self.assertIn('Page #1', response.data)
def test_init_app(self): self.app.blueprints.pop('app_admin') views = LazyViews() self.assertRaises(AssertionError, views.add, '/default-page', 'page', defaults={'page_id': 1}, endpoint='default_page') self.assertRaises(AssertionError, views.add_admin, AdminView(name='Admin View')) self.assertRaises(AssertionError, views.add_error, 404, 'error') self.assertRaises(AssertionError, views.add_static, '/more-static/<path:filename>', endpoint='more_static') views.init_app(self.app) views.add('/default-page', page_view, defaults={'page_id': 1}, endpoint='default_page') views.add('/advanced-page', PageView.as_view('page'), defaults={'page_id': 2}, endpoint='advanced_page') views.add_admin(AdminView(name='Admin View')) views.add_static('/more-static/<path:filename>', endpoint='more_static') self.assertIn('advanced_page', self.app.view_functions) self.assertIn('default_page', self.app.view_functions) self.assertIn('more_static', self.app.view_functions) response = self.client.get(self.url('default_page')) self.assert200(response) self.assertContains(response, 'Page #1') response = self.client.get(self.url('advanced_page')) self.assert200(response) self.assertContains(response, 'Page #2') response = self.client.get(self.url('admin.index')) self.assert200(response) self.assertContains(response, 'Admin View') favicon_url = self.url('more_static', filename='img/favicon.ico') response = self.client.get(favicon_url) self.assert200(response)
def test_custom_config_blueprint(self): name = self.get_blueprint_name() blueprint = init_blueprint(name) views = LazyViews(blueprint, import_prefix=blueprint.import_name) views.add('/more-advanced', 'views.advanced', endpoint='more_advanced', methods=('GET', 'POST', 'PUT')) self.app.register_blueprint(blueprint, url_prefix='/test') self.assertIn('{0}.more_advanced'.format(name), self.app.view_functions) response = self.client.put(self.url('{0}.more_advanced'.format(name))) self.assert200(response) self.assertContains(response, 'Advanced test page')
def test_custom_config_blueprint(self): name = self.get_blueprint_name() blueprint = init_blueprint(name) views = LazyViews(blueprint, import_prefix=blueprint.import_name) views.add('/more-advanced', 'views.advanced', endpoint='more_advanced', methods=('GET', 'POST', 'PUT')) self.app.register_blueprint(blueprint, url_prefix='/test') self.assertIn('{0}.more_advanced'.format(name), self.app.view_functions) response = self.client.put(self.url('{0}.more_advanced'.format(name))) self.assert200(response) self.assertContains(response, 'Advanced test page')
def test_custom_config_blueprint(self): views = LazyViews(blueprint, import_prefix=blueprint.import_name) views.add('/more-advanced', 'views.advanced', endpoint='more_advanced', methods=('GET', 'POST', 'PUT')) # Don't forget to re-register blueprint self.app.blueprints.pop('test') self.app.register_blueprint(blueprint, url_prefix='/test') self.assertIn('test.more_advanced', app.view_functions) client = self.app.test_client() response = client.put(self.url('test.more_advanced')) self.assert200(response) self.assertIn('Advanced test page', response.data)
def test_error_config_app(self): views = LazyViews(self.app, import_prefix='weird.path') views.add('/default-page', 'views.page', defaults={'page_id': 1}, endpoint='default_page') self.assertIn('default_page', self.app.view_functions) self.assertRaises(ImportStringError, self.client.get, self.url('default_page')) views = LazyViews(self.app, import_prefix='testapp.views') views.add('/another-default-page', 'does_not_exist', endpoint='another_default_page') self.assertIn('another_default_page', self.app.view_functions) self.assertRaises(ImportStringError, self.client.get, self.url('another_default_page'))
def create_blueprint(name=None): """ Factory for create blueprint. """ # Create blueprint name = name or 'testblueprint' blueprint = Blueprint(name, 'testapp.{0}'.format(name), template_folder='templates') # Initialize lazy views extension, register views and error handlers views = LazyViews(blueprint, import_prefix='.views') views.add('/', 'test') views.add('/advanced', 'advanced', methods=('GET', 'POST')) views.add('/error/<int:code>', 'custom_error') views.add('/gone', 'custom_error', defaults={'code': 410}, endpoint='gone') views.add_error(403, 'error') views.add_error(410, 'error', app=True) return blueprint
from flask.ext.dropbox import Dropbox from flask.ext.lazyviews import LazyViews from flask.ext.script import Manager import settings # Initialize and configure Flask app app = Flask(__name__) app.config.from_object(settings) # Setup Dropbox and script extensions dropbox = Dropbox(app) dropbox.register_blueprint(url_prefix='/dropbox') manager = Manager(app) # Add test project views views = LazyViews(app, 'testapp.views') views.add('/', 'home') views.add('/delete/<path:filename>', 'delete') views.add('/download/<path:filename>', 'download', endpoint='download') views.add('/files', 'files') views.add('/media/<path:filename>', 'download', defaults={'media': True}, endpoint='media') views.add('/session/clear', 'session_clear') views.add('/session/dump', 'session_dump') views.add('/success/<path:filename>', 'success') views.add('/upload', 'upload', methods=('GET', 'POST'))
from flask import Blueprint from flask.ext.lazyviews import LazyViews blueprint = Blueprint('core', __name__) views = LazyViews(blueprint, 'core.views') #views.add('/', 'index') views.add('/', 'example')
from flask import Blueprint from flask.ext.lazyviews import LazyViews blueprint = Blueprint('about', __name__) views = LazyViews(blueprint, 'about.views') views.add('/', 'index')
from flask.ext.flatpages import FlatPages from flask.ext.lazyviews import LazyViews from flask.ext.mail import Mail from flask.ext.script import Manager from learnpython import settings # Initialize Flask application app = Flask('learnpython') app.config.from_object(settings) # Configure all necessary plugins babel = Babel(app) mail = Mail(app) manager = Manager(app) pages = FlatPages(app) # Register all possible urls views = LazyViews(app, '.views') views.add('/', 'page', defaults={'name': 'index'}, endpoint='index') views.add('/contacts', 'contacts', methods=('GET', 'POST')) views.add('/flows', 'flows', endpoint='flows') views.add('/flows/archive/<int:archive>', 'flows', endpoint='flows_archive') views.add('/status', 'status') views.add('/subscribe', 'subscribe', methods=('GET', 'POST')) views.add_error(404, 'error') views.add_error(500, 'error') views.add_static('/favicon.ico', defaults={'filename': 'img/favicon.ico'}) views.add('/<path:name>', 'page')
import settings # Initialize and configure Flask app app = Flask(__name__) app.config.from_object(settings) # Setup Dropbox support dropbox = Dropbox(app) dropbox_blueprint = DropboxBlueprint(dropbox) app.register_blueprint(dropbox_blueprint, url_prefix='/dropbox') # Add test project views views = LazyViews(app) views.add('/', 'views.home') views.add('/delete/<path:filename>', 'views.delete') views.add('/download/<path:filename>', 'views.download', endpoint='download') views.add('/list', 'views.list') views.add('/media/<path:filename>', 'views.download', defaults={'media': True}, endpoint='media') views.add('/success/<path:filename>', 'views.success') views.add('/upload', 'views.upload', methods=('GET', 'POST')) if __name__ == '__main__': host = '0.0.0.0' port = 5000
from flask import Blueprint from flask.ext.lazyviews import LazyViews from .admin import AdminView blueprint = Blueprint('test', 'testapp.test', template_folder='templates') views = LazyViews(blueprint, import_prefix='.views') views.add('/', 'test') views.add('/advanced', 'advanced', methods=('GET', 'POST'))
import os import sys from flask import Flask from flask.ext.lazyviews import LazyViews from testapp.test import blueprint as test_blueprint # Init Flask application app = Flask(__name__) # Add url routes to application views = LazyViews(app) views.add('/', 'views.home') views.add('/page/<int:page_id>', 'views.page', endpoint='flatpage') # Register test blueprint app.register_blueprint(test_blueprint, url_prefix='/test') if __name__ == '__main__': host = '0.0.0.0' port = 5000 if len(sys.argv) == 2: mixed = sys.argv[1] try: host, port = mixed.split(':')
# -*- coding: utf-8 -*- from flask import Blueprint from flask.ext.lazyviews import LazyViews bp = Blueprint('html', __name__) views = LazyViews() views.init_blueprint(bp, '.view') views.add('/', 'home', methods=['GET'])
Created on Jul 30, 2012 @author: vmandal ''' from flask import Flask from flask.ext.dropbox import Dropbox, DropboxBlueprint from flask.ext.lazyviews import LazyViews import settings app = Flask(__name__) app.config.from_object(settings) dp = Dropbox(app) dropbox_blueprint = DropboxBlueprint(dp) app.register_blueprint(dropbox_blueprint, url_prefix='/dp') views = LazyViews(app) views.add('/', 'views.index') views.add('/session/clear', 'views.session_clear') views.add('/session/dump', 'views.session_dump') views.add('/upload', 'views.upload', methods=['GET', 'POST']) views.add('/media/<path:filename>', 'views.download', defaults={'media': True}, endpoint='media') if __name__ == '__main__': app.debug = True app.run()
def create_app(name=None, **options): """ Factory function to create Flask applictaion. """ # Init Flask application and configure it app = Flask(name or __name__) app.config.update({'DEBUG': True, 'SQLALCHEMY_DATABASE_URI': 'sqlite://'}) app.config.update(options) # Setup necessary extensions Admin(app), SQLAlchemy(app) # Add lazy views to application views = LazyViews(app, 'testapp') views.add('/', 'views.home') views.add('/db', 'views.database_page', endpoint='dbpage') views.add('/error', 'views.server_error') views.add('/error/default', 'views.custom_error', defaults={'code': 400}) views.add('/error/<int:code>', 'views.custom_error') views.add('/gone', 'views.custom_error', defaults={'code': 410}, endpoint='gone') views.add('/page/<int:page_id>', 'views.page', endpoint='flatpage') views.add('/page/<int:page_id>/cls', 'views.PageView', endpoint='flatpage_cls') # Admin view views.add_admin('admin.AdminView', endpoint='app_admin', name='Custom Admin Page') # Error handlers views.add_error(404, 'views.error') views.add_error(500, 'views.error') # Custom static file serving views.add_static('/favicon.ico', 'img/favicon.ico', endpoint='favicon') views.add_static('/old-favicon.ico', defaults={'filename': 'img/favicon.ico'}, endpoint='old_favicon') # Render templates with dict/callable context or without any context views.add_template('/template', 'template.html', context={'text': 'Test Text'}, endpoint='template') views.add_template('/template/callable-context', 'template.html', context=lambda: {'text': 'Callable Test Text'}, endpoint='template_callable_context') views.add_template('/template/no-context', 'template.html', endpoint='template_no_context') # Create and register test blueprint app.register_blueprint(create_blueprint(), url_prefix='/test') return app
from flask import Blueprint from flask.ext.lazyviews import LazyViews blueprint = Blueprint('core', __name__) views = LazyViews(blueprint, 'core.views') views.add('/', 'index')
def create_app(name=None, **options): """ Factory function to create Flask applictaion. """ # Init Flask application and configure it app = Flask(name or __name__) app.config.update({'DEBUG': True, 'SQLALCHEMY_DATABASE_URI': 'sqlite://'}) app.config.update(options) # Setup necessary extensions Admin(app), SQLAlchemy(app) # Add lazy views to application views = LazyViews(app, 'testapp') views.add('/', 'views.home') views.add('/db', 'views.database_page', endpoint='dbpage') views.add('/error', 'views.server_error') views.add('/error/default', 'views.custom_error', defaults={'code': 400}) views.add('/error/<int:code>', 'views.custom_error') views.add('/gone', 'views.custom_error', defaults={'code': 410}, endpoint='gone') views.add('/page/<int:page_id>', 'views.page', endpoint='flatpage') views.add('/page/<int:page_id>/cls', 'views.PageView', endpoint='flatpage_cls') # Admin view views.add_admin('admin.AdminView', endpoint='app_admin', name='Custom Admin Page') # Error handlers views.add_error(404, 'views.error') views.add_error(500, 'views.error') # Custom static file serving views.add_static('/favicon.ico', 'img/favicon.ico', endpoint='favicon') views.add_static('/old-favicon.ico', defaults={'filename': 'img/favicon.ico'}, endpoint='old_favicon') # Render templates with dict/callable context or without any context views.add_template('/template', 'template.html', context={'text': 'Test Text'}, endpoint='template') views.add_template('/template/callable-context', 'template.html', context=lambda: {'text': 'Callable Test Text'}, endpoint='template_callable_context') views.add_template('/template/no-context', 'template.html', endpoint='template_no_context') # Create and register test blueprint app.register_blueprint(create_blueprint(), url_prefix='/test') return app
from flask import Flask from flask.ext.dropbox import Dropbox from flask.ext.lazyviews import LazyViews from flask.ext.script import Manager from . import settings # Initialize and configure Flask app app = Flask(__name__) app.config.from_object(settings) # Setup Dropbox and script extensions dropbox = Dropbox(app) dropbox.register_blueprint(url_prefix='/dropbox') manager = Manager(app) # Add test project views views = LazyViews(app, 'testapp.views') views.add('/', 'home') views.add('/delete/<path:filename>', 'delete') views.add('/download/<path:filename>', 'download', endpoint='download') views.add('/files', 'files') views.add('/media/<path:filename>', 'download', defaults={'media': True}, endpoint='media') views.add('/session/clear', 'session_clear') views.add('/session/dump', 'session_dump') views.add('/success/<path:filename>', 'success') views.add('/upload', 'upload', methods=('GET', 'POST'))
import sys from flask import Flask from flask.ext.admin import Admin from flask.ext.lazyviews import LazyViews from flask.ext.script import Manager from testapp.test import blueprint as test_blueprint # Init Flask application and necessary extensions app = Flask(__name__) admin = Admin(app) manager = Manager(app) # Add url rules to application views = LazyViews(app) views.add('/', 'views.home') views.add('/error', 'views.server_error') views.add('/page/<int:page_id>', 'views.page', endpoint='flatpage') views.add('/page/<int:page_id>/cls', 'views.PageView', endpoint='flatpage_cls') views.add_admin('admin.AdminView', endpoint='app_admin', name='App View') views.add_error(404, 'views.error') views.add_error(500, 'views.error') views.add_static('/favicon.ico', defaults={'filename': 'img/favicon.ico'}, endpoint='favicon') # Register test blueprint app.register_blueprint(test_blueprint, url_prefix='/test')