author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 26 May 2009 19:36:40 +0200 | |
branch | stable |
changeset 1944 | a1b1d4f8482c |
parent 1802 | d628defebc17 |
child 1977 | 606923dff11b |
permissions | -rw-r--r-- |
0 | 1 |
"""hooks to ensure use_email / primary_email relations consistency |
2 |
||
3 |
:organization: Logilab |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 |
""" |
|
7 |
__docformat__ = "restructuredtext en" |
|
8 |
||
9 |
from cubicweb.server.hooksmanager import Hook |
|
10 |
from cubicweb.server.pool import PreCommitOperation |
|
11 |
||
12 |
class SetUseEmailRelationOp(PreCommitOperation): |
|
13 |
"""delay this operation to commit to avoid conflict with a late rql query |
|
14 |
already setting the relation |
|
15 |
""" |
|
16 |
rtype = 'use_email' |
|
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
17 |
fromeid = toeid = None # make pylint happy |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
18 |
|
0 | 19 |
def condition(self): |
20 |
"""check entity has use_email set for the email address""" |
|
21 |
return not self.session.unsafe_execute( |
|
22 |
'Any X WHERE X eid %(x)s, X use_email Y, Y eid %(y)s', |
|
23 |
{'x': self.fromeid, 'y': self.toeid}, 'x') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
24 |
|
0 | 25 |
def precommit_event(self): |
26 |
session = self.session |
|
27 |
if self.condition(): |
|
28 |
session.unsafe_execute( |
|
29 |
'SET X %s Y WHERE X eid %%(x)s, Y eid %%(y)s' % self.rtype, |
|
30 |
{'x': self.fromeid, 'y': self.toeid}, 'x') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
31 |
|
0 | 32 |
class SetPrimaryEmailRelationOp(SetUseEmailRelationOp): |
33 |
rtype = 'primary_email' |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
34 |
|
0 | 35 |
def condition(self): |
36 |
"""check entity has no primary_email set""" |
|
37 |
return not self.session.unsafe_execute( |
|
38 |
'Any X WHERE X eid %(x)s, X primary_email Y', |
|
39 |
{'x': self.fromeid}, 'x') |
|
40 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
41 |
|
0 | 42 |
class SetPrimaryEmailHook(Hook): |
43 |
"""notify when a bug or story or version has its state modified""" |
|
44 |
events = ('after_add_relation',) |
|
45 |
accepts = ('use_email',) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
46 |
|
0 | 47 |
def call(self, session, fromeid, rtype, toeid): |
48 |
subjtype = session.describe(fromeid)[0] |
|
49 |
eschema = self.vreg.schema[subjtype] |
|
50 |
if 'primary_email' in eschema.subject_relations(): |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
51 |
SetPrimaryEmailRelationOp(session, vreg=self.vreg, |
0 | 52 |
fromeid=fromeid, toeid=toeid) |
53 |
||
54 |
class SetUseEmailHook(Hook): |
|
55 |
"""notify when a bug or story or version has its state modified""" |
|
56 |
events = ('after_add_relation',) |
|
57 |
accepts = ('primary_email',) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
58 |
|
0 | 59 |
def call(self, session, fromeid, rtype, toeid): |
60 |
subjtype = session.describe(fromeid)[0] |
|
61 |
eschema = self.vreg.schema[subjtype] |
|
62 |
if 'use_email' in eschema.subject_relations(): |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
63 |
SetUseEmailRelationOp(session, vreg=self.vreg, |
0 | 64 |
fromeid=fromeid, toeid=toeid) |