|
| 1 | +.. _versioning_contract: |
| 2 | + |
| 3 | +Versioning contract |
| 4 | +=================== |
| 5 | + |
| 6 | +Django CMS uses a contract-based approach for versioning, allowing different versioning |
| 7 | +implementations to integrate with the CMS and its ecosystem. **djangocms-versioning defines |
| 8 | +the versioning contract** for django CMS. This section describes the contract that |
| 9 | +djangocms-versioning does implement and other versioning packages must implement to work |
| 10 | +with django CMS. |
| 11 | + |
| 12 | +Overview |
| 13 | +-------- |
| 14 | + |
| 15 | +The contract is implemented through django CMS's ``CMSAppExtension`` mechanism in the |
| 16 | +``cms_config.py`` module. When installed, djangocms-versioning becomes the versioning |
| 17 | +provider for all content types that register with it—including django CMS pages, |
| 18 | +aliases, stories, and any custom content models. |
| 19 | + |
| 20 | +.. note:: |
| 21 | + |
| 22 | + **djangocms-versioning** is the reference implementation endorsed by the django CMS |
| 23 | + Association. |
| 24 | + |
| 25 | +Contract definition |
| 26 | +------------------- |
| 27 | + |
| 28 | +The contract is defined in djangocms-versioning's ``cms_config.py`` using the |
| 29 | +``contract`` class attribute, a 2-tuple consisting of the contract name (``"djangocms_versioning"``) |
| 30 | +and the contract class (``VersionableItem``): |
| 31 | + |
| 32 | +.. code-block:: python |
| 33 | +
|
| 34 | + from cms.app_base import CMSAppExtension |
| 35 | + from .datastructures import VersionableItem |
| 36 | +
|
| 37 | + class VersioningCMSExtension(CMSAppExtension): |
| 38 | + contract = "djangocms_versioning", VersionableItem |
| 39 | +
|
| 40 | + def __init__(self): |
| 41 | + self.versionables = [] |
| 42 | +
|
| 43 | + def configure_app(self, cms_config): |
| 44 | + # Process the versioning configuration |
| 45 | + if hasattr(cms_config, "versioning"): |
| 46 | + self.handle_versioning_setting(cms_config) |
| 47 | + # ... additional setup |
| 48 | +
|
| 49 | +The ``contract`` attribute is a tuple of: |
| 50 | + |
| 51 | +1. The contract name (``"djangocms_versioning"``) |
| 52 | +2. The ``VersionableItem`` class that apps use to register their content models |
| 53 | + |
| 54 | +This allows other packages to register for versioning without importing directly from |
| 55 | +djangocms-versioning, enabling alternative implementations to provide the same contract. |
| 56 | + |
| 57 | +Contract components |
| 58 | +------------------- |
| 59 | + |
| 60 | +VersionableItem class |
| 61 | +~~~~~~~~~~~~~~~~~~~~~ |
| 62 | + |
| 63 | +The ``VersionableItem`` class defines how a content model participates in versioning. |
| 64 | +At minimum, it must accept: |
| 65 | + |
| 66 | +``content_model`` |
| 67 | + The Django model class that stores versioned content (the :term:`content model`). |
| 68 | + |
| 69 | +``grouper_field_name`` |
| 70 | + The name of the foreign key field on the content model that points to the |
| 71 | + :term:`grouper model`. |
| 72 | + |
| 73 | +``copy_function`` |
| 74 | + An (optional) callable that creates a copy of a content object when creating new versions. |
| 75 | + |
| 76 | +Additional optional parameters are djangocms-versioning-specific and may include: |
| 77 | + |
| 78 | +- ``extra_grouping_fields``: Additional fields for grouping versions (e.g., ``language``) |
| 79 | +- ``on_publish``, ``on_unpublish``, ``on_draft_create``, ``on_archive``: Lifecycle hooks |
| 80 | +- ``preview_url``: Function to generate preview URLs for versions |
| 81 | +- ``content_admin_mixin``: Custom admin mixin for the content model |
| 82 | +- ``grouper_admin_mixin``: Custom admin mixin for the grouper model |
| 83 | + |
| 84 | +Manager modifications |
| 85 | +~~~~~~~~~~~~~~~~~~~~~ |
| 86 | + |
| 87 | +A versioning package typically modifies the content model's managers: |
| 88 | + |
| 89 | +``objects`` manager |
| 90 | + Should filter to return only published content by default, ensuring unpublished |
| 91 | + content never leaks to the public. |
| 92 | + |
| 93 | +``admin_manager`` |
| 94 | + Should provide access to all content versions, for use in admin contexts only. |
| 95 | + |
| 96 | +These managers enable the pattern: |
| 97 | + |
| 98 | +.. code-block:: python |
| 99 | +
|
| 100 | + # Public queries - only published content |
| 101 | + PostContent.objects.filter(...) |
| 102 | +
|
| 103 | + # Admin queries - all versions accessible |
| 104 | + PostContent.admin_manager.filter(...) |
| 105 | +
|
| 106 | +Registration mechanism |
| 107 | +---------------------- |
| 108 | + |
| 109 | +Content models register for versioning via ``cms_config.py``: |
| 110 | + |
| 111 | +.. code-block:: python |
| 112 | +
|
| 113 | + # myapp/cms_config.py |
| 114 | + from cms.app_base import CMSAppConfig |
| 115 | +
|
| 116 | + from .models import MyContent |
| 117 | +
|
| 118 | +
|
| 119 | + class MyAppConfig(CMSAppConfig): |
| 120 | + djangocms_versioning_enabled = True # <contract>_enabled = True |
| 121 | +
|
| 122 | + def __init__(self, app): |
| 123 | + super().__init__(app) |
| 124 | +
|
| 125 | + # Dynamically get the installed contract object |
| 126 | + VersionableItem = self.get_contract("djangocms_versioning") |
| 127 | +
|
| 128 | + self.versioning = [ |
| 129 | + VersionableItem( |
| 130 | + content_model=MyContent, |
| 131 | + grouper_field_name="grouper", |
| 132 | + grouper_admin_mixin="__default__", |
| 133 | + ), |
| 134 | + ] |
| 135 | +
|
| 136 | +The ``djangocms_versioning_enabled = True`` attribute signals that this app wants to |
| 137 | +use the versioning extension. The ``get_contract("djangocms_versioning")`` call retrieves the |
| 138 | +``VersionableItem`` class from the installed versioning package, allowing the app to |
| 139 | +register its content models for versioning. |
| 140 | + |
| 141 | +Implementing alternative versioning packages |
| 142 | +-------------------------------------------- |
| 143 | + |
| 144 | +Alternative versioning packages must follow the contract defined by djangocms-versioning. |
| 145 | +Specifically, they must: |
| 146 | + |
| 147 | +1. **Export a VersionableItem class** (or compatible equivalent) that other packages |
| 148 | + can discover and use for registration. |
| 149 | + |
| 150 | +2. **Process the** ``versioning`` **attribute** from ``CMSAppConfig`` subclasses that |
| 151 | + have ``djangocms_versioning_enabled = True``. |
| 152 | + |
| 153 | +3. **Modify content model managers** to provide the ``objects`` / ``admin_manager`` |
| 154 | + pattern expected by django CMS and ecosystem packages. |
| 155 | + |
| 156 | +4. **(Optional) Provide version state information** for the CMS toolbar and admin |
| 157 | + interfaces. djangocms-versioning does this by injecting a ``content_indicator`` |
| 158 | + method onto content models that returns status strings (e.g., ``"published"``, |
| 159 | + ``"draft"``, ``"dirty"``). Alternative implementations may define their own states |
| 160 | + or omit this functionality. |
| 161 | + |
| 162 | +Ecosystem compatibility |
| 163 | +----------------------- |
| 164 | + |
| 165 | +Packages in the django CMS ecosystem (such as djangocms-alias and djangocms-stories) |
| 166 | +register their content models using the versioning contract. When you install a |
| 167 | +versioning package, it becomes responsible for managing versions of *all* registered |
| 168 | +content types. |
| 169 | + |
| 170 | +This means: |
| 171 | + |
| 172 | +- Switching versioning packages affects all versioned content across your site |
| 173 | +- Alternative implementations must handle registrations from ecosystem packages |
| 174 | +- The version states and workflow defined by your versioning package apply universally |
| 175 | + |
| 176 | +See also |
| 177 | +-------- |
| 178 | + |
| 179 | +- :doc:`/introduction/versioning_integration` for integrating your models with versioning |
| 180 | +- :doc:`advanced_configuration` for customizing versioning behavior |
| 181 | +- :doc:`models` for the Version model reference |
0 commit comments