author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 16 Oct 2009 17:12:10 +0200 | |
branch | stable |
changeset 3710 | 5bfdb591050a |
parent 3248 | db09803df8b2 |
child 4212 | ab6573088b4a |
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 |
|
3248
db09803df8b2
fix cardinality integrity and security when setting use_email/primary_email
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
12 |
from cubicweb.server.repository import ensure_card_respected |
0 | 13 |
|
14 |
class SetUseEmailRelationOp(PreCommitOperation): |
|
15 |
"""delay this operation to commit to avoid conflict with a late rql query |
|
16 |
already setting the relation |
|
17 |
""" |
|
18 |
rtype = 'use_email' |
|
1138
22f634977c95
make pylint happy, fix some bugs on the way
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
19 |
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
|
20 |
|
0 | 21 |
def condition(self): |
22 |
"""check entity has use_email set for the email address""" |
|
23 |
return not self.session.unsafe_execute( |
|
24 |
'Any X WHERE X eid %(x)s, X use_email Y, Y eid %(y)s', |
|
25 |
{'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
|
26 |
|
0 | 27 |
def precommit_event(self): |
28 |
session = self.session |
|
29 |
if self.condition(): |
|
3248
db09803df8b2
fix cardinality integrity and security when setting use_email/primary_email
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
30 |
# we've to handle cardinaly by ourselves since we're using unsafe_execute |
db09803df8b2
fix cardinality integrity and security when setting use_email/primary_email
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
31 |
# but use session.execute and not session.unsafe_execute to check we |
db09803df8b2
fix cardinality integrity and security when setting use_email/primary_email
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
32 |
# can change the relation |
db09803df8b2
fix cardinality integrity and security when setting use_email/primary_email
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
33 |
ensure_card_respected(session.execute, session, |
db09803df8b2
fix cardinality integrity and security when setting use_email/primary_email
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
34 |
self.fromeid, self.rtype, self.toeid) |
0 | 35 |
session.unsafe_execute( |
36 |
'SET X %s Y WHERE X eid %%(x)s, Y eid %%(y)s' % self.rtype, |
|
37 |
{'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
|
38 |
|
0 | 39 |
class SetPrimaryEmailRelationOp(SetUseEmailRelationOp): |
40 |
rtype = 'primary_email' |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
41 |
|
0 | 42 |
def condition(self): |
43 |
"""check entity has no primary_email set""" |
|
44 |
return not self.session.unsafe_execute( |
|
45 |
'Any X WHERE X eid %(x)s, X primary_email Y', |
|
46 |
{'x': self.fromeid}, 'x') |
|
47 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
48 |
|
0 | 49 |
class SetPrimaryEmailHook(Hook): |
50 |
"""notify when a bug or story or version has its state modified""" |
|
51 |
events = ('after_add_relation',) |
|
52 |
accepts = ('use_email',) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
53 |
|
0 | 54 |
def call(self, session, fromeid, rtype, toeid): |
55 |
subjtype = session.describe(fromeid)[0] |
|
56 |
eschema = self.vreg.schema[subjtype] |
|
57 |
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
|
58 |
SetPrimaryEmailRelationOp(session, vreg=self.vreg, |
0 | 59 |
fromeid=fromeid, toeid=toeid) |
60 |
||
61 |
class SetUseEmailHook(Hook): |
|
62 |
"""notify when a bug or story or version has its state modified""" |
|
63 |
events = ('after_add_relation',) |
|
64 |
accepts = ('primary_email',) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
65 |
|
0 | 66 |
def call(self, session, fromeid, rtype, toeid): |
67 |
subjtype = session.describe(fromeid)[0] |
|
68 |
eschema = self.vreg.schema[subjtype] |
|
69 |
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
|
70 |
SetUseEmailRelationOp(session, vreg=self.vreg, |
0 | 71 |
fromeid=fromeid, toeid=toeid) |