from collective.workspace.membership import ITeamMembership
from collective.workspace.membership import TeamMembership
from logging import getLogger
from plone import api
from zope.globalrequest import getRequest
from zope.interface import implementer

logger = getLogger(__name__)


@implementer(ITeamMembership)
class PloneIntranetTeamMembership(TeamMembership):
    """
    Overwrite collective.workspace.membership, so that we can add our own
    handlers.
    """

    def handle_removed(self):
        # If the now removed user has created any content in the workspace,
        # he will have Ownership and the local role 'Owner' on it.
        # This needs to be purged, to avoid that the user still has direct
        # access to those contents.
        view = api.content.get_view("teammembership-purge-owner", self, getRequest())
        try:
            view.maybe_purge_owner()
        except BaseException:
            logger.exception("Cannot reset owner")
