hooks/notification.py
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Fri, 23 Apr 2010 20:06:04 +0200
branchstable
changeset 5400 b7ab099b128a
parent 4993 c9229f185690
child 5174 78438ad513ca
child 5421 8167de96c523
permissions -rw-r--r--
[doc/book] various content fixes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     1
"""some hooks to handle notification on entity's changes
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     2
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     3
:organization: Logilab
4307
7fba9c34c88f update copyright
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4075
diff changeset
     4
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     5
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     6
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     7
"""
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     8
__docformat__ = "restructuredtext en"
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     9
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    10
from logilab.common.textutils import normalize_text
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    11
4075
e710f4052bd6 use implements instead of entity_implements
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3536
diff changeset
    12
from cubicweb.selectors import implements
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    13
from cubicweb.server import hook
3427
78dfef45ee48 [notification] add missing import, fix dummy AttributeError in RenderAndSendNotificationView
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3422
diff changeset
    14
from cubicweb.sobjects.supervising import SupervisionMailOp
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    15
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    16
class RenderAndSendNotificationView(hook.Operation):
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    17
    """delay rendering of notification view until precommit"""
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    18
    def precommit_event(self):
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    19
        view = self.view
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3376
diff changeset
    20
        if view.cw_rset is not None and not view.cw_rset:
3072
6fb42c53f6df backport 3.5 branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3023
diff changeset
    21
            return # entity added and deleted in the same transaction (cache effect)
3422
089c4b71ac16 [hooks] fix dummy NameError in RenderAndSendNotification
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3418
diff changeset
    22
        if view.cw_rset and self.session.deleted_in_transaction(view.cw_rset[view.cw_row or 0][view.cw_col or 0]):
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    23
            return # entity added and deleted in the same transaction
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    24
        self.view.render_and_send(**getattr(self, 'viewargs', {}))
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    25
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    26
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    27
class NotificationHook(hook.Hook):
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    28
    __abstract__ = True
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    29
    category = 'notification'
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    30
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    31
    def select_view(self, vid, rset, row=0, col=0):
4721
8f63691ccb7f pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
    32
        return self._cw.vreg['views'].select_or_none(vid, self._cw, rset=rset,
8f63691ccb7f pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
    33
                                                     row=row, col=col)
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    34
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    35
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    36
class StatusChangeHook(NotificationHook):
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    37
    """notify when a workflowable entity has its state modified"""
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163
diff changeset
    38
    __regid__ = 'notifystatuschange'
4075
e710f4052bd6 use implements instead of entity_implements
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3536
diff changeset
    39
    __select__ = NotificationHook.__select__ & implements('TrInfo')
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    40
    events = ('after_add_entity',)
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    41
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    42
    def __call__(self):
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    43
        entity = self.entity
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    44
        if not entity.from_state: # not a transition
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    45
            return
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    46
        rset = entity.related('wf_info_for')
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    47
        view = self.select_view('notif_status_change', rset=rset, row=0)
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    48
        if view is None:
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    49
            return
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    50
        comment = entity.printable_value('comment', format='text/plain')
3023
7864fee8b4ec backport 3.5 step 1, remaining wf changes in hooks to merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2900
diff changeset
    51
        # XXX don't try to wrap rest until we've a proper transformation (see
7864fee8b4ec backport 3.5 step 1, remaining wf changes in hooks to merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2900
diff changeset
    52
        # #103822)
7864fee8b4ec backport 3.5 step 1, remaining wf changes in hooks to merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2900
diff changeset
    53
        if comment and entity.comment_format != 'text/rest':
7864fee8b4ec backport 3.5 step 1, remaining wf changes in hooks to merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2900
diff changeset
    54
            comment = normalize_text(comment, 80)
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
    55
        RenderAndSendNotificationView(self._cw, view=view, viewargs={
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    56
            'comment': comment, 'previous_state': entity.previous_state.name,
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    57
            'current_state': entity.new_state.name})
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    58
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    59
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    60
class RelationChangeHook(NotificationHook):
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163
diff changeset
    61
    __regid__ = 'notifyrelationchange'
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    62
    events = ('before_add_relation', 'after_add_relation',
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    63
              'before_delete_relation', 'after_delete_relation')
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    64
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    65
    def __call__(self):
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    66
        """if a notification view is defined for the event, send notification
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    67
        email defined by the view
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    68
        """
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
    69
        rset = self._cw.eid_rset(self.eidfrom)
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    70
        view = self.select_view('notif_%s_%s' % (self.event,  self.rtype),
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    71
                                rset=rset, row=0)
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    72
        if view is None:
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    73
            return
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
    74
        RenderAndSendNotificationView(self._cw, view=view)
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    75
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    76
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    77
class EntityChangeHook(NotificationHook):
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    78
    """if a notification view is defined for the event, send notification
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    79
    email defined by the view
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    80
    """
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163
diff changeset
    81
    __regid__ = 'notifyentitychange'
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    82
    events = ('after_add_entity', 'after_update_entity')
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    83
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    84
    def __call__(self):
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    85
        rset = self.entity.as_rset()
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    86
        view = self.select_view('notif_%s' % self.event, rset=rset, row=0)
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    87
        if view is None:
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    88
            return
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
    89
        RenderAndSendNotificationView(self._cw, view=view)
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    90
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    91
3536
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
    92
class EntityUpdatedNotificationOp(hook.SingleLastOperation):
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
    93
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
    94
    def precommit_event(self):
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
    95
        session = self.session
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
    96
        for eid in session.transaction_data['changes']:
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
    97
            view = session.vreg['views'].select('notif_entity_updated', session,
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
    98
                                                rset=session.eid_rset(eid),
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
    99
                                                row=0)
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
   100
            RenderAndSendNotificationView(session, view=view)
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
   101
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
   102
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
   103
class EntityUpdateHook(NotificationHook):
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
   104
    __regid__ = 'notifentityupdated'
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
   105
    __abstract__ = True # do not register by default
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
   106
    __select__ = NotificationHook.__select__ & hook.from_dbapi_query()
3536
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
   107
    events = ('before_update_entity',)
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
   108
    skip_attrs = set()
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
   109
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
   110
    def __call__(self):
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
   111
        session = self._cw
4843
5f7363416765 fix hooks control method name + other litle cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
   112
        if session.added_in_transaction(self.entity.eid):
3536
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
   113
            return # entity is being created
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
   114
        # then compute changes
4843
5f7363416765 fix hooks control method name + other litle cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
   115
        attrs = [k for k in self.entity.edited_attributes
5f7363416765 fix hooks control method name + other litle cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4835
diff changeset
   116
                 if not k in self.skip_attrs]
3536
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
   117
        if not attrs:
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
   118
            return
4993
c9229f185690 [notification] don't create 'changes' in transaction data until actually needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
   119
        changes = session.transaction_data.setdefault('changes', {})
c9229f185690 [notification] don't create 'changes' in transaction data until actually needed
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4843
diff changeset
   120
        thisentitychanges = changes.setdefault(self.entity.eid, set())
3536
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
   121
        rqlsel, rqlrestr = [], ['X eid %(x)s']
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
   122
        for i, attr in enumerate(attrs):
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
   123
            var = chr(65+i)
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
   124
            rqlsel.append(var)
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
   125
            rqlrestr.append('X %s %s' % (attr, var))
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
   126
        rql = 'Any %s WHERE %s' % (','.join(rqlsel), ','.join(rqlrestr))
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
   127
        rset = session.execute(rql, {'x': self.entity.eid}, 'x')
3536
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
   128
        for i, attr in enumerate(attrs):
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
   129
            oldvalue = rset[0][i]
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
   130
            newvalue = self.entity[attr]
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
   131
            if oldvalue != newvalue:
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
   132
                thisentitychanges.add((attr, oldvalue, newvalue))
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
   133
        if thisentitychanges:
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
   134
            EntityUpdatedNotificationOp(session)
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
   135
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3427
diff changeset
   136
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   137
# supervising ##################################################################
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   138
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   139
class SomethingChangedHook(NotificationHook):
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163
diff changeset
   140
    __regid__ = 'supervising'
4835
13b0b96d7982 [repo] enhanced security handling: deprecates unsafe_execute, in favor of explicit read/write security control using the `enabled_security` context manager. Also code executed on the repository side is now unsafe by default.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
   141
    __select__ = NotificationHook.__select__ & hook.from_dbapi_query()
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   142
    events = ('before_add_relation', 'before_delete_relation',
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   143
              'after_add_entity', 'before_update_entity')
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   144
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   145
    def __call__(self):
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
   146
        dest = self._cw.vreg.config['supervising-addrs']
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   147
        if not dest: # no supervisors, don't do this for nothing...
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   148
            return
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   149
        if self._call():
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
   150
            SupervisionMailOp(self._cw)
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   151
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   152
    def _call(self):
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   153
        event = self.event.split('_', 1)[1]
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   154
        if event == 'update_entity':
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
   155
            if self._cw.added_in_transaction(self.entity.eid):
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   156
                return False
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   157
            if self.entity.e_schema == 'CWUser':
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   158
                if not (self.entity.edited_attributes - frozenset(('eid', 'modification_date',
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   159
                                                                   'last_login_time'))):
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   160
                    # don't record last_login_time update which are done
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   161
                    # automatically at login time
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   162
                    return False
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
   163
        self._cw.transaction_data.setdefault('pendingchanges', []).append(
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   164
            (event, self))
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   165
        return True
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   166
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   167
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   168
class EntityDeleteHook(SomethingChangedHook):
3376
f5c69485381f [appobjects] use __regid__ instead of __id__, more explicit
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163
diff changeset
   169
    __regid__ = 'supervisingentitydel'
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   170
    events = ('before_delete_entity',)
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   171
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   172
    def _call(self):
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   173
        try:
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   174
            title = self.entity.dc_title()
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   175
        except:
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   176
            # may raise an error during deletion process, for instance due to
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   177
            # missing required relation
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   178
            title = '#%s' % eid
2847
c2ee28f4d4b1 use ._cw instead of .cw_req
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2841
diff changeset
   179
        self._cw.transaction_data.setdefault('pendingchanges', []).append(
3427
78dfef45ee48 [notification] add missing import, fix dummy AttributeError in RenderAndSendNotificationView
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3422
diff changeset
   180
            ('delete_entity', (self.entity.eid, str(self.entity.e_schema), title)))
2841
107ba1c45227 rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   181
        return True