### Minimal TurboGears Web Application Source: https://github.com/turbogears/tg2/blob/development/README.rst A basic TurboGears application demonstrating a simple 'Hello World' response. This example uses the MinimalApplicationConfigurator for a microframework setup. ```python from wsgiref.simple_server import make_server from tg import MinimalApplicationConfigurator from tg import expose, TGController # RootController of our web app, in charge of serving content for / class RootController(TGController): @expose(content_type="text/plain") def index(self): return 'Hello World' # Configure a new minimal application with our root controller. config = MinimalApplicationConfigurator() config.update_blueprint({ 'root_controller': RootController() }) # Serve the newly configured web application. print("Serving on port 8080...") httpd = make_server('', 8080, config.make_wsgi_app()) httpd.serve_forever() ``` -------------------------------- ### Install TurboGears Source: https://github.com/turbogears/tg2/blob/development/README.rst Install TurboGears using pip. Ensure you have pip installed first. ```bash curl -O 'https://bootstrap.pypa.io/get-pip.py' python get-pip.py pip install TurboGears2 ``` -------------------------------- ### Controller-wide Authorization Example Source: https://github.com/turbogears/tg2/blob/development/CHANGES.txt Demonstrates how to set controller-wide authorization using the 'allow_only' attribute and the '@authorize.require' decorator. This is an alternative to using the !SecureController class. ```python class SomeSecureController(BaseController): allow_only = predicates.has_permission('onePermission') @expose('my_package.template.index') def index(self): # do something here @expose('my_package.template.add') @authorize.require(predicates.has_permission('specialPerm')) def do_things(self, **kw): # do other things here ``` -------------------------------- ### Remove deprecated middleware setup function Source: https://github.com/turbogears/tg2/blob/development/CHANGES.txt Remove the import of `setup_tg_wsgi_app` from `tg.middleware` as it has been replaced by a method on `base_config`. ```python -from pylons.wsgiapp import PylonsApp -from tg.middleware import setup_tg_wsgi_app ``` -------------------------------- ### DBTest Example without explicit database definition Source: https://github.com/turbogears/tg2/blob/development/CHANGES.txt Shows how a DBTest subclass can be defined when 'sqlalchemy.url' is specified in the 'test.ini' file, making the explicit test database definition optional. ```python class TestModel(DBTest): """The base class for testing models in you TG project.""" model = model ``` -------------------------------- ### Evaluate repoze.what predicates without environ Source: https://github.com/turbogears/tg2/blob/development/CHANGES.txt Demonstrates how to evaluate repoze.what predicates directly. This is useful for checking conditions like user anonymity without needing the full request environment. ```python >>> from repoze.what.predicates import not_anonymous >>> p = non_anonymous() >>> bool(p) # Will return False if the user is anonymous; True otherwise False ``` -------------------------------- ### Replacing Old Expose with New Expose Source: https://github.com/turbogears/tg2/blob/development/CHANGES.txt Illustrates the change from the older 'expose' decorator to the new 'expose' decorator with an HTML template specified. ```python return dict(page='index') }}} ``` ```python @expose('index.html') def index(self): return dict(page='index') }}} ``` -------------------------------- ### Update expose decorator for new renderers Source: https://github.com/turbogears/tg2/blob/development/CHANGES.txt When using new-style renderers, provide the full filename including the extension in the `expose` decorator. The template directory is now registered in the search path. ```python @expose('testproject.templates.index') def index(self): ``` -------------------------------- ### Updating base_config in middleware.py Source: https://github.com/turbogears/tg2/blob/development/CHANGES.txt Demonstrates the necessary change in config/middleware.py to align with the updated base_config object in TurboGears 2. ```python -make_base_app = setup_tg_wsgi_app(load_environment, base_config) +make_base_app = base_config.setup_tg_wsgi_app(load_environment) ``` -------------------------------- ### Re-bind SQLAlchemy session in model initialization Source: https://github.com/turbogears/tg2/blob/development/CHANGES.txt Add a line to `yourapp.model.init_model` to bind the SQLAlchemy session to the provided engine, which is necessary for multi-database support. ```python def init_model(engine): DBSession.configure(bind=enigne) # <-- this, add this ``` -------------------------------- ### Updating base_config in environment.py Source: https://github.com/turbogears/tg2/blob/development/CHANGES.txt Shows the modification required in config/environment.py to adapt to changes in the base_config object for TurboGears 2. ```python -load_environment = make_load_environment(base_config) +load_environment = base_config.make_load_environment() ``` -------------------------------- ### Displaying Local Objects in TurboGears Source: https://github.com/turbogears/tg2/blob/development/tests/test.html This snippet shows how to display all objects from the local scope in TurboGears 2.0. It iterates through local variables and prints their representation. ```Python All objects from locals(): ========================== ${item}: ${repr(locals()['data'][item])} ================================================ ``` -------------------------------- ### Update module import for configuration Source: https://github.com/turbogears/tg2/blob/development/CHANGES.txt Change the import statement for configuration modules from `tg.config` to `tg.configuration`. ```python from tg.config import AppConfig, Bunch +from tg.configuration import AppConfig, Bunch ``` -------------------------------- ### Update authorization plugin configuration Source: https://github.com/turbogears/tg2/blob/development/CHANGES.txt Modify the configuration for the authorization plugin to use `user_class`, `group_class`, and `permission_class` instead of `user`. ```python -base_config.sa_auth.user = model.User +base_config.sa_auth.user_class = model.User base_config.sa_auth.user_criterion = model.User.user_name base_config.sa_auth.user_id_column = 'user_id' +base_config.sa_auth.group_class = model.Group +base_config.sa_auth.permission_class = model.Permission ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.