Un-publishing data

In order to facilitate restoring erroneously removed items, each model in the QMS has a “publish” field. Un-publishing item marks it as “deleted” for the end users.

See also

utils.unpublish implemented in Task #27238

Note

Unpublished items are still visible to staff in django admin panel.

Un-publishing child items

Un-publishing an item will cascade to its child items. For example, un-publishing a ward means that all sessions and observations submitted to it are no longer accessible by users. Similarly, un-publishing an institution or department will unpublish all related wards.

This cascading logic applies to all relations set as on_delete=CASCADE. All other relations are unaffected. For instance, on_delete=SET_NULL will not set null until the item is physically deleted from the database.

Warning

While all children of unpublished objects are automatically detected and unpublished, generic relations (issues, comments) are more tricky, so to optimize the process, only some models will trigger un-publish on those, More details in unpublish_generic_relations.

In dashboards and settings

Any action labelled as “remove” will unpublish the item and its child items.

This action requires that user has the “delete” permission even though the item is technically being modified rather than edited as it results in user losing access to the object as if it were deleted.

The label “Remove” also indicates that the item is not being “deleted”.

../_images/unpublish-settings.png

The Remove button unpublishes the edited item and its child items. In this case a ward is being removed. This will also un-publish all rooms, but also any observations submitted to this ward.

In django admin

The “Unpublish” button is available besides “Delete” in every model that is published. It will give you a preview of related items about to be unpublished so you can review and confirm.

../_images/unpublish-admin-btn.png

The “Unpublish” button in django admin is effectively the same as “Remove” is dashboards, but it will give you a chance to review related items before unpublishing

../_images/unpublish-confirm.png

The confirmation page lists and summarizes all object that will be affected by the unpublish action

Unpublishing in bulk by selecting items and choosing “Unpublish (including relations)” will result in items and their children being un-published

../_images/unpublish-relations.png

The “Unpublish (including relations)” action will unpublish selected objects, including their child items

Exceptions

In django admin it is possible to unpublish a single object without propagating this change to child items

unpublishing item by un-ticking “publish” checkbox only affects the current item and *does not* propagate to child items

../_images/unpublish-admin-checkbox.png

The checkbox can be used to un-publish the item without affecting any child items

The base “Unpublish” admin action will only unpublish selected objects without propagating change to relations

In Python code

Use Unpublisher class to unpublish object, propagate its removal to children, and log the action.

u = Unpublisher(obj, user=user)
u.unpublish(log_action=True, log_comment="Unpublished by user")

See also

The Unpublisher class has more functionality. Visit its documentation and its main unpublish() method for more details.

Using command-line to update child objects

If objects have been unpublished, but their children have not, it may lead to inconsistent state and some unpredicted issues. Use the command below to ensure that children of all unpublished object are also unpublished:

# unpublish children of commonly used models
./manage.py propagate_unpublished
# Unpublish children of specific models
./manage.py propagate_unpublished eguide auditform

Note

This command runs non-atomically, so if it crashes or stops before completing, it will partially complete the process without rolling back changes.

Restoring removed items

Undo removal

The proper way to restore (undo unpublish) items is to use UnpublishLogEntry admin page and invoke the “Restore unpublished objects” action. The admin page shows exact date/time of removal and the user who did it along with the number of objects affected.

../_images/unpublish-restore.png

The restore action restores the object and its children

To re-publish individual items, you need to either:

  • go to model’s django admin page, select multiple objects, and run “Publish” action

  • go through each item and publish it intedepndently by checking “published”

../_images/unpublish-admin-checkbox.png

The checkbox can be used to re-publish the item

Logging unpublished items

class action_audit.models.UnpublishLogEntry(*args, **kwargs)

Logs ‘unpublish’ action, along with metadata who unpublished, when and which objects were affected

property count: int

Number of items unpublish.

Defaults to 0 if objs list is malformed

property querysets: Iterator[QuerySet]

Objects logged in this entry expressed as querysets

re_publish() int

Re-publishes (restores) objects logged as unpublished in this entry

exception DoesNotExist
exception MultipleObjectsReturned