sobjects/email.py
changeset 0 b97547f5f1fa
child 1138 22f634977c95
equal deleted inserted replaced
-1:000000000000 0:b97547f5f1fa
       
     1 """hooks to ensure use_email / primary_email relations consistency
       
     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 SetUseEmailRelationOp(PreCommitOperation):
       
    13     """delay this operation to commit to avoid conflict with a late rql query
       
    14     already setting the relation
       
    15     """
       
    16     rtype = 'use_email'
       
    17 
       
    18     def condition(self):
       
    19         """check entity has use_email set for the email address"""
       
    20         return not self.session.unsafe_execute(
       
    21             'Any X WHERE X eid %(x)s, X use_email Y, Y eid %(y)s',
       
    22             {'x': self.fromeid, 'y': self.toeid}, 'x')
       
    23     
       
    24     def precommit_event(self):
       
    25         session = self.session
       
    26         if self.condition():
       
    27             session.unsafe_execute(
       
    28                 'SET X %s Y WHERE X eid %%(x)s, Y eid %%(y)s' % self.rtype,
       
    29                 {'x': self.fromeid, 'y': self.toeid}, 'x')
       
    30     
       
    31 class SetPrimaryEmailRelationOp(SetUseEmailRelationOp):
       
    32     rtype = 'primary_email'
       
    33     
       
    34     def condition(self):
       
    35         """check entity has no primary_email set"""
       
    36         return not self.session.unsafe_execute(
       
    37             'Any X WHERE X eid %(x)s, X primary_email Y',
       
    38             {'x': self.fromeid}, 'x')
       
    39 
       
    40     
       
    41 class SetPrimaryEmailHook(Hook):
       
    42     """notify when a bug or story or version has its state modified"""
       
    43     events = ('after_add_relation',)
       
    44     accepts = ('use_email',)
       
    45     
       
    46     def call(self, session, fromeid, rtype, toeid):
       
    47         subjtype = session.describe(fromeid)[0]
       
    48         eschema = self.vreg.schema[subjtype]
       
    49         if 'primary_email' in eschema.subject_relations():
       
    50             SetPrimaryEmailRelationOp(session, vreg=self.vreg, 
       
    51                                       fromeid=fromeid, toeid=toeid)
       
    52 
       
    53 class SetUseEmailHook(Hook):
       
    54     """notify when a bug or story or version has its state modified"""
       
    55     events = ('after_add_relation',)
       
    56     accepts = ('primary_email',)
       
    57     
       
    58     def call(self, session, fromeid, rtype, toeid):
       
    59         subjtype = session.describe(fromeid)[0]
       
    60         eschema = self.vreg.schema[subjtype]
       
    61         if 'use_email' in eschema.subject_relations():
       
    62             SetUseEmailRelationOp(session, vreg=self.vreg, 
       
    63                                   fromeid=fromeid, toeid=toeid)