djblets.extensions.extension¶
Base classes for implementing extensions.
- class JSExtension(extension)¶
Bases:
object
Base class for a JavaScript extension.
This can be subclassed to provide the information needed to initialize a JavaScript extension.
The JSExtension subclass is expected to define a
model_class
attribute naming its JavaScript counterpart. This would be the variable name for the (uninitialized) model for the extension, defined in a JavaScript bundle.It may also define
apply_to
, which is a list of URL names that the extension will be initialized on. If not provided, the extension will be initialized on all pages.To provide additional data to the model instance, the JSExtension subclass can implement
get_model_data()
and return a dictionary of data to pass. You may also override theget_settings()
method to return, a dict of settings to themodel_class
. By default, the associated extension’s settings are returned.- model_class = None¶
The name of the JavaScript model class to instantiate.
This class will be instantiated on the page. It should be a subclass of
Djblets.Extension()
.
- apply_to = None¶
The list of URL names to load this extension on.
If not provided, this will be loaded on all pages.
- __init__(extension)¶
Initialize the JavaScript extension.
- Parameters:
extension (
Extension
) – The main extension that owns this JavaScript extension.
- applies_to(url_name)¶
Return whether this extension applies to the given URL name.
- get_model_data(request, **kwargs)¶
Return model data for the Extension model instance in JavaScript.
Subclasses can override this to return custom data to pass to the extension class defined in
model_class
. This data must be JSON-serializable.- Parameters:
request (
django.http.HttpRequest
) – The HTTP request from the client.- Returns:
Model data to pass to the constructor of the JavaScript extension class.
- Return type:
- get_settings()¶
Return the settings for the JS Extension.
By default, this is the associated
Extension
object’s settings. Subclasses may override this method to provide different settings.These settings will be provided to the
model_class
as asettings
key in its initialization options.- Returns:
The extension settings.
- Return type:
- class Extension(extension_manager)¶
Bases:
object
Base class for an extension.
Extensions must subclass this class. They’ll automatically have support for settings, adding hooks, and plugging into the administration UI.
For information on writing extensions, see Writing Extensions.
- admin_site¶
The database administration site set for the extension. This will be set automatically if
has_admin_site`
isTrue
.
- extension_manager¶
The extension manager that manages this extension.
- hooks¶
The hooks currently registered and enabled for the extension.
- middleware_classes¶
The list of new-style (Django 1.10+) middleware classses.
New in version 2.2.4.
- Type:
list
ofcallable
- settings¶
The settings for the extension.
- metadata = None¶
Metadata describing the package.
This is used to set the name, version, and other information for the extension. This data defaults to coming from the extension’s Python package metadata, but can be set on the extension itself. You would want to use this if shipping multiple extensions in a single package, if you want to include a space in the package name, or if you want the version to be set independently of the package, for example.
The following keys are supported:
Name
: The displayed name of the extension.Version
: The version of the extension.Summary
: A summary of the extension.Description
: A more detailed description of the extension.License
: The license the extension was released under.Author
: The name of the author writing/maintaining the extension.Author-email
: The e-mail address of the author writing/maintaining the extension.Author-home-page
: The URL of the author writing/maintaining the extension.Home-page
: The URL of the extension’s home/product/company page.
- is_configurable = False¶
Whether or not the extension is user-configurable.
If
True
, the extension will have a Configure link in the extension list that will take the user to a page for modifying extension settings. The extension must provide aadmin_urls.py
file defining a top-level URL (^$
) pointing to a view that will handle configuration.
- default_settings = {}¶
Default settings for the extension.
These values will be used when looking up keys in
settings
that don’t have custom values saved in the database.
- has_admin_site = False¶
Whether or not the extension has a database administration site.
If
True
, the extension will have a Database link in the extension list that will take the user to a page for adding/editing any database models registered by the extension.
- requirements = []¶
A list of any extension IDs to enable when this extension is enabled.
This is used to ensure that another extension is enabled before this one. It’s primarily for extensions that are augmenting or depending on another extension’s functionality.
- resources = []¶
A list of API resource instances offered by this extension.
Each entry in the list is an instance of a custom
WebAPIResource
. These resources will appear underneath the extension’s own resource.
- apps = []¶
A list of Django application module paths to load.
Each of these will be added to
INSTALLED_APPS
while the extension is enabled. It follows the same format as that setting.
- context_processors = []¶
A list of Django context processors to load.
Each of these will be added to
TEMPLATE_CONTEXT_PROCESSORS
while the extension is enabled. It follows the same format as that setting.
- middleware = []¶
A list of Django middleware to load.
Each of these will be run as if they were part of
MIDDLEWARE
depending on your setup) while the extension is enabled. It follows the same format as that setting.
- css_bundles = {}¶
A dictionary of CSS bundles to include in the package.
These will be automatically packaged along with the extension, and can be loaded in templates using
{% ext_css_bundle %}
.Each key in the dictionary is the name of the bundle, and the value is a dictionary containing a
source_filenames
key pointing to a list of CSS/LessCSS files.A special bundle ID of
default
will cause the CSS to be loaded on every page.
- js_bundles = {}¶
A dictionary of JavaScript bundles to include in the package.
These will be automatically packaged along with the extension, and can be loaded in templates using
{% ext_js_bundle %}
.Each key in the dictionary is the name of the bundle, and the value is a dictionary containing a
source_filenames
key pointing to a list of JavaScript files.A special bundle ID of
default
will cause the JavaScript to be loaded on every page.
- js_extensions = []¶
A list of JavaScript extension classes to enable on pages.
Each entry in the list is a
JSExtension
subclass to load.
- __init__(extension_manager)¶
Initialize the extension.
Subclasses should not override this. Instead, they should override
initialize()
.- Parameters:
extension_manager (
djblets.extensions.manager.ExtensionManager
) – The extension manager that manages this extension.
- initialize()¶
Initialize the extension.
Subclasses can override this to provide any custom initialization. They do not need to call the parent function, as it does nothing.
- shutdown()¶
Shut down the extension.
By default, this calls shutdown_hooks.
Subclasses should override this if they need custom shutdown behavior.
- shutdown_hooks()¶
Shut down all hooks for the extension.
- get_static_url(path)¶
Return the URL to a static media file for this extension.
This takes care of resolving the static media path name to a path relative to the web server. If a versioned media file is found, it will be used, so that browser-side caching can be used.
- get_bundle_id(name)¶
Return the ID for a CSS or JavaScript bundle.
This ID should be used when manually referencing the bundle in a template. The ID will be unique across all extensions.
- admin_urlconf()¶
The module defining URLs for the extension’s admin site.
- class ExtensionInfo(ext_class, package_name, metadata={})¶
Bases:
object
Information on an extension.
This class stores the information and metadata on an extension. This includes the name, version, author information, where it can be downloaded, whether or not it’s enabled or installed, and anything else that may be in the Python package for the extension.
- encodings = ['utf-8', 'UTF-8', 'latin1']¶
- classmethod create_from_entrypoint(entrypoint, ext_class)¶
Create a new ExtensionInfo from a Python EntryPoint.
This will pull out information from the EntryPoint and return a new ExtensionInfo from it.
It handles pulling out metadata from the older
PKG-INFO
files and the newerMETADATA
files.- Parameters:
- Returns:
An ExtensionInfo instance, populated with metadata from the package.
- Return type:
- __init__(ext_class, package_name, metadata={})¶
Instantiate the ExtensionInfo using metadata and an extension class.
This will set information about the extension based on the metadata provided by the caller and the extension class itself.
- Parameters:
- Raises:
TypeError – The parameters passed were invalid (they weren’t a new-style call or a legacy entrypoint-related call).
- installed_static_version_path()¶
The path to the static media version file.
This file records the version of the extension used when last installing the static media files.
- Type:
- has_resource(path)¶
Return whether an extension has a resource in its package.
A resource is a file or directory that exists within an extension’s package.
- extract_resource(path)¶
Return the filesystem path to an extracted resource.
A resource is a file or directory that exists within an extension’s package.
This will extract the resource from the package, if the package is compressed, and then return the local path to the file on the filesystem.
- write_installed_static_version()¶
Write the extension’s current static media version to disk.
This will write the extension’s current version in its static media directory, creating that directory if necessary. This will allow the extension manager to check if new media files need to be installed.
- Raises:
djblets.extensions.errors.InstallExtensionMediaError – There was an error writing the version to the static media directory. Details are in the error message.
- get_installed_static_version()¶
Return the extension’s locally-written static media version.
- Returns:
The extension version written to disk, or
None
if it didn’t exist or couldn’t be read.- Return type:
- __str__()¶
Return str(self).