author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 05 Mar 2010 12:18:22 +0100 | |
changeset 4812 | 2eb6a9b33aa5 |
parent 4643 | 921737d2e3a8 |
child 4835 | 13b0b96d7982 |
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 |
0 | 11 |
|
3721 | 12 |
from logilab.common.compat import any |
13 |
||
14 |
||
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
|
15 |
class SetUseEmailRelationOp(hook.Operation): |
0 | 16 |
"""delay this operation to commit to avoid conflict with a late rql query |
17 |
already setting the relation |
|
18 |
""" |
|
19 |
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
|
20 |
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
|
21 |
|
0 | 22 |
def condition(self): |
23 |
"""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
|
24 |
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
|
25 |
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
|
26 |
|
0 | 27 |
def precommit_event(self): |
28 |
if self.condition(): |
|
3421
7fec79c1c11f
[hooks] fix dummy AttributeError in SetUseEmailRelationOp
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3376
diff
changeset
|
29 |
self.session.unsafe_execute( |
0 | 30 |
'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
|
31 |
{'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
|
32 |
|
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
|
33 |
|
0 | 34 |
class SetPrimaryEmailRelationOp(SetUseEmailRelationOp): |
35 |
rtype = 'primary_email' |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
36 |
|
0 | 37 |
def condition(self): |
38 |
"""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
|
39 |
return not self.entity.primary_email |
0 | 40 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
41 |
|
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
|
42 |
class SetPrimaryEmailHook(hook.Hook): |
0 | 43 |
"""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
|
44 |
__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
|
45 |
__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
|
46 |
category = 'email' |
0 | 47 |
events = ('after_add_relation',) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
48 |
|
2843
3f5194ef620d
should be __call__
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2841
diff
changeset
|
49 |
def __call__(self): |
2847
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2843
diff
changeset
|
50 |
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
|
51 |
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
|
52 |
SetPrimaryEmailRelationOp(self._cw, entity=entity, |
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2843
diff
changeset
|
53 |
email=self._cw.entity_from_eid(self.eidto)) |
0 | 54 |
|
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
|
55 |
class SetUseEmailHook(hook.Hook): |
0 | 56 |
"""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
|
57 |
__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
|
58 |
__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
|
59 |
category = 'email' |
0 | 60 |
events = ('after_add_relation',) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1138
diff
changeset
|
61 |
|
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
|
62 |
def __call__(self): |
2847
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2843
diff
changeset
|
63 |
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
|
64 |
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
|
65 |
SetUseEmailRelationOp(self._cw, entity=entity, |
c2ee28f4d4b1
use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2843
diff
changeset
|
66 |
email=self._cw.entity_from_eid(self.eidto)) |