hooks/notification.py
changeset 2847 c2ee28f4d4b1
parent 2841 107ba1c45227
child 2900 9d65e0350aa1
equal deleted inserted replaced
2846:e71d6a585b83 2847:c2ee28f4d4b1
    26 class NotificationHook(hook.Hook):
    26 class NotificationHook(hook.Hook):
    27     __abstract__ = True
    27     __abstract__ = True
    28     category = 'notification'
    28     category = 'notification'
    29 
    29 
    30     def select_view(self, vid, rset, row=0, col=0):
    30     def select_view(self, vid, rset, row=0, col=0):
    31         return self.cw_req.vreg['views'].select_object(vid, self.cw_req,
    31         return self._cw.vreg['views'].select_object(vid, self._cw,
    32                                                        rset=rset, row=0, col=0)
    32                                                        rset=rset, row=0, col=0)
    33 
    33 
    34 
    34 
    35 class StatusChangeHook(NotificationHook):
    35 class StatusChangeHook(NotificationHook):
    36     """notify when a workflowable entity has its state modified"""
    36     """notify when a workflowable entity has its state modified"""
    48             return
    48             return
    49         comment = entity.printable_value('comment', format='text/plain')
    49         comment = entity.printable_value('comment', format='text/plain')
    50         if comment:
    50         if comment:
    51             comment = normalize_text(comment, 80,
    51             comment = normalize_text(comment, 80,
    52                                      rest=entity.comment_format=='text/rest')
    52                                      rest=entity.comment_format=='text/rest')
    53         RenderAndSendNotificationView(self.cw_req, view=view, viewargs={
    53         RenderAndSendNotificationView(self._cw, view=view, viewargs={
    54             'comment': comment, 'previous_state': entity.previous_state.name,
    54             'comment': comment, 'previous_state': entity.previous_state.name,
    55             'current_state': entity.new_state.name})
    55             'current_state': entity.new_state.name})
    56 
    56 
    57 
    57 
    58 class RelationChangeHook(NotificationHook):
    58 class RelationChangeHook(NotificationHook):
    62 
    62 
    63     def __call__(self):
    63     def __call__(self):
    64         """if a notification view is defined for the event, send notification
    64         """if a notification view is defined for the event, send notification
    65         email defined by the view
    65         email defined by the view
    66         """
    66         """
    67         rset = self.cw_req.eid_rset(self.eidfrom)
    67         rset = self._cw.eid_rset(self.eidfrom)
    68         view = self.select_view('notif_%s_%s' % (self.event,  self.rtype),
    68         view = self.select_view('notif_%s_%s' % (self.event,  self.rtype),
    69                                 rset=rset, row=0)
    69                                 rset=rset, row=0)
    70         if view is None:
    70         if view is None:
    71             return
    71             return
    72         RenderAndSendNotificationView(self.cw_req, view=view)
    72         RenderAndSendNotificationView(self._cw, view=view)
    73 
    73 
    74 
    74 
    75 class EntityChangeHook(NotificationHook):
    75 class EntityChangeHook(NotificationHook):
    76     """if a notification view is defined for the event, send notification
    76     """if a notification view is defined for the event, send notification
    77     email defined by the view
    77     email defined by the view
    82     def __call__(self):
    82     def __call__(self):
    83         rset = self.entity.as_rset()
    83         rset = self.entity.as_rset()
    84         view = self.select_view('notif_%s' % self.event, rset=rset, row=0)
    84         view = self.select_view('notif_%s' % self.event, rset=rset, row=0)
    85         if view is None:
    85         if view is None:
    86             return
    86             return
    87         RenderAndSendNotificationView(self.cw_req, view=view)
    87         RenderAndSendNotificationView(self._cw, view=view)
    88 
    88 
    89 
    89 
    90 # supervising ##################################################################
    90 # supervising ##################################################################
    91 
    91 
    92 class SomethingChangedHook(NotificationHook):
    92 class SomethingChangedHook(NotificationHook):
    93     __id__ = 'supervising'
    93     __id__ = 'supervising'
    94     events = ('before_add_relation', 'before_delete_relation',
    94     events = ('before_add_relation', 'before_delete_relation',
    95               'after_add_entity', 'before_update_entity')
    95               'after_add_entity', 'before_update_entity')
    96 
    96 
    97     def __call__(self):
    97     def __call__(self):
    98         dest = self.cw_req.vreg.config['supervising-addrs']
    98         dest = self._cw.vreg.config['supervising-addrs']
    99         if not dest: # no supervisors, don't do this for nothing...
    99         if not dest: # no supervisors, don't do this for nothing...
   100             return
   100             return
   101         if self._call():
   101         if self._call():
   102             SupervisionMailOp(self.cw_req)
   102             SupervisionMailOp(self._cw)
   103 
   103 
   104     def _call(self):
   104     def _call(self):
   105         event = self.event.split('_', 1)[1]
   105         event = self.event.split('_', 1)[1]
   106         if event == 'update_entity':
   106         if event == 'update_entity':
   107             if self.cw_req.added_in_transaction(self.entity.eid):
   107             if self._cw.added_in_transaction(self.entity.eid):
   108                 return False
   108                 return False
   109             if self.entity.e_schema == 'CWUser':
   109             if self.entity.e_schema == 'CWUser':
   110                 if not (self.entity.edited_attributes - frozenset(('eid', 'modification_date',
   110                 if not (self.entity.edited_attributes - frozenset(('eid', 'modification_date',
   111                                                                    'last_login_time'))):
   111                                                                    'last_login_time'))):
   112                     # don't record last_login_time update which are done
   112                     # don't record last_login_time update which are done
   113                     # automatically at login time
   113                     # automatically at login time
   114                     return False
   114                     return False
   115         self.cw_req.transaction_data.setdefault('pendingchanges', []).append(
   115         self._cw.transaction_data.setdefault('pendingchanges', []).append(
   116             (event, self))
   116             (event, self))
   117         return True
   117         return True
   118 
   118 
   119 
   119 
   120 class EntityDeleteHook(SomethingChangedHook):
   120 class EntityDeleteHook(SomethingChangedHook):
   126             title = self.entity.dc_title()
   126             title = self.entity.dc_title()
   127         except:
   127         except:
   128             # may raise an error during deletion process, for instance due to
   128             # may raise an error during deletion process, for instance due to
   129             # missing required relation
   129             # missing required relation
   130             title = '#%s' % eid
   130             title = '#%s' % eid
   131         self.cw_req.transaction_data.setdefault('pendingchanges', []).append(
   131         self._cw.transaction_data.setdefault('pendingchanges', []).append(
   132             ('delete_entity', (self.eid, str(self.entity.e_schema), title)))
   132             ('delete_entity', (self.eid, str(self.entity.e_schema), title)))
   133         return True
   133         return True