djblets.extensions.extension¶
Base classes for implementing extensions.
-
class
JSExtension
(extension)[source]¶ 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[source]¶ 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[source]¶ The list of URL names to load this extension on.
If not provided, this will be loaded on all pages.
-
__init__
(extension)[source]¶ Initialize the JavaScript extension.
Parameters: extension (Extension) – The main extension that owns this JavaScript extension.
-
applies_to
(url_name)[source]¶ Return whether this extension applies to the given URL name.
Parameters: url_name (unicode) – The name of the URL. Returns: True
if this JavaScript extension should load on the page with the given URL name.False
if it should not load.Return type: bool
-
get_model_data
(request, **kwargs)[source]¶ 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: dict
-
get_settings
()[source]¶ 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: dict
-
-
class
Extension
(extension_manager)[source]¶ 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
.Type: django.contrib.admin.AdminSite
-
extension_manager
¶ The extension manager that manages this extension.
Type: djblets.extensions.manager.ExtensionManager
-
hooks
¶ The hooks currently registered and enabled for the extension.
Type: set of djblets.extensions.hooks.ExtensionHook
-
middleware_instances
¶ The list of Django middleware instances. Each will be an instance of a class listed in
middleware
.Type: list of object
-
settings
¶ The settings for the extension.
Type: djblets.extensions.settings.ExtensionSettings
-
metadata
= None[source]¶ 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[source]¶ 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
= {}[source]¶ 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[source]¶ 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
= [][source]¶ 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
= [][source]¶ 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
= [][source]¶ 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
= [][source]¶ 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
= [][source]¶ A list of Django middleware to load.
Each of these will be added to
MIDDLEWARE_CLASSES
while the extension is enabled. It follows the same format as that setting.
-
css_bundles
= {}[source]¶ 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
= {}[source]¶ 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
= [][source]¶ A list of JavaScript extension classes to enable on pages.
Each entry in the list is a
JSExtension
subclass to load.
-
__init__
(extension_manager)[source]¶ 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
()[source]¶ 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
()[source]¶ Shut down the extension.
By default, this calls shutdown_hooks.
Subclasses should override this if they need custom shutdown behavior.
-
get_static_url
(path)[source]¶ 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.
Parameters: path (unicode) – The path within the static directory for the extension. Returns: The resulting static media URL. Return type: unicode
-
get_bundle_id
(name)[source]¶ 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.
Parameters: name (unicode) – The name of the bundle to reference. Returns: The ID of the bundle corresponding to the name. Return type: unicode
-
-
class
ExtensionInfo
(ext_class, package_name, metadata={})[source]¶ 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.
-
classmethod
create_from_entrypoint
(entrypoint, ext_class)[source]¶ 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={})[source]¶ 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
[source]¶ 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:
- unicode
-
has_resource
(path)[source]¶ Return whether an extension has a resource in its package.
A resource is a file or directory that exists within an extension’s package.
Parameters: path (unicode) – The /
-delimited path to the resource within the package.Returns: True
if the resource exits.False
if it does not.Return type: bool
-
extract_resource
(path)[source]¶ 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.
Parameters: path (unicode) – The /
-delimited path to the resource within the package.Returns: The local filesystem path to the resource, or None
if it could not be found.Return type: unicode
-
write_installed_static_version
()[source]¶ 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.
-
classmethod