|
1 """hooks to ensure use_email / primary_email relations consistency |
|
2 |
|
3 :organization: Logilab |
|
4 :copyright: 2001-2009 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 import hook |
|
11 |
|
12 class SetUseEmailRelationOp(hook.Operation): |
|
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 entity = email = None # make pylint happy |
|
18 |
|
19 def condition(self): |
|
20 """check entity has use_email set for the email address""" |
|
21 return not any(e for e in self.entity.use_email |
|
22 if self.email.eid == e.eid) |
|
23 |
|
24 def precommit_event(self): |
|
25 if self.condition(): |
|
26 self.session.unsafe_execute( |
|
27 'SET X %s Y WHERE X eid %%(x)s, Y eid %%(y)s' % self.rtype, |
|
28 {'x': self.entity.eid, 'y': self.email.eid}, 'x') |
|
29 |
|
30 class SetPrimaryEmailRelationOp(SetUseEmailRelationOp): |
|
31 rtype = 'primary_email' |
|
32 |
|
33 def condition(self): |
|
34 """check entity has no primary_email set""" |
|
35 return not self.entity.primary_email |
|
36 |
|
37 |
|
38 class SetPrimaryEmailHook(hook.Hook): |
|
39 """notify when a bug or story or version has its state modified""" |
|
40 __id__ = 'setprimaryemail' |
|
41 __select__ = hook.Hook.__select__ & hook.match_rtype('use_email') |
|
42 category = 'email' |
|
43 events = ('after_add_relation',) |
|
44 |
|
45 def call(self, session, eidfrom, rtype, eidto): |
|
46 entity = self.cw_req.entity_from_eid(self.eidfrom) |
|
47 if 'primary_email' in entity.e_schema.subject_relations(): |
|
48 SetPrimaryEmailRelationOp(self.cw_req, entity=entity, |
|
49 email=self.cw_req.entity_from_eid(self.eidto)) |
|
50 |
|
51 class SetUseEmailHook(hook.Hook): |
|
52 """notify when a bug or story or version has its state modified""" |
|
53 __id__ = 'setprimaryemail' |
|
54 __select__ = hook.Hook.__select__ & hook.match_rtype('primary_email') |
|
55 category = 'email' |
|
56 events = ('after_add_relation',) |
|
57 |
|
58 def __call__(self): |
|
59 entity = self.cw_req.entity_from_eid(self.eidfrom) |
|
60 if 'use_email' in entity.e_schema.subject_relations(): |
|
61 SetUseEmailRelationOp(self.cw_req, entity=entity, |
|
62 email=self.cw_req.entity_from_eid(self.eidto)) |