cubicweb/sobjects/notification.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 14 Mar 2017 11:07:58 +0100
changeset 12043 b8d2e6b9f548
parent 11913 4516c3956d46
child 12046 9056a41d91ba
permissions -rw-r--r--
Stop using Session on the repository side Only expect session on web request, and let the web session/authentication managers provide them. Access to cnx.data, which used to return session data, is deprecated: there is no more access to session data from the repository side, and they should be access from req.session.data from the web side.
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
11767
432f87a63057 flake8 and all
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11143
diff changeset
    20
10666
7f6b5f023884 [py3k] replace '_ = unicode' in global scope (closes #7589459)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10662
diff changeset
    21
from cubicweb import _
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
10702
f94c812c3669 [py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10666
diff changeset
    25
from six import text_type
f94c812c3669 [py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10666
diff changeset
    26
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
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
    28
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
    29
from logilab.common.registry import yes
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
8931
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    31
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
    32
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
    33
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
    34
from cubicweb.mail import construct_message_id, format_mail
12043
b8d2e6b9f548 Stop using Session on the repository side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11913
diff changeset
    35
from cubicweb.server.session import Connection, 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
    36
0
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
class RecipientsFinder(Component):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
    """this component is responsible to find recipients of a notification
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
    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
    42
    email addresses specified in the configuration are used
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
    """
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163
diff changeset
    44
    __regid__ = 'recipients_finder'
781
323656dd85a9 fix import, use non_final_entity instead of implements('Any')
sylvain.thenault@logilab.fr
parents: 761
diff changeset
    45
    __select__ = yes()
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
    46
    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
    47
                'X primary_email E, E address A')
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1398 1410
diff changeset
    48
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
    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
    50
        mode = self._cw.vreg.config['default-recipients-mode']
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
        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
    52
            execute = self._cw.execute
11143
ebb6809659a4 [notification] avoid using InternalManager when we've got a CWUser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11057
diff changeset
    53
            dests = list(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):
9826
7c17659c9eae [notification] drop on commit sending of message feature of NotificationView
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9770
diff changeset
    83
        """generate and send email messages for this view"""
10296
df34de44f40f [notification] don't block while sending mails
Julien Cristau <julien.cristau@logilab.fr>
parents: 9826
diff changeset
    84
        # render_emails changes self._cw so cache it here so all mails are sent
df34de44f40f [notification] don't block while sending mails
Julien Cristau <julien.cristau@logilab.fr>
parents: 9826
diff changeset
    85
        # after we commit our transaction.
df34de44f40f [notification] don't block while sending mails
Julien Cristau <julien.cristau@logilab.fr>
parents: 9826
diff changeset
    86
        cnx = self._cw
df34de44f40f [notification] don't block while sending mails
Julien Cristau <julien.cristau@logilab.fr>
parents: 9826
diff changeset
    87
        for msg, recipients in self.render_emails(**kwargs):
df34de44f40f [notification] don't block while sending mails
Julien Cristau <julien.cristau@logilab.fr>
parents: 9826
diff changeset
    88
            SendMailOp(cnx, recipients=recipients, msg=msg)
8931
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    89
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    90
    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
    91
        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
    92
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    93
    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
    94
        """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
    95
        self._kwargs = kwargs
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    96
        recipients = self.recipients()
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    97
        if not recipients:
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    98
            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
    99
            return
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   100
        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
   101
            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
   102
            # 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
   103
            # previous email
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   104
            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
   105
                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
   106
                        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
   107
            else:
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   108
                refs = ()
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   109
            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
   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 = None
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   113
        req = self._cw
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   114
        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
   115
        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
   116
            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
   117
                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
   118
                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
   119
            else:
8931
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   120
                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
   121
                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
   122
            # hi-jack self._cw to get a session for the returned user
12043
b8d2e6b9f548 Stop using Session on the repository side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11913
diff changeset
   123
            with Connection(self._cw.repo, user) as cnx:
9765
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   124
                self._cw = cnx
9305
f7a738afc295 [notification] avoid leaking cnxsets (closes #3243810)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9006
diff changeset
   125
                try:
9765
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   126
                    # 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
   127
                    # 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
   128
                    self.w = None
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   129
                    try:
9770
112c884b2d8d merge 3.18.5 into 3.19 branch
Julien Cristau <julien.cristau@logilab.fr>
parents: 9765 9699
diff changeset
   130
                        # XXX forcing the row & col here may make the content and
112c884b2d8d merge 3.18.5 into 3.19 branch
Julien Cristau <julien.cristau@logilab.fr>
parents: 9765 9699
diff changeset
   131
                        #     subject inconsistent because subject will depend on
112c884b2d8d merge 3.18.5 into 3.19 branch
Julien Cristau <julien.cristau@logilab.fr>
parents: 9765 9699
diff changeset
   132
                        #     self.cw_row & self.cw_col if they are set.
9765
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   133
                        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
   134
                        subject = self.subject()
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   135
                    except SkipEmail:
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   136
                        continue
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   137
                    except Exception as ex:
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   138
                        # 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
   139
                        # 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
   140
                        # 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
   141
                        self.exception(str(ex))
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   142
                        continue
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   143
                    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
   144
                                      config=self._cw.vreg.config, msgid=msgid, references=refs)
9826
7c17659c9eae [notification] drop on commit sending of message feature of NotificationView
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9770
diff changeset
   145
                    yield msg, [emailaddr]
9765
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   146
                finally:
9305
f7a738afc295 [notification] avoid leaking cnxsets (closes #3243810)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9006
diff changeset
   147
                    self._cw = req
8931
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   148
9826
7c17659c9eae [notification] drop on commit sending of message feature of NotificationView
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9770
diff changeset
   149
    # recipients handling ######################################################
8931
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   150
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   151
    def recipients(self):
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   152
        """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
   153
        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
   154
        """
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   155
        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
   156
            '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
   157
            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
   158
        return finder.recipients()
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   159
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   160
    # email generation helpers #################################################
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   161
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   162
    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
   163
        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
   164
                                    self.msgid_timestamp)
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   165
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   166
    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
   167
        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
   168
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   169
    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
   170
        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
   171
            '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
   172
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   173
    def subject(self):
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   174
        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
   175
        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
   176
        etype = entity.dc_type()
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   177
        eid = entity.eid
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   178
        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
   179
        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
   180
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   181
    def context(self, **kwargs):
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   182
        entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0)
10662
10942ed172de [py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10301
diff changeset
   183
        for key, val in kwargs.items():
10702
f94c812c3669 [py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10666
diff changeset
   184
            if val and isinstance(val, text_type) and val.strip():
8931
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   185
               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
   186
        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
   187
                       'eid': entity.eid,
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   188
                       'etype': entity.dc_type(),
11913
4516c3956d46 Drop support for https-url in all-in-one.conf
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11767
diff changeset
   189
                       'url': entity.absolute_url(),
8931
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   190
                       '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
   191
        return kwargs
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   192
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   193
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   194
class StatusChangeMixIn(object):
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163
diff changeset
   195
    __regid__ = 'notif_status_change'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   196
    msgid_timestamp = True
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   197
    message = _('status changed')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   198
    content = _("""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   199
%(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
   200
'%(title)s'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   201
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   202
%(comment)s
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   203
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   204
url: %(url)s
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   205
""")
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   206
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   207
738
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   208
###############################################################################
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   209
# Actual notification views.                                                  #
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   210
#                                                                             #
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   211
# 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
   212
###############################################################################
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   213
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   214
# 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
   215
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   216
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
   217
    """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
   218
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
   219
    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
   220
    * 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
   221
    * 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
   222
      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
   223
    """
738
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   224
    __abstract__ = True
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163
diff changeset
   225
    __regid__ = 'notif_after_add_entity'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   226
    msgid_timestamp = False
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   227
    message = _('new')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   228
    content = """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   229
%(title)s
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   230
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   231
%(content)s
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   232
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   233
url: %(url)s
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   234
"""
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
   235
    # 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
   236
    content_attr = None
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   237
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   238
    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
   239
        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
   240
        content = entity.printable_value(self.content_attr, format='text/plain')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   241
        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
   242
            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
   243
                                    '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
   244
            # 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
   245
            # #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
   246
            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
   247
                content = normalize_text(content, 80)
761
sylvain.thenault@logilab.fr
parents: 738
diff changeset
   248
        return super(ContentAddedView, self).context(content=content, **kwargs)
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1398 1410
diff changeset
   249
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   250
    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
   251
        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
   252
        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
   253
                                  entity.eid, self.user_data['login'])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   254
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
   255
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   256
def format_value(value):
10702
f94c812c3669 [py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10666
diff changeset
   257
    if isinstance(value, text_type):
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
   258
        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
   259
    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
   260
3536
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3418 3525
diff changeset
   261
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
   262
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
   263
    """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
   264
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   265
    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
   266
    * 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
   267
    * 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
   268
      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
   269
    """
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
    __abstract__ = True
4926
626d31035662 3.6 want __regid__, not id
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   271
    __regid__ = 'notif_entity_updated'
10261
b2f7f03f10b3 [notifications] use non-fixed message-id for EntityUpdatedNotificationView
Julien Cristau <julien.cristau@logilab.fr>
parents: 9826
diff changeset
   272
    msgid_timestamp = True
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
    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
   274
    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
   275
    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
   276
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
   277
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
%(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
   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
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
   281
"""
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
9006
e4ea8f9ffa11 notification: use viewargs for notif_entity_updated instead of transaction_data
Julien Cristau <julien.cristau@logilab.fr>
parents: 9005
diff changeset
   283
    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
   284
        context = super(EntityUpdatedNotificationView, self).context(**kwargs)
4230
6514a76eaa5c 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4023
diff changeset
   285
        _ = 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
   286
        formatted_changes = []
4230
6514a76eaa5c 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4023
diff changeset
   287
        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
   288
        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
   289
            # 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
   290
            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
   291
            if rschema.final:
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   292
                rdef = entity.e_schema.rdef(rschema)
4230
6514a76eaa5c 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4023
diff changeset
   293
                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
   294
                    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
   295
            # 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
   296
            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
   297
                                      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
   298
                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
   299
            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
   300
                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
   301
            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
   302
                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
   303
                    '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
   304
                    '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
   305
                    '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
   306
            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
   307
                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
   308
                    '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
   309
            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
   310
        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
   311
            # 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
   312
            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
   313
        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
   314
        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
   315
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
    def subject(self):
4230
6514a76eaa5c 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4023
diff changeset
   317
        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
   318
        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
   319
                                  entity.eid, self.user_data['login'])