Add docs for DraftStateMixin

pull/8994/head
Sage Abdullah 2022-08-15 14:02:24 +07:00 zatwierdzone przez LB (Ben Johnston)
rodzic cf3cea9a5b
commit fb6ec3ad00
2 zmienionych plików z 81 dodań i 13 usunięć

Wyświetl plik

@ -523,6 +523,66 @@ The model is added to allow Snippets to save revisions, revert to a previous rev
.. automethod:: with_content_json
```
## `DraftStateMixin`
`DraftStateMixin` is an abstract model that can be added to any non-page Django model to allow its instances to have unpublished changes.
This mixin requires {class}`~wagtail.models.RevisionMixin` to be applied. Pages already include this mixin, so there is no need to add it.
```{versionadded} 4.0
The model is added to allow Snippets to have changes that are not immediately reflected to the instance.
```
### Database fields
```{eval-rst}
.. class:: DraftStateMixin
.. attribute:: live
(boolean)
A boolean that is set to ``True`` if the object is published.
Note: this field defaults to ``True`` meaning that any objects that are created programmatically will be published by default.
.. attribute:: live_revision
(foreign key to :class:`~wagtail.models.Revision`)
This points to the revision that is currently live.
.. attribute:: has_unpublished_changes
(boolean)
A boolean that is set to ``True`` when the object is either in draft or published with draft changes.
.. attribute:: first_published_at
(date/time)
The date/time when the object was first published.
.. attribute:: last_published_at
(date/time)
The date/time when the object was last published.
```
### Methods and properties
```{eval-rst}
.. class:: DraftStateMixin
:noindex:
.. automethod:: publish
.. automethod:: unpublish
.. automethod:: with_content_json
```
(revision_model_ref)=
## `Revision`

Wyświetl plik

@ -480,6 +480,17 @@ class DraftStateMixin(models.Model):
def publish(
self, revision, user=None, changed=True, log_action=True, previous_revision=None
):
"""
Publish a revision of the object by applying the changes in the revision to the live object.
:param revision: Revision to publish.
:type revision: Revision
:param user: The publishing user.
:param changed: Indicated whether content has changed.
:param log_action: Flag for the logging action, pass ``False`` to skip logging.
:param previous_revision: Indicates a revision reversal. Should be set to the previous revision instance.
:type previous_revision: Revision
"""
return PublishRevisionAction(
revision,
user=user,
@ -489,6 +500,14 @@ class DraftStateMixin(models.Model):
).execute()
def unpublish(self, set_expired=False, commit=True, user=None, log_action=True):
"""
Unpublish the live object.
:param set_expired: Mark the object as expired.
:param commit: Commit the changes to the database.
:param user: The unpublishing user.
:param log_action: Flag for the logging action, pass ``False`` to skip logging.
"""
return UnpublishAction(
self,
set_expired=set_expired,
@ -499,23 +518,12 @@ class DraftStateMixin(models.Model):
def with_content_json(self, content):
"""
Returns a new version of the object with field values updated to reflect changes
in the provided ``content`` (which usually comes from a previously-saved revision).
Similar to :meth:`RevisionMixin.with_content_json`,
but with the following fields also preserved:
Certain field values are preserved in order to prevent errors if the returned
object is saved, such as ``id``. The following field values are also preserved,
as they are considered to be meaningful to the object as a whole, rather than
to a specific revision:
* ``latest_revision``
* ``live``
* ``has_unpublished_changes``
* ``first_published_at``
If ``TranslatableMixin`` is applied, the following field values are also preserved:
* ``translation_key``
* ``locale``
"""
obj = super().with_content_json(content)