Skip to main content

A Plone addon introducing a trash with restore functionality.

Project description

ftw.trash is a Plone addon which modifies Plone’s behavior when content is deleted. When a user deletes content, it will not actually be deleted but rather marked as trashed. Trashed content can then be restored when needed.

Installation and usage

  • Be aware that ftw.trash requires and installs collective.deletepermission.

  • Add ftw.trash to the eggs in your buildout configuration:

    [instance]
    eggs +=
        ...
        ftw.trash
  • Install the ftw.trash addon in Plone (Addons control panel or portal_setup or quickinstaller).

  • Deleting content looks exactly as in standard Plone, but it does not actually delete the content but only hides it.

  • Administrators can restore content with a global trash view, accessible via the user menu.

  • Personal trashes are not implemented at the moment.

https://github.com/4teamwork/ftw.trash/raw/master/docs/trash.png

Querying

The catalog is patched, so that it includes the default query {'trashed': False}. This makes sure that we only work with ‘untrashed’ objects by default.

If you want to access trashed objects, you can simply use the query keyword trashed with one of these values:

  • False: only return objects which are not trashed.

  • True: only return objects which are trashed.

  • None: do not apply “trashed” filter, return both trashed and ‘untrashed’ objects.

These filters only apply when portal_catalog.searchResults is used. When using portal_catalog.unrestrictedSearchResults the behavior is different, especially for trashed=None, since this method is not patched.

Methods for trashing and deleting

ftw.trash patches manage_delObjects so that it trashes the content instead of deleting it. ftw.trash also adds new methods:

  • parent.manage_trashObjects([id1, id2, ..]): trashes the contents.

  • parent.manage_immediatelyDeleteObjects(([id1, id2, ..]): immediately deletes the contents without trashing it.

  • parent.manage_delObjects([id1, id2, ..]): trashes the contents. If called from ZMI (or within link integrity checker), the content is immediately deleted.

The patches are applied on the site root, on DX- and on AT-folders when ftw.trash is installed in the path. For the methods to work properly, the Generic Setup profile must be installed as well.

Temporary disable trash feature

You can either set the env variable DISABLE_FTW_TRASH manually, or use the provided context manager.

from ftw.trash.utils import temporary_disable_trash

with temporary_disable_trash():
    self.portal.manage_delObjects([container1.getId()])

Setting the required permission for restoring content

Restoring a page can be compared to adding a new page to its container. Therefore by default we require the Add portal content permission on the parent in order to restore content.

However this can depend on the application and the content type - there are some content types which can be seen as part of the content of their parents, in which case we’d like to require the Modify portal content permission for the parent instead. This can be modelled by simply registering an IIsRestoreAllowedAdapter for the content type being restored. For example:

@implementer(IIsRestoreAllowedAdapter)
@adapter(IMyType, IMyBrowserLayer)
def is_restore_allowed_for_my_type(context, request):
    parent = aq_parent(aq_inner(context))
    return getSecurityManager().checkPermission('Modify portal content', parent)

Events

The following object events are fired:

  • ftw.trash.interfaces.IBeforeObjectTrashedEvent: the object will be trashed.

  • ftw.trash.interfaces.IObjectTrashedEvent: the object has been trashed.

  • ftw.trash.interfaces.IBeforeObjectRestoredEvent: the object will be restored.

  • ftw.trash.interfaces.IObjectRestoredEvent: the object has been restored.

Internals

  • When content is deleted, it is marked as ITrashed and IRestorable, children are only marked as ITrashed.

  • Only the root node of the deleted structure can be restored and thus provides IRestorable. Restoring children without their deleted parents is not possible since their parent(s) would be missing.

  • Trashed content is not moved.

  • The catalog’s searchResults method is patched so that it filters trashed objects by default.

  • The contentItems method is patched to exclude trashed content. It is used for listFolderContents and getFolderContents.

  • Trashed content is prevented from being published / accessed through the browser unless the user has the Manager role.

  • For restoring content, the permissions Restore trashed content and Add portal content are required. The Restore trashed content is granted by default to the roles Manager and Site Administrator on the site root.

Development

  1. Fork this repo

  2. Clone your fork

  3. Shell: ln -s development.cfg buildout.cfg

  4. Shell: python bootstrap.py

  5. Shell: bin/buildout

Run bin/test to test your changes.

Or start an instance by running bin/instance fg.

Changelog

1.7.4 (2021-06-01)

  • Only sheck IItem based objects for trashed interfaces. [mathias.leimgruber]

1.7.3 (2020-01-07)

  • Stop trash items with long paths from hiding action buttons on the trash view. [djowett-ftw]

1.7.2 (2019-12-09)

  • Rename trash user action from “user_management” to “trash”. If you use ftw.usermanagement you should reinstall it. [jone]

1.7.1 (2019-12-05)

  • Fix bug when migrating Plone site. [jone]

1.7.0 (2019-12-05)

  • Disable trash when migrating Plone site. [jone]

1.6.0 (2019-12-04)

  • Introduce temporary_disable_trash context manager. [mathias.leimgruber]

1.5.1 (2019-10-18)

  • Do not check security recursively when trashing in order to have the same behavior as standard Plone. [jone]

1.5.0 (2019-10-02)

  • Let contentItems, listFolderContents and getFolderContents no longer return trashed content. [jone]

1.4.1 (2019-07-25)

  • Make it possible to delete single items in the trash view. [jone]

1.3.1 (2019-04-23)

  • Clean trash, even if there is not enough permission to delete the actual item. [mathias.leimgruber]

1.3.0 (2018-12-17)

  • Add Plone 5.1 support. [jone]

1.2.1 (2018-11-13)

  • Patch Products.PloneFormGen to make it aware of trashed objects [Nachtalb]

1.2.0 (2018-08-16)

  • When restoring a file, redirect to /view so that the file is not downloaded. [jone]

  • Provide a manage_immediatelyDeleteObjects method. [jone]

  • Fix cleaning trash when there is a parent and a child in the trash at once. [jone]

  • Fix and translate error message when trying to restore child of trashed parent. [jone]

1.1.1 (2018-08-03)

  • Show changelog on pypi. [jone]

1.1.0 (2018-08-03)

  • Do not restore earlier trashed children when restoring parent. [jone]

  • Allow deleting content from ZMI. [jone]

  • Fire events when trashing and restoring object. [jone]

  • Do not intercept link integrity checker. [jone]

  • Add “clean trash” functionality to trash view. [jone]

1.0.0 (2018-07-05)

  • Initial implementation [jone]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ftw.trash-1.7.4.tar.gz (219.1 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page