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