author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 06 Jul 2009 14:52:05 +0200 | |
branch | stable |
changeset 2285 | 1cf9e44e2f1f |
parent 1977 | 606923dff11b |
child 3248 | db09803df8b2 |
permissions | -rw-r--r-- |
0 | 1 |
"""hooks to ensure use_email / primary_email relations consistency |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 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' |
|
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
18 |
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
|
19 |
|
0 | 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') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
25 |
|
0 | 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') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
32 |
|
0 | 33 |
class SetPrimaryEmailRelationOp(SetUseEmailRelationOp): |
34 |
rtype = 'primary_email' |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
35 |
|
0 | 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 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
42 |
|
0 | 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',) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
47 |
|
0 | 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(): |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
52 |
SetPrimaryEmailRelationOp(session, vreg=self.vreg, |
0 | 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',) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
59 |
|
0 | 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(): |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
64 |
SetUseEmailRelationOp(session, vreg=self.vreg, |
0 | 65 |
fromeid=fromeid, toeid=toeid) |