sobjects/notification.py
changeset 3536 f6c9a5df80fb
parent 3418 7b49fa7e942d
parent 3525 2dc3908f667f
child 3589 a5432f99f2d9
equal deleted inserted replaced
3524:a3431f4e2f40 3536:f6c9a5df80fb
    16 from cubicweb.selectors import yes
    16 from cubicweb.selectors import yes
    17 from cubicweb.view import Component
    17 from cubicweb.view import Component
    18 from cubicweb.common.mail import format_mail
    18 from cubicweb.common.mail import format_mail
    19 from cubicweb.common.mail import NotificationView
    19 from cubicweb.common.mail import NotificationView
    20 from cubicweb.server.hook import SendMailOp
    20 from cubicweb.server.hook import SendMailOp
       
    21 
       
    22 parse_message_id = deprecated('parse_message_id is now defined in cubicweb.common.mail')(parse_message_id)
    21 
    23 
    22 
    24 
    23 class RecipientsFinder(Component):
    25 class RecipientsFinder(Component):
    24     """this component is responsible to find recipients of a notification
    26     """this component is responsible to find recipients of a notification
    25 
    27 
   116         entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0)
   118         entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0)
   117         return  u'%s #%s (%s)' % (self._cw.__('New %s' % entity.e_schema),
   119         return  u'%s #%s (%s)' % (self._cw.__('New %s' % entity.e_schema),
   118                                   entity.eid, self.user_data['login'])
   120                                   entity.eid, self.user_data['login'])
   119 
   121 
   120 
   122 
       
   123 def format_value(value):
       
   124     if isinstance(value, unicode):
       
   125         return u'"%s"' % value
       
   126     return value
       
   127 
       
   128 
       
   129 class EntityUpdatedNotificationView(NotificationView):
       
   130     """abstract class for notification on entity/relation
       
   131 
       
   132     all you have to do by default is :
       
   133     * set id and __select__ attributes to match desired events and entity types
       
   134     * set a content attribute to define the content of the email (unless you
       
   135       override call)
       
   136     """
       
   137     __abstract__ = True
       
   138     id = 'notif_entity_updated'
       
   139     msgid_timestamp = False
       
   140     message = _('updated')
       
   141     no_detailed_change_attrs = ()
       
   142     content = """
       
   143 Properties have been updated by %(user)s:
       
   144 
       
   145 %(changes)s
       
   146 
       
   147 url: %(url)s
       
   148 """
       
   149 
       
   150     def context(self, **kwargs):
       
   151         context = super(EntityUpdatedNotificationView, self).context(**kwargs)
       
   152         changes = self.req.transaction_data['changes'][self.rset[0][0]]
       
   153         _ = self.req._
       
   154         formatted_changes = []
       
   155         for attr, oldvalue, newvalue in sorted(changes):
       
   156             # check current user has permission to see the attribute
       
   157             rschema = self.vreg.schema[attr]
       
   158             if rschema.is_final():
       
   159                 if not rschema.has_perm(self.req, 'read', eid=self.rset[0][0]):
       
   160                     continue
       
   161             # XXX suppose it's a subject relation...
       
   162             elif not rschema.has_perm(self.req, 'read', fromeid=self.rset[0][0]):
       
   163                 continue
       
   164             if attr in self.no_detailed_change_attrs:
       
   165                 msg = _('%s updated') % _(attr)
       
   166             elif oldvalue not in (None, ''):
       
   167                 msg = _('%(attr)s updated from %(oldvalue)s to %(newvalue)s') % {
       
   168                     'attr': _(attr),
       
   169                     'oldvalue': format_value(oldvalue),
       
   170                     'newvalue': format_value(newvalue)}
       
   171             else:
       
   172                 msg = _('%(attr)s set to %(newvalue)s') % {
       
   173                     'attr': _(attr), 'newvalue': format_value(newvalue)}
       
   174             formatted_changes.append('* ' + msg)
       
   175         if not formatted_changes:
       
   176             # current user isn't allowed to see changes, skip this notification
       
   177             raise SkipEmail()
       
   178         context['changes'] = '\n'.join(formatted_changes)
       
   179         return context
       
   180 
       
   181     def subject(self):
       
   182         entity = self.entity(self.row or 0, self.col or 0)
       
   183         return  u'%s #%s (%s)' % (self.req.__('Updated %s' % entity.e_schema),
       
   184                                   entity.eid, self.user_data['login'])
       
   185 
       
   186 
   121 from logilab.common.deprecation import class_renamed, class_moved, deprecated
   187 from logilab.common.deprecation import class_renamed, class_moved, deprecated
   122 from cubicweb.hooks.notification import RenderAndSendNotificationView
   188 from cubicweb.hooks.notification import RenderAndSendNotificationView
   123 from cubicweb.common.mail import parse_message_id
   189 from cubicweb.common.mail import parse_message_id
   124 
   190 
   125 NormalizedTextView = class_renamed('NormalizedTextView', ContentAddedView)
   191 NormalizedTextView = class_renamed('NormalizedTextView', ContentAddedView)