sobjects/email.py
branchtls-sprint
changeset 1802 d628defebc17
parent 1138 22f634977c95
child 1977 606923dff11b
equal deleted inserted replaced
1801:672acc730ce5 1802:d628defebc17
     1 """hooks to ensure use_email / primary_email relations consistency
     1 """hooks to ensure use_email / primary_email relations consistency
     2 
     2 
     3 :organization: Logilab
     3 :organization: Logilab
     4 :copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     4 :copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     6 """
     6 """
     7 __docformat__ = "restructuredtext en"
     7 __docformat__ = "restructuredtext en"
     8 
     8 
     9 from cubicweb.server.hooksmanager import Hook
     9 from cubicweb.server.hooksmanager import Hook
    13     """delay this operation to commit to avoid conflict with a late rql query
    13     """delay this operation to commit to avoid conflict with a late rql query
    14     already setting the relation
    14     already setting the relation
    15     """
    15     """
    16     rtype = 'use_email'
    16     rtype = 'use_email'
    17     fromeid = toeid = None # make pylint happy
    17     fromeid = toeid = None # make pylint happy
    18     
    18 
    19     def condition(self):
    19     def condition(self):
    20         """check entity has use_email set for the email address"""
    20         """check entity has use_email set for the email address"""
    21         return not self.session.unsafe_execute(
    21         return not self.session.unsafe_execute(
    22             'Any X WHERE X eid %(x)s, X use_email Y, Y eid %(y)s',
    22             'Any X WHERE X eid %(x)s, X use_email Y, Y eid %(y)s',
    23             {'x': self.fromeid, 'y': self.toeid}, 'x')
    23             {'x': self.fromeid, 'y': self.toeid}, 'x')
    24     
    24 
    25     def precommit_event(self):
    25     def precommit_event(self):
    26         session = self.session
    26         session = self.session
    27         if self.condition():
    27         if self.condition():
    28             session.unsafe_execute(
    28             session.unsafe_execute(
    29                 'SET X %s Y WHERE X eid %%(x)s, Y eid %%(y)s' % self.rtype,
    29                 'SET X %s Y WHERE X eid %%(x)s, Y eid %%(y)s' % self.rtype,
    30                 {'x': self.fromeid, 'y': self.toeid}, 'x')
    30                 {'x': self.fromeid, 'y': self.toeid}, 'x')
    31     
    31 
    32 class SetPrimaryEmailRelationOp(SetUseEmailRelationOp):
    32 class SetPrimaryEmailRelationOp(SetUseEmailRelationOp):
    33     rtype = 'primary_email'
    33     rtype = 'primary_email'
    34     
    34 
    35     def condition(self):
    35     def condition(self):
    36         """check entity has no primary_email set"""
    36         """check entity has no primary_email set"""
    37         return not self.session.unsafe_execute(
    37         return not self.session.unsafe_execute(
    38             'Any X WHERE X eid %(x)s, X primary_email Y',
    38             'Any X WHERE X eid %(x)s, X primary_email Y',
    39             {'x': self.fromeid}, 'x')
    39             {'x': self.fromeid}, 'x')
    40 
    40 
    41     
    41 
    42 class SetPrimaryEmailHook(Hook):
    42 class SetPrimaryEmailHook(Hook):
    43     """notify when a bug or story or version has its state modified"""
    43     """notify when a bug or story or version has its state modified"""
    44     events = ('after_add_relation',)
    44     events = ('after_add_relation',)
    45     accepts = ('use_email',)
    45     accepts = ('use_email',)
    46     
    46 
    47     def call(self, session, fromeid, rtype, toeid):
    47     def call(self, session, fromeid, rtype, toeid):
    48         subjtype = session.describe(fromeid)[0]
    48         subjtype = session.describe(fromeid)[0]
    49         eschema = self.vreg.schema[subjtype]
    49         eschema = self.vreg.schema[subjtype]
    50         if 'primary_email' in eschema.subject_relations():
    50         if 'primary_email' in eschema.subject_relations():
    51             SetPrimaryEmailRelationOp(session, vreg=self.vreg, 
    51             SetPrimaryEmailRelationOp(session, vreg=self.vreg,
    52                                       fromeid=fromeid, toeid=toeid)
    52                                       fromeid=fromeid, toeid=toeid)
    53 
    53 
    54 class SetUseEmailHook(Hook):
    54 class SetUseEmailHook(Hook):
    55     """notify when a bug or story or version has its state modified"""
    55     """notify when a bug or story or version has its state modified"""
    56     events = ('after_add_relation',)
    56     events = ('after_add_relation',)
    57     accepts = ('primary_email',)
    57     accepts = ('primary_email',)
    58     
    58 
    59     def call(self, session, fromeid, rtype, toeid):
    59     def call(self, session, fromeid, rtype, toeid):
    60         subjtype = session.describe(fromeid)[0]
    60         subjtype = session.describe(fromeid)[0]
    61         eschema = self.vreg.schema[subjtype]
    61         eschema = self.vreg.schema[subjtype]
    62         if 'use_email' in eschema.subject_relations():
    62         if 'use_email' in eschema.subject_relations():
    63             SetUseEmailRelationOp(session, vreg=self.vreg, 
    63             SetUseEmailRelationOp(session, vreg=self.vreg,
    64                                   fromeid=fromeid, toeid=toeid)
    64                                   fromeid=fromeid, toeid=toeid)