sobjects/email.py
branchstable
changeset 4556 43c14e0e8972
parent 4555 8968c50818db
parent 4553 23201259ffeb
child 4560 23e0632df615
equal deleted inserted replaced
4555:8968c50818db 4556:43c14e0e8972
     1 """hooks to ensure use_email / primary_email relations consistency
       
     2 
       
     3 :organization: Logilab
       
     4 :copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
       
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     6 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
       
     7 """
       
     8 __docformat__ = "restructuredtext en"
       
     9 
       
    10 from cubicweb.server.hooksmanager import Hook
       
    11 from cubicweb.server.pool import PreCommitOperation
       
    12 from cubicweb.server.repository import ensure_card_respected
       
    13 
       
    14 class SetUseEmailRelationOp(PreCommitOperation):
       
    15     """delay this operation to commit to avoid conflict with a late rql query
       
    16     already setting the relation
       
    17     """
       
    18     rtype = 'use_email'
       
    19     fromeid = toeid = None # make pylint happy
       
    20 
       
    21     def condition(self):
       
    22         """check entity has use_email set for the email address"""
       
    23         return not self.session.unsafe_execute(
       
    24             'Any X WHERE X eid %(x)s, X use_email Y, Y eid %(y)s',
       
    25             {'x': self.fromeid, 'y': self.toeid}, 'x')
       
    26 
       
    27     def precommit_event(self):
       
    28         session = self.session
       
    29         if self.condition():
       
    30             # we've to handle cardinaly by ourselves since we're using unsafe_execute
       
    31             # but use session.execute and not session.unsafe_execute to check we
       
    32             # can change the relation
       
    33             ensure_card_respected(session.execute, session,
       
    34                                   self.fromeid, self.rtype, self.toeid)
       
    35             session.unsafe_execute(
       
    36                 'SET X %s Y WHERE X eid %%(x)s, Y eid %%(y)s' % self.rtype,
       
    37                 {'x': self.fromeid, 'y': self.toeid}, 'x')
       
    38 
       
    39 class SetPrimaryEmailRelationOp(SetUseEmailRelationOp):
       
    40     rtype = 'primary_email'
       
    41 
       
    42     def condition(self):
       
    43         """check entity has no primary_email set"""
       
    44         return not self.session.unsafe_execute(
       
    45             'Any X WHERE X eid %(x)s, X primary_email Y',
       
    46             {'x': self.fromeid}, 'x')
       
    47 
       
    48 
       
    49 class SetPrimaryEmailHook(Hook):
       
    50     """notify when a bug or story or version has its state modified"""
       
    51     events = ('after_add_relation',)
       
    52     accepts = ('use_email',)
       
    53 
       
    54     def call(self, session, fromeid, rtype, toeid):
       
    55         subjtype = session.describe(fromeid)[0]
       
    56         eschema = self.vreg.schema[subjtype]
       
    57         if 'primary_email' in eschema.subject_relations():
       
    58             SetPrimaryEmailRelationOp(session, vreg=self.vreg,
       
    59                                       fromeid=fromeid, toeid=toeid)
       
    60 
       
    61 class SetUseEmailHook(Hook):
       
    62     """notify when a bug or story or version has its state modified"""
       
    63     events = ('after_add_relation',)
       
    64     accepts = ('primary_email',)
       
    65 
       
    66     def call(self, session, fromeid, rtype, toeid):
       
    67         subjtype = session.describe(fromeid)[0]
       
    68         eschema = self.vreg.schema[subjtype]
       
    69         if 'use_email' in eschema.subject_relations():
       
    70             SetUseEmailRelationOp(session, vreg=self.vreg,
       
    71                                   fromeid=fromeid, toeid=toeid)