cubicweb/sobjects/supervising.py
changeset 11057 0b59724cb3f2
parent 10666 7f6b5f023884
child 11765 9cb215e833b0
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
       
     1 # copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    18 """some hooks and views to handle supervising of any data changes"""
       
    19 
       
    20 __docformat__ = "restructuredtext en"
       
    21 from cubicweb import _
       
    22 
       
    23 from cubicweb import UnknownEid
       
    24 from cubicweb.predicates import none_rset
       
    25 from cubicweb.schema import display_name
       
    26 from cubicweb.view import Component
       
    27 from cubicweb.mail import format_mail
       
    28 from cubicweb.server.hook import SendMailOp
       
    29 
       
    30 
       
    31 def filter_changes(changes):
       
    32     """
       
    33     * when an entity has been deleted:
       
    34       * don't show deletion of its relations
       
    35       * don't show related TrInfo deletion if any
       
    36     * when an entity has been added don't show owned_by relation addition
       
    37     * don't show new TrInfo entities if any
       
    38     """
       
    39     # first build an index of changes
       
    40     index = {}
       
    41     added, deleted = set(), set()
       
    42     for change in changes[:]:
       
    43         event, changedescr = change
       
    44         if event == 'add_entity':
       
    45             entity = changedescr.entity
       
    46             added.add(entity.eid)
       
    47             if entity.e_schema == 'TrInfo':
       
    48                 changes.remove(change)
       
    49                 event = 'change_state'
       
    50                 change = (event,
       
    51                           (entity.wf_info_for[0],
       
    52                            entity.from_state[0], entity.to_state[0]))
       
    53                 changes.append(change)
       
    54         elif event == 'delete_entity':
       
    55             deleted.add(changedescr[0])
       
    56         index.setdefault(event, set()).add(change)
       
    57     for key in ('delete_relation', 'add_relation'):
       
    58         for change in index.get(key, {}).copy():
       
    59             if change[1].rtype == 'in_state':
       
    60                 index[key].remove(change)
       
    61     # filter changes
       
    62     for eid in added:
       
    63         try:
       
    64             for change in index['add_relation'].copy():
       
    65                 changedescr = change[1]
       
    66                 # skip meta-relations which are set automatically
       
    67                 # XXX generate list below using rtags (category = 'generated')
       
    68                 if changedescr.rtype in ('created_by', 'owned_by', 'is', 'is_instance_of',
       
    69                                       'from_state', 'to_state', 'by_transition',
       
    70                                       'wf_info_for') \
       
    71                        and changedescr.eidfrom == eid:
       
    72                     index['add_relation'].remove(change)
       
    73         except KeyError:
       
    74             break
       
    75     for eid in deleted:
       
    76         try:
       
    77             for change in index['delete_relation'].copy():
       
    78                 if change[1].eidfrom == eid:
       
    79                     index['delete_relation'].remove(change)
       
    80                 elif change[1].eidto == eid:
       
    81                     index['delete_relation'].remove(change)
       
    82                     if change[1].rtype == 'wf_info_for':
       
    83                         for change_ in index['delete_entity'].copy():
       
    84                             if change_[1].eidfrom == change[1].eidfrom:
       
    85                                 index['delete_entity'].remove(change_)
       
    86         except KeyError:
       
    87             break
       
    88     for change in changes:
       
    89         event, changedescr = change
       
    90         if change in index[event]:
       
    91             yield change
       
    92 
       
    93 
       
    94 class SupervisionEmailView(Component):
       
    95     """view implementing the email API for data changes supervision notification
       
    96     """
       
    97     __regid__ = 'supervision_notif'
       
    98     __select__ = none_rset()
       
    99 
       
   100     def recipients(self):
       
   101         return self._cw.vreg.config['supervising-addrs']
       
   102 
       
   103     def subject(self):
       
   104         return self._cw._('[%s supervision] changes summary') % self._cw.vreg.config.appid
       
   105 
       
   106     def call(self, changes):
       
   107         user = self._cw.user
       
   108         self.w(self._cw._('user %s has made the following change(s):\n\n')
       
   109                % user.login)
       
   110         for event, changedescr in filter_changes(changes):
       
   111             self.w(u'* ')
       
   112             getattr(self, event)(changedescr)
       
   113             self.w(u'\n\n')
       
   114 
       
   115     def _entity_context(self, entity):
       
   116         return {'eid': entity.eid,
       
   117                 'etype': entity.dc_type().lower(),
       
   118                 'title': entity.dc_title()}
       
   119 
       
   120     def add_entity(self, changedescr):
       
   121         msg = self._cw._('added %(etype)s #%(eid)s (%(title)s)')
       
   122         self.w(u'%s\n' % (msg % self._entity_context(changedescr.entity)))
       
   123         self.w(u'  %s' % changedescr.entity.absolute_url())
       
   124 
       
   125     def update_entity(self, changedescr):
       
   126         msg = self._cw._('updated %(etype)s #%(eid)s (%(title)s)')
       
   127         self.w(u'%s\n' % (msg % self._entity_context(changedescr.entity)))
       
   128         # XXX print changes
       
   129         self.w(u'  %s' % changedescr.entity.absolute_url())
       
   130 
       
   131     def delete_entity(self, args):
       
   132         eid, etype, title = args
       
   133         msg = self._cw._('deleted %(etype)s #%(eid)s (%(title)s)')
       
   134         etype = display_name(self._cw, etype).lower()
       
   135         self.w(msg % locals())
       
   136 
       
   137     def change_state(self, args):
       
   138         _ = self._cw._
       
   139         entity, fromstate, tostate = args
       
   140         msg = _('changed state of %(etype)s #%(eid)s (%(title)s)')
       
   141         self.w(u'%s\n' % (msg % self._entity_context(entity)))
       
   142         self.w(_('  from state %(fromstate)s to state %(tostate)s\n' %
       
   143                  {'fromstate': _(fromstate.name), 'tostate': _(tostate.name)}))
       
   144         self.w(u'  %s' % entity.absolute_url())
       
   145 
       
   146     def _relation_context(self, changedescr):
       
   147         cnx = self._cw
       
   148         def describe(eid):
       
   149             try:
       
   150                 return cnx._(cnx.entity_metas(eid)['type']).lower()
       
   151             except UnknownEid:
       
   152                 # may occurs when an entity has been deleted from an external
       
   153                 # source and we're cleaning its relation
       
   154                 return cnx._('unknown external entity')
       
   155         eidfrom, rtype, eidto = changedescr.eidfrom, changedescr.rtype, changedescr.eidto
       
   156         return {'rtype': cnx._(rtype),
       
   157                 'eidfrom': eidfrom,
       
   158                 'frometype': describe(eidfrom),
       
   159                 'eidto': eidto,
       
   160                 'toetype': describe(eidto)}
       
   161 
       
   162     def add_relation(self, changedescr):
       
   163         msg = self._cw._('added relation %(rtype)s from %(frometype)s #%(eidfrom)s to %(toetype)s #%(eidto)s')
       
   164         self.w(msg % self._relation_context(changedescr))
       
   165 
       
   166     def delete_relation(self, changedescr):
       
   167         msg = self._cw._('deleted relation %(rtype)s from %(frometype)s #%(eidfrom)s to %(toetype)s #%(eidto)s')
       
   168         self.w(msg % self._relation_context(changedescr))
       
   169 
       
   170 
       
   171 class SupervisionMailOp(SendMailOp):
       
   172     """special send email operation which should be done only once for a bunch
       
   173     of changes
       
   174     """
       
   175     def _get_view(self):
       
   176         return self.cnx.vreg['components'].select('supervision_notif', self.cnx)
       
   177 
       
   178     def _prepare_email(self):
       
   179         cnx = self.cnx
       
   180         config = cnx.vreg.config
       
   181         uinfo = {'email': config['sender-addr'],
       
   182                  'name': config['sender-name']}
       
   183         view = self._get_view()
       
   184         content = view.render(changes=cnx.transaction_data.get('pendingchanges'))
       
   185         recipients = view.recipients()
       
   186         msg = format_mail(uinfo, recipients, content, view.subject(), config=config)
       
   187         self.to_send = [(msg, recipients)]
       
   188 
       
   189     def postcommit_event(self):
       
   190         self._prepare_email()
       
   191         SendMailOp.postcommit_event(self)