sobjects/hooks.py
changeset 0 b97547f5f1fa
child 1098 739d4dce9b19
equal deleted inserted replaced
-1:000000000000 0:b97547f5f1fa
       
     1 """various library content hooks
       
     2 
       
     3 :organization: Logilab
       
     4 :copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     6 """
       
     7 __docformat__ = "restructuredtext en"
       
     8 
       
     9 from cubicweb.server.hooksmanager import Hook
       
    10 from cubicweb.server.pool import PreCommitOperation
       
    11 
       
    12 class AddUpdateEUserHook(Hook):
       
    13     """ensure user logins are stripped"""
       
    14     events = ('before_add_entity', 'before_update_entity',)
       
    15     accepts = ('EUser',)
       
    16     
       
    17     def call(self, session, entity):
       
    18         if 'login' in entity and entity['login']:
       
    19             entity['login'] = entity['login'].strip()
       
    20 
       
    21 
       
    22 class AutoDeleteBookmark(PreCommitOperation):
       
    23     def precommit_event(self):
       
    24         session = self.session
       
    25         if not self.beid in session.query_data('pendingeids', ()):
       
    26             if not session.unsafe_execute('Any X WHERE X bookmarked_by U, X eid %(x)s',
       
    27                                           {'x': self.beid}, 'x'):
       
    28                 session.unsafe_execute('DELETE Bookmark X WHERE X eid %(x)s',
       
    29                                        {'x': self.beid}, 'x')
       
    30         
       
    31 class DelBookmarkedByHook(Hook):
       
    32     """ensure user logins are stripped"""
       
    33     events = ('after_delete_relation',)
       
    34     accepts = ('bookmarked_by',)
       
    35     
       
    36     def call(self, session, subj, rtype, obj):
       
    37         AutoDeleteBookmark(session, beid=subj)