sobjects/notification.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 09 Apr 2014 17:29:25 +0200
changeset 9765 5607ef9ab9f5
parent 9481 7e35d62c67a9
child 9770 112c884b2d8d
permissions -rw-r--r--
[sobjects] update notifications system to 3.19 api
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9765
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
     1
# copyright 2003-2014 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
9481
7e35d62c67a9 [notification] use an InternalManager object for notifications if we have an (email, lang) tuple
Julien Cristau <julien.cristau@logilab.fr>
parents: 9387
diff changeset
    33
from cubicweb.server.session import Session, InternalManager
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
    34
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
class RecipientsFinder(Component):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
    """this component is responsible to find recipients of a notification
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
    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
    40
    email addresses specified in the configuration are used
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
    """
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163
diff changeset
    42
    __regid__ = 'recipients_finder'
781
323656dd85a9 fix import, use non_final_entity instead of implements('Any')
sylvain.thenault@logilab.fr
parents: 761
diff changeset
    43
    __select__ = yes()
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
    44
    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
    45
                'X primary_email E, E address A')
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1398 1410
diff changeset
    46
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
    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
    48
        mode = self._cw.vreg.config['default-recipients-mode']
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
        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
    50
            execute = self._cw.execute
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5512
diff changeset
    51
            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
    52
                      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
    53
                     for u in execute(self.user_rql, build_descr=True).entities()]
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    54
        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
    55
            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
    56
            dests = zip(self._cw.vreg.config['default-dest-addrs'], repeat(lang))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
        else: # mode == 'none'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
            dests = []
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
        return dests
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1398 1410
diff changeset
    61
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
# abstract or deactivated notification views and mixin ########################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
8931
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    64
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    65
class SkipEmail(Exception):
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    66
    """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
    67
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    68
8932
4ae4242bceb1 [notification] merge BaseNotificationView and NotificationView
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8931
diff changeset
    69
class NotificationView(EntityView):
8931
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    70
    """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
    71
    notification)
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    72
    """
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    73
    # 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
    74
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    75
    msgid_timestamp = True
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    76
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    77
    # 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
    78
    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
    79
    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
    80
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    81
    # 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
    82
    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
    83
        """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
    84
        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
    85
        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
    86
            if delayed is None:
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    87
                self.send(recipients, msg)
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    88
            elif delayed:
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    89
                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
    90
            else:
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    91
                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
    92
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    93
    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
    94
        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
    95
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    96
    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
    97
        """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
    98
        self._kwargs = kwargs
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    99
        recipients = self.recipients()
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   100
        if not recipients:
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   101
            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
   102
            return
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   103
        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
   104
            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
   105
            # 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
   106
            # previous email
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   107
            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
   108
                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
   109
                        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
   110
            else:
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   111
                refs = ()
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   112
            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
   113
        else:
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   114
            refs = ()
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   115
            msgid = None
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   116
        req = self._cw
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   117
        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
   118
        for something in recipients:
9481
7e35d62c67a9 [notification] use an InternalManager object for notifications if we have an (email, lang) tuple
Julien Cristau <julien.cristau@logilab.fr>
parents: 9387
diff changeset
   119
            if isinstance(something, tuple):
7e35d62c67a9 [notification] use an InternalManager object for notifications if we have an (email, lang) tuple
Julien Cristau <julien.cristau@logilab.fr>
parents: 9387
diff changeset
   120
                emailaddr, lang = something
7e35d62c67a9 [notification] use an InternalManager object for notifications if we have an (email, lang) tuple
Julien Cristau <julien.cristau@logilab.fr>
parents: 9387
diff changeset
   121
                user = InternalManager(lang=lang)
7e35d62c67a9 [notification] use an InternalManager object for notifications if we have an (email, lang) tuple
Julien Cristau <julien.cristau@logilab.fr>
parents: 9387
diff changeset
   122
            else:
8931
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   123
                emailaddr = something.cw_adapt_to('IEmailable').get_email()
9481
7e35d62c67a9 [notification] use an InternalManager object for notifications if we have an (email, lang) tuple
Julien Cristau <julien.cristau@logilab.fr>
parents: 9387
diff changeset
   124
                user = something
7e35d62c67a9 [notification] use an InternalManager object for notifications if we have an (email, lang) tuple
Julien Cristau <julien.cristau@logilab.fr>
parents: 9387
diff changeset
   125
            # hi-jack self._cw to get a session for the returned user
9765
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   126
            session = Session(user, self._cw.repo)
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   127
            with session.new_cnx() as cnx:
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   128
                self._cw = cnx
9305
f7a738afc295 [notification] avoid leaking cnxsets (closes #3243810)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9006
diff changeset
   129
                try:
9765
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   130
                    # since the same view (eg self) may be called multiple time and we
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   131
                    # need a fresh stream at each iteration, reset it explicitly
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   132
                    self.w = None
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   133
                    # XXX call render before subject to set .row/.col attributes on the
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   134
                    #     view
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   135
                    try:
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   136
                        content = self.render(row=0, col=0, **kwargs)
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   137
                        subject = self.subject()
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   138
                    except SkipEmail:
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   139
                        continue
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   140
                    except Exception as ex:
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   141
                        # shouldn't make the whole transaction fail because of rendering
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   142
                        # error (unauthorized or such) XXX check it doesn't actually
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   143
                        # occurs due to rollback on such error
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   144
                        self.exception(str(ex))
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   145
                        continue
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   146
                    msg = format_mail(self.user_data, [emailaddr], content, subject,
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   147
                                      config=self._cw.vreg.config, msgid=msgid, references=refs)
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   148
                    yield [emailaddr], msg
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   149
                finally:
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   150
                    # ensure we have a cnxset since commit will fail if there is
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   151
                    # some operation but no cnxset. This may occurs in this very
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   152
                    # specific case (eg SendMailOp)
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   153
                    with cnx.ensure_cnx_set:
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   154
                        cnx.commit()
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   155
                    self._cw = req
8931
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   156
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   157
    # recipients / email sending ###############################################
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   158
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   159
    def recipients(self):
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   160
        """return a list of either 2-uple (email, language) or user entity to
9481
7e35d62c67a9 [notification] use an InternalManager object for notifications if we have an (email, lang) tuple
Julien Cristau <julien.cristau@logilab.fr>
parents: 9387
diff changeset
   161
        whom this email should be sent
8931
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   162
        """
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   163
        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
   164
            '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
   165
            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
   166
        return finder.recipients()
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   167
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   168
    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
   169
        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
   170
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   171
    def send_on_commit(self, recipients, msg):
8932
4ae4242bceb1 [notification] merge BaseNotificationView and NotificationView
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8931
diff changeset
   172
        SendMailOp(self._cw, recipients=recipients, msg=msg)
4ae4242bceb1 [notification] merge BaseNotificationView and NotificationView
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8931
diff changeset
   173
    send = send_on_commit
8931
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
    # email generation helpers #################################################
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   176
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   177
    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
   178
        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
   179
                                    self.msgid_timestamp)
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   180
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   181
    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
   182
        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
   183
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   184
    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
   185
        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
   186
            '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
   187
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   188
    def subject(self):
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   189
        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
   190
        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
   191
        etype = entity.dc_type()
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   192
        eid = entity.eid
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   193
        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
   194
        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
   195
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   196
    def context(self, **kwargs):
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   197
        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
   198
        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
   199
            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
   200
               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
   201
        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
   202
                       'eid': entity.eid,
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   203
                       'etype': entity.dc_type(),
9387
f31f0c8bda4f [notification] Use https url when available in notification view context
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9305
diff changeset
   204
                       'url': entity.absolute_url(__secure__=True),
8931
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   205
                       '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
   206
        return kwargs
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   207
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   208
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   209
class StatusChangeMixIn(object):
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163
diff changeset
   210
    __regid__ = 'notif_status_change'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   211
    msgid_timestamp = True
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   212
    message = _('status changed')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   213
    content = _("""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   214
%(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
   215
'%(title)s'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   216
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   217
%(comment)s
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   218
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   219
url: %(url)s
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   220
""")
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
738
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   223
###############################################################################
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   224
# Actual notification views.                                                  #
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   225
#                                                                             #
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   226
# 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
   227
###############################################################################
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
# 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
   230
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   231
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
   232
    """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
   233
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
   234
    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
   235
    * 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
   236
    * 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
   237
      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
   238
    """
738
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   239
    __abstract__ = True
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163
diff changeset
   240
    __regid__ = 'notif_after_add_entity'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   241
    msgid_timestamp = False
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   242
    message = _('new')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   243
    content = """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   244
%(title)s
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   245
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   246
%(content)s
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   247
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   248
url: %(url)s
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   249
"""
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
   250
    # 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
   251
    content_attr = None
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   252
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   253
    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
   254
        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
   255
        content = entity.printable_value(self.content_attr, format='text/plain')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   256
        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
   257
            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
   258
                                    '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
   259
            # 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
   260
            # #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
   261
            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
   262
                content = normalize_text(content, 80)
761
sylvain.thenault@logilab.fr
parents: 738
diff changeset
   263
        return super(ContentAddedView, self).context(content=content, **kwargs)
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1398 1410
diff changeset
   264
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   265
    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
   266
        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
   267
        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
   268
                                  entity.eid, self.user_data['login'])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   269
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
   270
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   271
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
   272
    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
   273
        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
   274
    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
   275
3536
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3418 3525
diff changeset
   276
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
   277
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
   278
    """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
   279
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
    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
   281
    * 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
   282
    * 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
   283
      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
   284
    """
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
    __abstract__ = True
4926
626d31035662 3.6 want __regid__, not id
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   286
    __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
   287
    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
   288
    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
   289
    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
   290
    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
   291
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
   292
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
%(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
   294
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
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
   296
"""
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
9006
e4ea8f9ffa11 notification: use viewargs for notif_entity_updated instead of transaction_data
Julien Cristau <julien.cristau@logilab.fr>
parents: 9005
diff changeset
   298
    def context(self, changes=(), **kwargs):
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
   299
        context = super(EntityUpdatedNotificationView, self).context(**kwargs)
4230
6514a76eaa5c 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4023
diff changeset
   300
        _ = 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
   301
        formatted_changes = []
4230
6514a76eaa5c 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4023
diff changeset
   302
        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
   303
        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
   304
            # 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
   305
            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
   306
            if rschema.final:
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   307
                rdef = entity.e_schema.rdef(rschema)
4230
6514a76eaa5c 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4023
diff changeset
   308
                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
   309
                    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
   310
            # 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
   311
            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
   312
                                      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
   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
            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
   315
                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
   316
            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
   317
                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
   318
                    '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
   319
                    '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
   320
                    '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
   321
            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
   322
                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
   323
                    '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
   324
            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
   325
        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
   326
            # 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
   327
            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
   328
        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
   329
        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
   330
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
    def subject(self):
4230
6514a76eaa5c 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4023
diff changeset
   332
        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
   333
        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
   334
                                  entity.eid, self.user_data['login'])