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 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
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_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 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
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')