1 # copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
3 # |
|
4 # This file is part of CubicWeb. |
|
5 # |
|
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
7 # terms of the GNU Lesser General Public License as published by the Free |
|
8 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
9 # any later version. |
|
10 # |
|
11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
|
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
14 # details. |
|
15 # |
|
16 # You should have received a copy of the GNU Lesser General Public License along |
|
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
|
18 """hooks to ensure use_email / primary_email relations consistency""" |
|
19 |
|
20 __docformat__ = "restructuredtext en" |
|
21 |
|
22 from cubicweb.server import hook |
|
23 |
|
24 |
|
25 class SetUseEmailRelationOp(hook.Operation): |
|
26 """delay this operation to commit to avoid conflict with a late rql query |
|
27 already setting the relation |
|
28 """ |
|
29 rtype = 'use_email' |
|
30 entity = email = None # make pylint happy |
|
31 |
|
32 def condition(self): |
|
33 """check entity has use_email set for the email address""" |
|
34 return not any(e for e in self.entity.use_email |
|
35 if self.email.eid == e.eid) |
|
36 |
|
37 def precommit_event(self): |
|
38 if self.cnx.deleted_in_transaction(self.entity.eid): |
|
39 return |
|
40 if self.cnx.deleted_in_transaction(self.email.eid): |
|
41 return |
|
42 if self.condition(): |
|
43 self.cnx.execute( |
|
44 'SET X %s Y WHERE X eid %%(x)s, Y eid %%(y)s' % self.rtype, |
|
45 {'x': self.entity.eid, 'y': self.email.eid}) |
|
46 |
|
47 |
|
48 class SetPrimaryEmailRelationOp(SetUseEmailRelationOp): |
|
49 rtype = 'primary_email' |
|
50 |
|
51 def condition(self): |
|
52 """check entity has no primary_email set""" |
|
53 return not self.entity.primary_email |
|
54 |
|
55 |
|
56 class SetPrimaryEmailHook(hook.Hook): |
|
57 """notify when a bug or story or version has its state modified""" |
|
58 __regid__ = 'setprimaryemail' |
|
59 __select__ = hook.Hook.__select__ & hook.match_rtype('use_email') |
|
60 category = 'email' |
|
61 events = ('after_add_relation',) |
|
62 |
|
63 def __call__(self): |
|
64 entity = self._cw.entity_from_eid(self.eidfrom) |
|
65 if 'primary_email' in entity.e_schema.subject_relations(): |
|
66 SetPrimaryEmailRelationOp(self._cw, entity=entity, |
|
67 email=self._cw.entity_from_eid(self.eidto)) |
|
68 |
|
69 class SetUseEmailHook(hook.Hook): |
|
70 """notify when a bug or story or version has its state modified""" |
|
71 __regid__ = 'setprimaryemail' |
|
72 __select__ = hook.Hook.__select__ & hook.match_rtype('primary_email') |
|
73 category = 'email' |
|
74 events = ('after_add_relation',) |
|
75 |
|
76 def __call__(self): |
|
77 entity = self._cw.entity_from_eid(self.eidfrom) |
|
78 if 'use_email' in entity.e_schema.subject_relations(): |
|
79 SetUseEmailRelationOp(self._cw, entity=entity, |
|
80 email=self._cw.entity_from_eid(self.eidto)) |
|