Package: tgext.pluggable
About Pluggable Appstgext.pluggable permits to plug extensions and applications inside a TG projects
much like the Django Apps.Installingtgext.pluggable can be installed both from pypi or from bitbucket:pip install tgext.pluggableshould just work for most of the usersPlugging AppsIn your applicationconfig/app_cfg.pyimportplug:from tgext.pluggable import plugThen at theend of the filecall plug for each pluggable
application you want to enable (package_name must be
already installed in your python environment):plug(base_config, 'package_name')The plug function accepts various optional arguments, for
example if the plugged application exposes a controller
you can mount it in a different place specifying a differentappid:plug(base_config, 'package_name', 'new_app_id')If you want to mount the pluggable application in a subcontroller
you can used a dottedappid, likesubcontroller.appid.
Note thatsubcontrollermust exist in RootController.Other options include:plug_helpers (True/False) -> Enable helpers injectionplug_models (True/False) -> Enable models pluggingplug_controller (True/False) -> Mount pluggable app root controllerplug_bootstrap (True/False) -> Enable websetup.bootstrap pluggingplug_statics (True/False) -> Enable plugged app staticsrename_tables (True/False) -> Rename pluggable tables by prepending appid.Relations with Plugged Apps ModelsThere are cases when you might need to create a relationship or a foreign key
with a model which is defined by a pluggable application. As pluggable application
models are loaded after loading your application they are not available at the
time your app models are imported.tgext.pluggableprovides some utilities to make easier to create relations
with models defined by pluggable applications.The first step you might want to take is setting theglobal_models=Trueparameter to theplugcall, this will make all the models declared by the
pluggable application available to you:plug(base_config, 'package_name', global_models=True)After the specified pluggable application is plugged, the models will be available
inside your code through thetgext.pluggable.app_modelobject.Then you can create foreign keys to the desired model using thetgext.pluggable.LazyForeignKeyclass and declare relations using the lazy
version ofsqlalchemy.orm.relation:from tgext.pluggable import app_model, LazyForeignKey

class AdditionalInfo(DeclarativeBase):
    __tablename__ = 'sample_model'

    uid = Column(Integer, primary_key=True)
    data = Column(Unicode(255), nullable=False)

    plugged_model_id = Column(Integer, LazyForeignKey(lambda:app_model.PluggedModel.uid))
    plugged_model = relation(lambda: app_model.PluggedModel)Partialstgext.pluggables provides a bunch of utilities to work with partials.
Partials in tgext.pluggable can be declared as a function or TGController
subclass method that has an@exposedecorator. Those partials can lately
be rendered with:${h.call_partial('module:function_name', arg1='Something')}In the case of a class method:${h.call_partial('module.Class:method', arg1='Something')}The quickstarted pluggable application provides an example partial:from tg import expose

@expose('plugappname.templates.little_partial')
def something(name):
    return dict(name=name)which can be rendered using:${h.call_partial('plugappname.partials:something', name='Partial')}Replacing Templatestgext.pluggable provides a function to replace templates.
This is useful when you want to override the template that an application
you plugged in is exposing. To override callreplace_templateinside
your application config:from tgext.pluggable import replace_template

replace_template(base_config, 'myapp.templates.about', 'myapp.templates.index')replace_templatewill work even with tgext.pluggable partials, but
won’t work with templates rendered directly calling therendermethod.Calls to replace_template must be performed before the application has started.Patching Templatestgext.pluggable provides a function to patch templates, the result
of a template rendering will be passed through a list of operations which will
make possible to alter the rendering result.This behavior is much inspired byDeliverancehttp://pythonhosted.org/Deliverancemeant for much simpler use cases. The most common usage is for small changes to templates
of plugged applications. For advanced manipulations usingreplace_templateis suggested
as it’s both faster and easier to maintain.Template patching is enabled by using theload_template_patchesfunction:from tgext.pluggable import load_template_patches

load_template_patches(base_config)Supposing your project is inside a Python distribution namedmyappthis will
load thetemplate_patches.xmlfile from the root of the distribution and will
apply all the specified patches.To load template patches from a python module (or pluggable) use:load_template_patches(base_config, 'plugname')You can use previous expression even to load patches from your own application
in case the distribution automatic detection failed.Template patching format is an xml file in the form of:<patches>
  <patch template="tgext.crud.templates.get_all">
    <content selector="#crud_content > h1" template="myapp.templates.replacements.crud_title" />
    <append  selector="#crud_content > h1" template="myapp.templates.replacements.crud_subtitle" />
    <prepend selector="#crud_content > h1" template="myapp.templates.replacements.crud_superscript" />
    <replace selector="#crud_btn_new > .add_link" template="" />
  </patch>
</patches>Each action listed inside the patch will be performed whenever the specified template
is rendered, the template associated to the action will be used as the content of the templacement
and the same data available to the patched template will be available to the action template too.
Available actions are:content- replaces the content of tags identified by the selectorappend- appends after the tags identified by the selectorprepend- prepends before the tags identified by the selectorreplace- replaces the tags identified bt the selector.Creating Pluggable Appstgext.pluggable provides aquickstart-pluggablecommand
to create a new pluggable application:$ gearbox quickstart-pluggable plugtest
Enter package name [plugtest]:
...The quickstarted application will provide an example on how to use
models, helpers, bootstrap, controllers and statics.In the previous example the pluggable application can be enabled
inside any TurboGears using:plug(base_config, 'plugtest')After enabling theplugtestapplication you should rungearbox setup-appinside your TurboGears project
to create the sample model. Then you can access the sample
application page thoughhttp://localhost:8080/plugtestThe plugme Entry PointPluggable applications are required to implement aplugme(app_config, options)entry
point which will be called when plugging the application.The plugme action is called before TurboGears configuration has been loaded so that
it is possible to register more pluggables inside the plugme hook. This way a pluggable
can plug any dependency it requires just by calling tgext.pluggable.plug inside its ownplugmefunction.Any options passed to the plug call will be available inside the options dictionary,
other parts of the pluggable applications like controllers, models and so on will be
imported after the call to plugme so that plugme can set any configuration options that
will drive the behavior of the other parts.Keep in mind that as plugme is called before loading the TurboGears configuration if you
need to perform something based on any configuration file option you must register asetupfrom the plugme call and perform them there.Changing Static Files BehaviorBy default every pluggable application serves all the static files available inside
its public directory as they are. This is performed by a WSGI application which is
in charge of serving the static files. Since version 0.2.1 it is now possible to
replace this application or apply any WSGI middleware to it through thestatic_middlewaresoption.For example you can enable SCSS inside your pluggable application by
defining aplugmefunction like:from tgext.scss import SCSSMiddleware

def plugme(app_config, options):
    return dict(appid='plugtest', global_helpers=False, static_middlewares=[SCSSMiddleware])Accessing Application Models from Pluggable AppsWhen creating a pluggable application you might often need to
access to some models that have been declared inside the
target application where the pluggable app will be mounted.The most common use case for this is referencing the User, Group and Permission
models. To do this tgext.pluggable provides anapp_modelobject which
wraps the application model and is initialized before loading the pluggable app.This makes possible to access target application models referencing
them asapp_model.Userorapp_model.Groupand so on.
While you can guess that the primary key for those models is known
(for the app_model.User object for example you might consider referencing
to it as app_model.User.user_id) it is best practice to call theprimary_keyfunction provided by tgext.pluggable to get a reference to its column.This way it is possibile to declare relations to models which are not
provided by your pluggable app:from tgext.pluggable import app_model, primary_key

user_id = Column(Integer, ForeignKey(primary_key(app_model.User)))
user = relation(app_model.User)Pluggable Relative UrlsIt is possible to generate an url relative to a pluggable mount point
using theplug_url(pluggable, path, params=None, lazy=False)this
function is also exposed inside the application helpers when a pluggable
is used. For example to generate an url relative to theplugtestpluggable
it is possible to call plug_url:plug_url('plugtest', '/')To perform redirects inside a pluggable app theplug_redirect(pluggable, path, params=None)function is provided. This function exposes the same interface asplug_urlbut
performs a redirect much like tg.redirect.Internationalizationtgext.pluggable provides some utilities for to manage text translations inside
pluggables. Whentg.i18n.ugettextortg.i18n.lazy_ugettextare used
they will lookup for translations inside the Application and when not available
will fallback to the translations provided by the pluggable itself.Messages extration and catalog creation/update for the pluggable work as in TurboGears
using Babel.
Just run inside the pluggable application thepython setup.py extract_messages,python setup.py init_catalog-lLANGandpython setup.py compile_catalogcommands to create a catalog forLANG.Just distribute the catalogs with your pluggable application to make them
available and translated in applications that use it.Managing MigrationsIt is possible to initialize a migrations repository for a pluggable application.
This makes possible to evolve the database at later times for each pluggable application using
thealembicmigration
library for SQLAlchemy.Create Migration RepositoryTo be able to manage migrations the pluggable has to be initialized with a migration repository
to perform so, the author of the pluggable application has to run:$ gearbox migrate-pluggable plugtest initThen to create migration scripts run:$ gearbox migrate-pluggable plugtest create 'Add column for user_name'A file named like2c8c79324a5e_Add_column_for_user_name.pywill be available inside themigration/versionsdirectory
of the pluggable application.Remember to add this directory to your distribution package to make it available to users of your pluggable applicationUsing MigrationsIf the pluggable application your are using supports migrations it is possible to apply them
using theupgradeanddowngradecommands.It is possible to runupgradeto move forward:$ gearbox migrate-pluggable plugtest upgrade
22:11:28,029 INFO  [alembic.migration] Running upgrade None -> 3ca22a16fdccOrdowngradeto revert a migration:$ gearbox migrate-pluggable plugtest downgrade
22:15:24,004 INFO  [alembic.migration] Running downgrade 3ca22a16fdcc -> NoneThe versioning commands support being called on all the pluggables enabled inside your application
by specifyingallas the pluggable name. This will load your application to detect the plugged
apps and will run the specified command for each one of them:$ gearbox migrate-pluggable all db_version
22:15:54,104 INFO  [tgext.pluggable] Plugging plug1
22:15:54,105 INFO  [tgext.pluggable] Plugging plug2
22:15:54,106 INFO  [tgext.pluggable] Plugging plug3
Migrating plug1, plug3, plug2

plug1 Migrations
    Repository '/tmp/PLUGS/plug1/migration'
    Configuration File 'development.ini'
    Versioning Table 'plug1_migrate'
22:15:54,249 INFO  [alembic.migration] Context impl SQLiteImpl.
22:15:54,249 INFO  [alembic.migration] Will assume transactional DDL.
Current revision for sqlite:////tmp/provaapp/devdata.db: 4edef05cc346 -> 1ae930148d69 (head), fourth migration

plug3 Migrations
    Repository '/tmp/PLUGS/plug3/migration'
    Configuration File 'development.ini'
    Versioning Table 'plug3_migrate'
22:15:54,253 INFO  [alembic.migration] Context impl SQLiteImpl.
22:15:54,254 INFO  [alembic.migration] Will assume transactional DDL.
Current revision for sqlite:////tmp/provaapp/devdata.db: 15819683bb72 -> 453f571f41e4 (head), test migration

plug2 Migrations
    Repository '/tmp/PLUGS/plug2/migration'
    Configuration File 'development.ini'
    Versioning Table 'plug2_migrate'
22:15:54,258 INFO  [alembic.migration] Context impl SQLiteImpl.
22:15:54,259 INFO  [alembic.migration] Will assume transactional DDL.
Current revision for sqlite:////tmp/provaapp/devdata.db: 154b4f69cbd1 -> 2c8c79324a5e (head), third migration