hooks/email.py
author Pierre-Yves David <pierre-yves.david@logilab.fr>
Tue, 02 Feb 2010 18:22:25 +0100
changeset 4428 03614b377ecd
parent 4252 6c4f109c2b03
child 4643 921737d2e3a8
permissions -rw-r--r--
Add a ``schema`` command to cmd ctrl to generate schema image. This changeset add the new commande and do some refactoring in cwconfig and schema.py to allow the use of CubicWebNoAppConfiguration with CubicWebSchemaLoader.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     1
"""hooks to ensure use_email / primary_email relations consistency
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     2
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     7
"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     8
__docformat__ = "restructuredtext en"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    12
3721
a02f7df5bbec 2.4 compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3431
diff changeset
    13
from logilab.common.compat import any
a02f7df5bbec 2.4 compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3431
diff changeset
    14
a02f7df5bbec 2.4 compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3431
diff changeset
    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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    17
    """delay this operation to commit to avoid conflict with a late rql query
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
    already setting the relation
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
    def condition(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
    def precommit_event(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
class SetPrimaryEmailRelationOp(SetUseEmailRelationOp):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
    def condition(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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))