|
| 1 | +.. _alternative_admin: |
| 2 | + |
| 3 | +The Admin with Versioning |
| 4 | +========================= |
| 5 | + |
| 6 | +Versioning in django CMS provides powerful tools to manage content and grouper models in the admin interface. |
| 7 | +This chapter explains the default patterns and customization options for integrating versioning into your admin |
| 8 | +classes. |
| 9 | + |
| 10 | +Proxy models of :class:`djangocms_versioning.models.Version` are generated for each registered content model, |
| 11 | +allowing customization of the version table by model. |
| 12 | + |
| 13 | + |
| 14 | +Default Pattern |
| 15 | +--------------- |
| 16 | + |
| 17 | +The default pattern is to set the ``grouper_admin_mixin`` property to ``"__default__"``, which applies the |
| 18 | +:class:`~djangocms_versioning.admin.DefaultGrouperVersioningAdminMixin` to the grouper model admin. This mixin |
| 19 | +ensures that state indicators and admin list actions are displayed consistently. |
| 20 | + |
| 21 | +Admin Options Overview |
| 22 | +----------------------- |
| 23 | + |
| 24 | +.. list-table:: Overview on versioning admin options: Grouper models |
| 25 | + :widths: 25 75 |
| 26 | + :header-rows: 1 |
| 27 | + |
| 28 | + * - Versioning state |
| 29 | + - Grouper Model Admin |
| 30 | + * - **Default**: Indicators, drop down menu |
| 31 | + - .. code-block:: python |
| 32 | + |
| 33 | + class GrouperAdmin( |
| 34 | + DefaultGrouperVersioningAdminMixin, |
| 35 | + GrouperModelAdmin |
| 36 | + ): |
| 37 | + list_display = ... |
| 38 | + * - Indicators, drop down menu (fix the current default) |
| 39 | + - .. code-block:: python |
| 40 | + |
| 41 | + class GrouperAdmin( |
| 42 | + ExtendedGrouperVersionAdminMixin, |
| 43 | + StateIndicatorMixin, |
| 44 | + GrouperModelAdmin |
| 45 | + ): |
| 46 | + list_display = ... |
| 47 | + * - Text, no interaction |
| 48 | + - .. code-block:: python |
| 49 | + |
| 50 | + class GrouperAdmin( |
| 51 | + ExtendedGrouperVersionAdminMixin, |
| 52 | + GrouperModelAdmin |
| 53 | + ): |
| 54 | + list_display = ... |
| 55 | + |
| 56 | +.. list-table:: Overview on versioning admin options: Content models |
| 57 | + :widths: 25 75 |
| 58 | + :header-rows: 1 |
| 59 | + |
| 60 | + * - Versioning state |
| 61 | + - **Content Model Admin** |
| 62 | + * - Text, no interaction |
| 63 | + - .. code-block:: python |
| 64 | + |
| 65 | + class ContentAdmin( |
| 66 | + ExtendedVersionAdminMixin, |
| 67 | + admin.ModelAdmin |
| 68 | + ) |
| 69 | + * - Indicators, drop down menu |
| 70 | + - .. code-block:: python |
| 71 | + |
| 72 | + class ContentAdmin( |
| 73 | + ExtendedIndicatorVersionAdminMixin, |
| 74 | + admin.ModelAdmin, |
| 75 | + ) |
| 76 | + |
| 77 | +Adding Versioning to Content Model Admins |
| 78 | +----------------------------------------- |
| 79 | + |
| 80 | +The :term:`ExtendedVersionAdminMixin` provides fields and actions related to versioning, such as: |
| 81 | + |
| 82 | +* Author |
| 83 | +* Modified date |
| 84 | +* Versioning state |
| 85 | +* Preview action |
| 86 | +* Edit action |
| 87 | +* Version list action |
| 88 | + |
| 89 | +Example: |
| 90 | + |
| 91 | +.. code-block:: python |
| 92 | +
|
| 93 | + class PostContentAdmin(ExtendedVersionAdminMixin, admin.ModelAdmin): |
| 94 | + list_display = ["title"] |
| 95 | +
|
| 96 | +The :term:`ExtendedVersionAdminMixin` also has functionality to alter fields from other apps. By adding the :term:`admin_field_modifiers` to a given apps :term:`cms_config`, |
| 97 | +in the form of a dictionary of {model_name: {field: method}}, the admin for the model, will alter the field, using the method provided. |
| 98 | + |
| 99 | +.. code-block:: python |
| 100 | +
|
| 101 | + # cms_config.py |
| 102 | + def post_modifier(obj, field): |
| 103 | + return obj.get(field) + " extra field text!" |
| 104 | +
|
| 105 | + class PostCMSConfig(CMSAppConfig): |
| 106 | + # Other versioning configurations... |
| 107 | + admin_field_modifiers = [ |
| 108 | + {PostContent: {"title": post_modifier}}, |
| 109 | + ] |
| 110 | +
|
| 111 | +Given the code sample above, "This is how we add" would be displayed as |
| 112 | +"this is how we add extra field text!" in the changelist of PostAdmin. |
| 113 | + |
| 114 | +Adding State Indicators |
| 115 | +------------------------- |
| 116 | + |
| 117 | +djangocms-versioning provides status indicators for django CMS' content models, you may know them from the page tree in django-cms: |
| 118 | + |
| 119 | +.. image:: static/Status-indicators.png |
| 120 | + :width: 50% |
| 121 | + |
| 122 | +You can use these on your content model's changelist view admin by adding the following fixin to the model's Admin class: |
| 123 | + |
| 124 | +.. code-block:: python |
| 125 | +
|
| 126 | + class MyContentModelAdmin(StateIndicatorMixin, admin.ModelAdmin): |
| 127 | + list_display = [..., "state_indicator", ...] |
| 128 | +
|
| 129 | +.. note:: |
| 130 | + |
| 131 | + For grouper models, ensure that the admin instance defines properties for each extra grouping field (e.g., ``self.language``). |
| 132 | + If you derive your admin class from :class:`~cms.admin.utils.GrouperModelAdmin`, this behavior is automatically handled. |
| 133 | + |
| 134 | + Otherwise, this is typically set in the ``get_changelist_instance`` method, e.g., by getting the language from the request. The page |
| 135 | + tree, for example, keeps its extra grouping field (language) as a get parameter to avoid mixing language of the user interface and |
| 136 | + language that is changed. |
| 137 | + |
| 138 | + .. code-block:: python |
| 139 | +
|
| 140 | + def get_changelist_instance(self, request): |
| 141 | + """Set language property and remove language from changelist_filter_params""" |
| 142 | + if request.method == "GET": |
| 143 | + request.GET = request.GET.copy() |
| 144 | + for field in versionables.for_grouper(self.model).extra_grouping_fields: |
| 145 | + value = request.GET.pop(field, [None])[0] |
| 146 | + # Validation is recommended: Add clean_language etc. to your Admin class! |
| 147 | + if hasattr(self, f"clean_{field}"): |
| 148 | + value = getattr(self, f"clean_{field}")(value): |
| 149 | + setattr(self, field) = value |
| 150 | + # Grouping field-specific cache needs to be cleared when they are changed |
| 151 | + self._content_cache = {} |
| 152 | + instance = super().get_changelist_instance(request) |
| 153 | + # Remove grouping fields from filters |
| 154 | + if request.method == "GET": |
| 155 | + for field in versionables.for_grouper(self.model).extra_grouping_fields: |
| 156 | + if field in instance.params: |
| 157 | + del instance.params[field] |
| 158 | + return instance |
| 159 | +
|
| 160 | +
|
| 161 | +Combining Status Indicators and Versioning |
| 162 | +------------------------------------------ |
| 163 | + |
| 164 | +To combine both status indicators and versioning fields, use the :class:`~djangocms_versioning.admin.ExtendedIndicatorVersionAdminMixin`: |
| 165 | + |
| 166 | +.. code-block:: python |
| 167 | +
|
| 168 | + class MyContentModelAdmin(ExtendedIndicatorVersionAdminMixin, admin.ModelAdmin): |
| 169 | + ... |
| 170 | +
|
| 171 | +The versioning state and version list action are replaced by the status indicator and its context menu, respectively. |
| 172 | + |
| 173 | +Add additional actions by overwriting the ``self.get_list_actions()`` method and calling ``super()``. |
| 174 | + |
| 175 | +Adding Versioning to Grouper Model Admins |
| 176 | +----------------------------------------- |
| 177 | + |
| 178 | +For grouper models, use the :class:`~djangocms_versioning.admin.ExtendedGrouperVersionAdminMixin` to add versioning fields: |
| 179 | + |
| 180 | +.. code-block:: python |
| 181 | +
|
| 182 | + class PostAdmin(ExtendedGrouperVersionAdminMixin, GrouperModelAdmin): |
| 183 | + list_display = ["title", "get_author", "get_modified_date", "get_versioning_state"] |
| 184 | +
|
| 185 | +To also add state indicators, include the :class:`~djangocms_versioning.admin.StateIndicatorMixin`: |
| 186 | + |
| 187 | +.. code-block:: python |
| 188 | +
|
| 189 | + class PostAdmin(ExtendedGrouperVersionAdminMixin, StateIndicatorMixin, GrouperModelAdmin): |
| 190 | + list_display = ["title", "get_author", "get_modified_date", "state_indicator"] |
0 commit comments