author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 15 Feb 2010 15:05:15 +0100 | |
branch | stable |
changeset 4569 | 1acd90d0cb59 |
parent 4252 | 6c4f109c2b03 |
child 4643 | 921737d2e3a8 |
permissions | -rw-r--r-- |
0 | 1 |
"""hooks to ensure use_email / primary_email relations consistency |
2 |
||
3 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3248
diff
changeset
|
4 |
:copyright: 2001-2010 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 |
||
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
10 |
from cubicweb.server import hook |
3248
db09803df8b2
fix cardinality integrity and security when setting use_email/primary_email
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
11 |
from cubicweb.server.repository import ensure_card_respected |
0 | 12 |
|
3721 | 13 |
from logilab.common.compat import any |
14 |
||
15 |
||
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
16 |
class SetUseEmailRelationOp(hook.Operation): |
0 | 17 |
"""delay this operation to commit to avoid conflict with a late rql query |
18 |
already setting the relation |
|
19 |
""" |
|
20 |
rtype = 'use_email' |
|
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
21 |
entity = email = None # make pylint happy |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
22 |
|
0 | 23 |
def condition(self): |
24 |
"""check entity has use_email set for the email address""" |
|
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
25 |
return not any(e for e in self.entity.use_email |
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
26 |
if self.email.eid == e.eid) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
27 |
|
0 | 28 |
def precommit_event(self): |
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 |
3421
7fec79c1c11f
[hooks] fix dummy AttributeError in SetUseEmailRelationOp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3376
diff
changeset
|
33 |
ensure_card_respected(self.session.execute, self.session, |
7fec79c1c11f
[hooks] fix dummy AttributeError in SetUseEmailRelationOp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3376
diff
changeset
|
34 |
self.entity.eid, self.rtype, self.email.eid) |
7fec79c1c11f
[hooks] fix dummy AttributeError in SetUseEmailRelationOp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3376
diff
changeset
|
35 |
self.session.unsafe_execute( |
0 | 36 |
'SET X %s Y WHERE X eid %%(x)s, Y eid %%(y)s' % self.rtype, |
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
37 |
{'x': self.entity.eid, 'y': self.email.eid}, 'x') |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
38 |
|
3431
6944a92c16f2
move hooks test to the hooks package, update them to work with a minimal schema
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3421
diff
changeset
|
39 |
|
0 | 40 |
class SetPrimaryEmailRelationOp(SetUseEmailRelationOp): |
41 |
rtype = 'primary_email' |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
42 |
|
0 | 43 |
def condition(self): |
44 |
"""check entity has no primary_email set""" |
|
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
45 |
return not self.entity.primary_email |
0 | 46 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
47 |
|
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
48 |
class SetPrimaryEmailHook(hook.Hook): |
0 | 49 |
"""notify when a bug or story or version has its state modified""" |
3376
f5c69485381f
[appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
50 |
__regid__ = 'setprimaryemail' |
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
51 |
__select__ = hook.Hook.__select__ & hook.match_rtype('use_email') |
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
52 |
category = 'email' |
0 | 53 |
events = ('after_add_relation',) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
54 |
|
2843
3f5194ef620d
should be __call__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2841
diff
changeset
|
55 |
def __call__(self): |
2847
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2843
diff
changeset
|
56 |
entity = self._cw.entity_from_eid(self.eidfrom) |
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
57 |
if 'primary_email' in entity.e_schema.subject_relations(): |
2847
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2843
diff
changeset
|
58 |
SetPrimaryEmailRelationOp(self._cw, entity=entity, |
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2843
diff
changeset
|
59 |
email=self._cw.entity_from_eid(self.eidto)) |
0 | 60 |
|
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
61 |
class SetUseEmailHook(hook.Hook): |
0 | 62 |
"""notify when a bug or story or version has its state modified""" |
3376
f5c69485381f
[appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3293
diff
changeset
|
63 |
__regid__ = 'setprimaryemail' |
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
64 |
__select__ = hook.Hook.__select__ & hook.match_rtype('primary_email') |
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
65 |
category = 'email' |
0 | 66 |
events = ('after_add_relation',) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
67 |
|
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
68 |
def __call__(self): |
2847
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2843
diff
changeset
|
69 |
entity = self._cw.entity_from_eid(self.eidfrom) |
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
70 |
if 'use_email' in entity.e_schema.subject_relations(): |
2847
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2843
diff
changeset
|
71 |
SetUseEmailRelationOp(self._cw, entity=entity, |
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2843
diff
changeset
|
72 |
email=self._cw.entity_from_eid(self.eidto)) |