sobjects/notification.py
author Pierre-Yves David <pierre-yves.david@logilab.fr>
Thu, 25 Apr 2013 13:34:48 +0200
changeset 8931 4b195bd82e8b
parent 8897 11f7ccd2fa72
child 8932 4ae4242bceb1
permissions -rw-r--r--
[notification] move notification view in ``sobject.notification`` It has no user outside this module. This enforce serversideness of notification and allow future cleanup. No backward compat is set up to prevent circular import. The class has no other user anyway. (closes 2845144)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8190
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
     1
# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4930
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4930
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4930
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4930
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4930
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4930
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4930
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4930
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4930
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4930
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4930
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4930
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4930
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4930
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4930
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
5512
e23d681193cd cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5424
diff changeset
    18
"""some views to handle notification on data changes"""
0
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
__docformat__ = "restructuredtext en"
2101
08003e0354a7 update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1977
diff changeset
    21
_ = unicode
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
from itertools import repeat
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
from logilab.common.textutils import normalize_text
4719
aaed3f813ef8 kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    26
from logilab.common.deprecation import class_renamed, class_moved, deprecated
8190
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
    27
from logilab.common.registry import yes
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
8931
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    29
from cubicweb.entity import Entity
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    30
from cubicweb.view import Component, EntityView
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
    31
from cubicweb.server.hook import SendMailOp
8931
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    32
from cubicweb.mail import construct_message_id, format_mail
2879
ae26a80c0635 move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
    33
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
class RecipientsFinder(Component):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
    """this component is responsible to find recipients of a notification
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
    by default user's with their email set are notified if any, else the default
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
    email addresses specified in the configuration are used
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
    """
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163
diff changeset
    41
    __regid__ = 'recipients_finder'
781
323656dd85a9 fix import, use non_final_entity instead of implements('Any')
sylvain.thenault@logilab.fr
parents: 761
diff changeset
    42
    __select__ = yes()
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
    43
    user_rql = ('Any X,E,A WHERE X is CWUser, X in_state S, S name "activated",'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
                'X primary_email E, E address A')
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1398 1410
diff changeset
    45
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
    def recipients(self):
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
    47
        mode = self._cw.vreg.config['default-recipients-mode']
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
        if mode == 'users':
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: 4719
diff changeset
    49
            execute = self._cw.execute
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5512
diff changeset
    50
            dests = [(u.cw_adapt_to('IEmailable').get_email(),
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5512
diff changeset
    51
                      u.property_value('ui.language'))
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: 4719
diff changeset
    52
                     for u in execute(self.user_rql, build_descr=True).entities()]
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    53
        elif mode == 'default-dest-addrs':
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
    54
            lang = self._cw.vreg.property_value('ui.language')
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
    55
            dests = zip(self._cw.vreg.config['default-dest-addrs'], repeat(lang))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
        else: # mode == 'none'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
            dests = []
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
        return dests
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1398 1410
diff changeset
    60
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
# abstract or deactivated notification views and mixin ########################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
8931
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    63
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    64
class SkipEmail(Exception):
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    65
    """raise this if you decide to skip an email during its generation"""
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    66
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    67
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    68
class BaseNotificationView(EntityView):
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    69
    """abstract view implementing the "email" API (eg to simplify sending
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    70
    notification)
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    71
    """
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    72
    __abstract__ = True
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    73
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    74
    # XXX refactor this class to work with len(rset) > 1
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    75
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    76
    msgid_timestamp = True
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    77
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    78
    # to be defined on concrete sub-classes
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    79
    content = None # body of the mail
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    80
    message = None # action verb of the subject
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    81
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    82
    # this is usually the method to call
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    83
    def render_and_send(self, **kwargs):
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    84
        """generate and send an email message for this view"""
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    85
        delayed = kwargs.pop('delay_to_commit', None)
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    86
        for recipients, msg in self.render_emails(**kwargs):
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    87
            if delayed is None:
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    88
                self.send(recipients, msg)
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    89
            elif delayed:
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    90
                self.send_on_commit(recipients, msg)
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    91
            else:
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    92
                self.send_now(recipients, msg)
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    93
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    94
    def cell_call(self, row, col=0, **kwargs):
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    95
        self.w(self._cw._(self.content) % self.context(**kwargs))
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    96
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    97
    def render_emails(self, **kwargs):
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    98
        """generate and send emails for this view (one per recipient)"""
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    99
        self._kwargs = kwargs
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   100
        recipients = self.recipients()
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   101
        if not recipients:
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   102
            self.info('skipping %s notification, no recipients', self.__regid__)
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   103
            return
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   104
        if self.cw_rset is not None:
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   105
            entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0)
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   106
            # if the view is using timestamp in message ids, no way to reference
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   107
            # previous email
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   108
            if not self.msgid_timestamp:
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   109
                refs = [self.construct_message_id(eid)
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   110
                        for eid in entity.cw_adapt_to('INotifiable').notification_references(self)]
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   111
            else:
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   112
                refs = ()
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   113
            msgid = self.construct_message_id(entity.eid)
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   114
        else:
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   115
            refs = ()
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   116
            msgid = None
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   117
        req = self._cw
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   118
        self.user_data = req.user_data()
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   119
        origlang = req.lang
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   120
        for something in recipients:
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   121
            if isinstance(something, Entity):
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   122
                # hi-jack self._cw to get a session for the returned user
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   123
                self._cw = self._cw.hijack_user(something)
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   124
                emailaddr = something.cw_adapt_to('IEmailable').get_email()
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   125
            else:
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   126
                emailaddr, lang = something
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   127
                self._cw.set_language(lang)
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   128
            # since the same view (eg self) may be called multiple time and we
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   129
            # need a fresh stream at each iteration, reset it explicitly
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   130
            self.w = None
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   131
            # XXX call render before subject to set .row/.col attributes on the
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   132
            #     view
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   133
            try:
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   134
                content = self.render(row=0, col=0, **kwargs)
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   135
                subject = self.subject()
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   136
            except SkipEmail:
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   137
                continue
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   138
            except Exception as ex:
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   139
                # shouldn't make the whole transaction fail because of rendering
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   140
                # error (unauthorized or such) XXX check it doesn't actually
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   141
                # occurs due to rollback on such error
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   142
                self.exception(str(ex))
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   143
                continue
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   144
            msg = format_mail(self.user_data, [emailaddr], content, subject,
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   145
                              config=self._cw.vreg.config, msgid=msgid, references=refs)
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   146
            yield [emailaddr], msg
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   147
        # restore language
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   148
        req.set_language(origlang)
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   149
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   150
    # recipients / email sending ###############################################
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   151
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   152
    def recipients(self):
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   153
        """return a list of either 2-uple (email, language) or user entity to
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   154
        who this email should be sent
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   155
        """
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   156
        finder = self._cw.vreg['components'].select(
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   157
            'recipients_finder', self._cw, rset=self.cw_rset,
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   158
            row=self.cw_row or 0, col=self.cw_col or 0)
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   159
        return finder.recipients()
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   160
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   161
    def send_now(self, recipients, msg):
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   162
        self._cw.vreg.config.sendmails([(msg, recipients)])
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   163
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   164
    def send_on_commit(self, recipients, msg):
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   165
        raise NotImplementedError
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   166
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   167
    send = send_now
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   168
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   169
    # email generation helpers #################################################
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   170
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   171
    def construct_message_id(self, eid):
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   172
        return construct_message_id(self._cw.vreg.config.appid, eid,
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   173
                                    self.msgid_timestamp)
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   174
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   175
    def format_field(self, attr, value):
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   176
        return ':%(attr)s: %(value)s' % {'attr': attr, 'value': value}
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   177
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   178
    def format_section(self, attr, value):
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   179
        return '%(attr)s\n%(ul)s\n%(value)s\n' % {
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   180
            'attr': attr, 'ul': '-'*len(attr), 'value': value}
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   181
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   182
    def subject(self):
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   183
        entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0)
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   184
        subject = self._cw._(self.message)
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   185
        etype = entity.dc_type()
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   186
        eid = entity.eid
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   187
        login = self.user_data['login']
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   188
        return self._cw._('%(subject)s %(etype)s #%(eid)s (%(login)s)') % locals()
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   189
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   190
    def context(self, **kwargs):
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   191
        entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0)
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   192
        for key, val in kwargs.iteritems():
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   193
            if val and isinstance(val, unicode) and val.strip():
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   194
               kwargs[key] = self._cw._(val)
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   195
        kwargs.update({'user': self.user_data['login'],
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   196
                       'eid': entity.eid,
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   197
                       'etype': entity.dc_type(),
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   198
                       'url': entity.absolute_url(),
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   199
                       'title': entity.dc_long_title(),})
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   200
        return kwargs
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   201
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   202
6807
7330d1fa02dd [cleanup] pylint detected error (among a ton of false positives due to the dynamic nature of cubicweb's code...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   203
class NotificationView(BaseNotificationView):
2879
ae26a80c0635 move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
   204
    """overriden to delay actual sending of mails to a commit operation by
ae26a80c0635 move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
   205
    default
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   206
    """
2879
ae26a80c0635 move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
   207
    def send_on_commit(self, recipients, msg):
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   208
        SendMailOp(self._cw, recipients=recipients, msg=msg)
2879
ae26a80c0635 move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
   209
    send = send_on_commit
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   210
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   211
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   212
class StatusChangeMixIn(object):
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163
diff changeset
   213
    __regid__ = 'notif_status_change'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   214
    msgid_timestamp = True
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   215
    message = _('status changed')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   216
    content = _("""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   217
%(user)s changed status from <%(previous_state)s> to <%(current_state)s> for entity
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   218
'%(title)s'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   219
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   220
%(comment)s
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   221
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   222
url: %(url)s
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   223
""")
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   224
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   225
738
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   226
###############################################################################
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   227
# Actual notification views.                                                  #
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   228
#                                                                             #
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   229
# disable them at the recipients_finder level if you don't want them          #
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   230
###############################################################################
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   231
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   232
# XXX should be based on dc_title/dc_description, no?
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   233
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   234
class ContentAddedView(NotificationView):
2879
ae26a80c0635 move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
   235
    """abstract class for notification on entity/relation
ae26a80c0635 move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
   236
ae26a80c0635 move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
   237
    all you have to do by default is :
ae26a80c0635 move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
   238
    * set id and __select__ attributes to match desired events and entity types
ae26a80c0635 move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
   239
    * set a content attribute to define the content of the email (unless you
ae26a80c0635 move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
   240
      override call)
ae26a80c0635 move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2650
diff changeset
   241
    """
738
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   242
    __abstract__ = True
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163
diff changeset
   243
    __regid__ = 'notif_after_add_entity'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   244
    msgid_timestamp = False
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   245
    message = _('new')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   246
    content = """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   247
%(title)s
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   248
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   249
%(content)s
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   250
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   251
url: %(url)s
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   252
"""
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6807
diff changeset
   253
    # to be defined on concrete sub-classes
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6807
diff changeset
   254
    content_attr = None
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   255
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   256
    def context(self, **kwargs):
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   257
        entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   258
        content = entity.printable_value(self.content_attr, format='text/plain')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   259
        if content:
3004
09ab5e93a02c [notification] fix #103822, don't try to wrap text/rest to 80 characters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2879
diff changeset
   260
            contentformat = getattr(entity, self.content_attr + '_format',
09ab5e93a02c [notification] fix #103822, don't try to wrap text/rest to 80 characters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2879
diff changeset
   261
                                    'text/rest')
09ab5e93a02c [notification] fix #103822, don't try to wrap text/rest to 80 characters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2879
diff changeset
   262
            # XXX don't try to wrap rest until we've a proper transformation (see
09ab5e93a02c [notification] fix #103822, don't try to wrap text/rest to 80 characters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2879
diff changeset
   263
            # #103822)
09ab5e93a02c [notification] fix #103822, don't try to wrap text/rest to 80 characters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2879
diff changeset
   264
            if contentformat != 'text/rest':
09ab5e93a02c [notification] fix #103822, don't try to wrap text/rest to 80 characters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2879
diff changeset
   265
                content = normalize_text(content, 80)
761
sylvain.thenault@logilab.fr
parents: 738
diff changeset
   266
        return super(ContentAddedView, self).context(content=content, **kwargs)
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1398 1410
diff changeset
   267
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   268
    def subject(self):
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   269
        entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0)
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   270
        return  u'%s #%s (%s)' % (self._cw.__('New %s' % entity.e_schema),
3110
757d36162235 enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3051
diff changeset
   271
                                  entity.eid, self.user_data['login'])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   272
3525
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   273
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   274
def format_value(value):
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   275
    if isinstance(value, unicode):
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   276
        return u'"%s"' % value
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   277
    return value
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   278
3536
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3418 3525
diff changeset
   279
3525
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   280
class EntityUpdatedNotificationView(NotificationView):
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   281
    """abstract class for notification on entity/relation
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   282
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   283
    all you have to do by default is :
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   284
    * set id and __select__ attributes to match desired events and entity types
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   285
    * set a content attribute to define the content of the email (unless you
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   286
      override call)
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   287
    """
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   288
    __abstract__ = True
4926
626d31035662 3.6 want __regid__, not id
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   289
    __regid__ = 'notif_entity_updated'
3525
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   290
    msgid_timestamp = False
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   291
    message = _('updated')
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   292
    no_detailed_change_attrs = ()
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   293
    content = """
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   294
Properties have been updated by %(user)s:
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   295
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   296
%(changes)s
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   297
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   298
url: %(url)s
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   299
"""
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   300
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   301
    def context(self, **kwargs):
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   302
        context = super(EntityUpdatedNotificationView, self).context(**kwargs)
4230
6514a76eaa5c 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4023
diff changeset
   303
        changes = self._cw.transaction_data['changes'][self.cw_rset[0][0]]
6514a76eaa5c 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4023
diff changeset
   304
        _ = self._cw._
3525
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   305
        formatted_changes = []
4230
6514a76eaa5c 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4023
diff changeset
   306
        entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0)
3525
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   307
        for attr, oldvalue, newvalue in sorted(changes):
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   308
            # check current user has permission to see the attribute
4230
6514a76eaa5c 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4023
diff changeset
   309
            rschema = self._cw.vreg.schema[attr]
3689
deb13e88e037 follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3580
diff changeset
   310
            if rschema.final:
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   311
                rdef = entity.e_schema.rdef(rschema)
4230
6514a76eaa5c 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4023
diff changeset
   312
                if not rdef.has_perm(self._cw, 'read', eid=self.cw_rset[0][0]):
3525
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   313
                    continue
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   314
            # XXX suppose it's a subject relation...
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: 4719
diff changeset
   315
            elif not rschema.has_perm(self._cw, 'read',
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: 4719
diff changeset
   316
                                      fromeid=self.cw_rset[0][0]):
3525
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   317
                continue
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   318
            if attr in self.no_detailed_change_attrs:
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   319
                msg = _('%s updated') % _(attr)
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   320
            elif oldvalue not in (None, ''):
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   321
                msg = _('%(attr)s updated from %(oldvalue)s to %(newvalue)s') % {
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   322
                    'attr': _(attr),
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   323
                    'oldvalue': format_value(oldvalue),
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   324
                    'newvalue': format_value(newvalue)}
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   325
            else:
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   326
                msg = _('%(attr)s set to %(newvalue)s') % {
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   327
                    'attr': _(attr), 'newvalue': format_value(newvalue)}
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   328
            formatted_changes.append('* ' + msg)
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   329
        if not formatted_changes:
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   330
            # current user isn't allowed to see changes, skip this notification
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   331
            raise SkipEmail()
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   332
        context['changes'] = '\n'.join(formatted_changes)
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   333
        return context
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   334
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   335
    def subject(self):
4230
6514a76eaa5c 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4023
diff changeset
   336
        entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0)
6514a76eaa5c 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4023
diff changeset
   337
        return  u'%s #%s (%s)' % (self._cw.__('Updated %s' % entity.e_schema),
3525
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   338
                                  entity.eid, self.user_data['login'])