hooks/bookmark.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 05 Jul 2010 18:25:19 +0200
changeset 5891 99024ad59223
parent 5557 1a534c596bff
child 8544 3d049071957e
permissions -rw-r--r--
[schema migration] import refactoring to fix #1109558 and enhances things on the way the main pb demonstrated by #1109558 was due to the fact that in-memory schema was updated in commit_event of operations. This is undesired in most cases, since we want the modification to be taken into account in the interval between the modification detection and the commit_event. I've fixed this by merging in-memory schema / database alteration operations for most important changes, doing in-memory schema changes as they are detected and implementing a revertcommit_event method to revert them if necessary (with exception for removal of stuff from the schema, where this is simply done in a postcommit_event methods). Also, I've benefited from this to support reverting of database alteration for some operations (more to be done there), and to move so system source alteration code to the native source code for a nicer design. There may be some more stuff in syncschema.py that would benefit from similar changes, but most important things are done (at least to close #1109558, w/ unittest_syncschema and unittest_migration green).

# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This file is part of CubicWeb.
#
# CubicWeb is free software: you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
"""bookmark related hooks"""

__docformat__ = "restructuredtext en"

from cubicweb.server import hook


class AutoDeleteBookmarkOp(hook.Operation):
    bookmark = None # make pylint happy
    def precommit_event(self):
        if not self.session.deleted_in_transaction(self.bookmark.eid):
            if not self.bookmark.bookmarked_by:
                self.bookmark.cw_delete()


class DelBookmarkedByHook(hook.Hook):
    """ensure user logins are stripped"""
    __regid__ = 'autodelbookmark'
    __select__ = hook.Hook.__select__ & hook.match_rtype('bookmarked_by',)
    category = 'bookmark'
    events = ('after_delete_relation',)

    def __call__(self):
        AutoDeleteBookmarkOp(self._cw,
                             bookmark=self._cw.entity_from_eid(self.eidfrom))