cubicweb/sobjects/notification.py
author Guillaume Vandevelde <gvandevelde@logilab.fr>
Thu, 13 Feb 2020 13:56:12 +0100
branch3.26
changeset 12876 fe9bd0e937e4
parent 12046 9056a41d91ba
child 12567 26744ad37953
permissions -rw-r--r--
[pkg] version 3.26.15
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
8190
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
    28
from logilab.common.registry import yes
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
8931
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
12043
b8d2e6b9f548 Stop using Session on the repository side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11913
diff changeset
    33
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
    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
11143
ebb6809659a4 [notification] avoid using InternalManager when we've got a CWUser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11057
diff changeset
    51
            dests = list(execute(self.user_rql, build_descr=True).entities())
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    52
        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
    53
            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
    54
            dests = zip(self._cw.vreg.config['default-dest-addrs'], repeat(lang))
12046
9056a41d91ba Fix flake8 some errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12043
diff changeset
    55
        else:  # mode == 'none'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
            dests = []
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
        return dests
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1398 1410
diff changeset
    59
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
# abstract or deactivated notification views and mixin ########################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
8931
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    62
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    63
class SkipEmail(Exception):
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    64
    """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
    65
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    66
8932
4ae4242bceb1 [notification] merge BaseNotificationView and NotificationView
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8931
diff changeset
    67
class NotificationView(EntityView):
8931
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    68
    """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
    69
    notification)
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    70
    """
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    71
    # 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
    72
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    73
    msgid_timestamp = True
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
    # to be defined on concrete sub-classes
12046
9056a41d91ba Fix flake8 some errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12043
diff changeset
    76
    content = None  # body of the mail
9056a41d91ba Fix flake8 some errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12043
diff changeset
    77
    message = None  # action verb of the subject
8931
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    78
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    79
    # 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
    80
    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
    81
        """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
    82
        # 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
    83
        # after we commit our transaction.
df34de44f40f [notification] don't block while sending mails
Julien Cristau <julien.cristau@logilab.fr>
parents: 9826
diff changeset
    84
        cnx = self._cw
df34de44f40f [notification] don't block while sending mails
Julien Cristau <julien.cristau@logilab.fr>
parents: 9826
diff changeset
    85
        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
    86
            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
    87
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    88
    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
    89
        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
    90
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    91
    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
    92
        """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
    93
        self._kwargs = kwargs
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    94
        recipients = self.recipients()
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    95
        if not recipients:
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    96
            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
    97
            return
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    98
        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
    99
            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
   100
            # 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
   101
            # previous email
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   102
            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
   103
                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
   104
                        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
   105
            else:
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   106
                refs = ()
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   107
            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
   108
        else:
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   109
            refs = ()
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   110
            msgid = None
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   111
        req = self._cw
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   112
        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
   113
        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
   114
            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
   115
                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
   116
                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
   117
            else:
8931
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   118
                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
   119
                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
   120
            # 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
   121
            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
   122
                self._cw = cnx
9305
f7a738afc295 [notification] avoid leaking cnxsets (closes #3243810)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9006
diff changeset
   123
                try:
9765
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   124
                    # 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
   125
                    # 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
   126
                    self.w = None
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   127
                    try:
9770
112c884b2d8d merge 3.18.5 into 3.19 branch
Julien Cristau <julien.cristau@logilab.fr>
parents: 9765 9699
diff changeset
   128
                        # 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
   129
                        #     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
   130
                        #     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
   131
                        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
   132
                        subject = self.subject()
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   133
                    except SkipEmail:
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   134
                        continue
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   135
                    except Exception as ex:
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   136
                        # 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
   137
                        # 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
   138
                        # 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
   139
                        self.exception(str(ex))
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   140
                        continue
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   141
                    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
   142
                                      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
   143
                    yield msg, [emailaddr]
9765
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   144
                finally:
9305
f7a738afc295 [notification] avoid leaking cnxsets (closes #3243810)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9006
diff changeset
   145
                    self._cw = req
8931
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   146
9826
7c17659c9eae [notification] drop on commit sending of message feature of NotificationView
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9770
diff changeset
   147
    # recipients handling ######################################################
8931
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   148
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   149
    def recipients(self):
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   150
        """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
   151
        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
   152
        """
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   153
        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
   154
            '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
   155
            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
   156
        return finder.recipients()
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   157
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   158
    # email generation helpers #################################################
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
    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
   161
        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
   162
                                    self.msgid_timestamp)
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   163
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   164
    def 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
   165
        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
   166
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   167
    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
   168
        return '%(attr)s\n%(ul)s\n%(value)s\n' % {
12046
9056a41d91ba Fix flake8 some errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12043
diff changeset
   169
            'attr': attr, 'ul': '-' * len(attr), 'value': value}
8931
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 subject(self):
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   172
        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
   173
        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
   174
        etype = entity.dc_type()
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   175
        eid = entity.eid
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   176
        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
   177
        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
   178
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   179
    def context(self, **kwargs):
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   180
        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
   181
        for key, val in kwargs.items():
10702
f94c812c3669 [py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10666
diff changeset
   182
            if val and isinstance(val, text_type) and val.strip():
12046
9056a41d91ba Fix flake8 some errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12043
diff changeset
   183
                kwargs[key] = self._cw._(val)
8931
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   184
        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
   185
                       'eid': entity.eid,
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   186
                       '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
   187
                       'url': entity.absolute_url(),
12046
9056a41d91ba Fix flake8 some errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12043
diff changeset
   188
                       'title': entity.dc_long_title()})
8931
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   189
        return kwargs
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   190
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   191
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   192
class StatusChangeMixIn(object):
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163
diff changeset
   193
    __regid__ = 'notif_status_change'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   194
    msgid_timestamp = True
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   195
    message = _('status changed')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   196
    content = _("""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   197
%(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
   198
'%(title)s'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   199
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   200
%(comment)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
url: %(url)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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   205
738
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   206
###############################################################################
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   207
# Actual notification views.                                                  #
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
# 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
   210
###############################################################################
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   211
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   212
# 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
   213
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   214
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
   215
    """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
   216
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
    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
   218
    * 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
   219
    * 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
   220
      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
   221
    """
738
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   222
    __abstract__ = True
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163
diff changeset
   223
    __regid__ = 'notif_after_add_entity'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   224
    msgid_timestamp = False
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   225
    message = _('new')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   226
    content = """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   227
%(title)s
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   228
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   229
%(content)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
url: %(url)s
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   232
"""
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
   233
    # 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
   234
    content_attr = None
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   235
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   236
    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
   237
        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
   238
        content = entity.printable_value(self.content_attr, format='text/plain')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   239
        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
   240
            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
   241
                                    '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
   242
            # 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
   243
            # #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
   244
            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
   245
                content = normalize_text(content, 80)
761
sylvain.thenault@logilab.fr
parents: 738
diff changeset
   246
        return super(ContentAddedView, self).context(content=content, **kwargs)
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1398 1410
diff changeset
   247
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   248
    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
   249
        entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0)
12046
9056a41d91ba Fix flake8 some errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12043
diff changeset
   250
        return u'%s #%s (%s)' % (self._cw.__('New %s' % entity.e_schema),
9056a41d91ba Fix flake8 some errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12043
diff changeset
   251
                                 entity.eid, self.user_data['login'])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   252
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
   253
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   254
def format_value(value):
10702
f94c812c3669 [py3k] unicode → six.text_type
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10666
diff changeset
   255
    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
   256
        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
   257
    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
   258
3536
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3418 3525
diff changeset
   259
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
   260
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
   261
    """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
   262
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
    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
   264
    * 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
   265
    * 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
   266
      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
   267
    """
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
    __abstract__ = True
4926
626d31035662 3.6 want __regid__, not id
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   269
    __regid__ = 'notif_entity_updated'
10261
b2f7f03f10b3 [notifications] use non-fixed message-id for EntityUpdatedNotificationView
Julien Cristau <julien.cristau@logilab.fr>
parents: 9826
diff changeset
   270
    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
   271
    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
   272
    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
   273
    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
   274
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
   275
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
%(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
   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
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
   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
9006
e4ea8f9ffa11 notification: use viewargs for notif_entity_updated instead of transaction_data
Julien Cristau <julien.cristau@logilab.fr>
parents: 9005
diff changeset
   281
    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
   282
        context = super(EntityUpdatedNotificationView, self).context(**kwargs)
4230
6514a76eaa5c 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4023
diff changeset
   283
        _ = 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
   284
        formatted_changes = []
4230
6514a76eaa5c 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4023
diff changeset
   285
        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
   286
        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
   287
            # 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
   288
            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
   289
            if rschema.final:
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   290
                rdef = entity.e_schema.rdef(rschema)
4230
6514a76eaa5c 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4023
diff changeset
   291
                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
   292
                    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
   293
            # 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
   294
            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
   295
                                      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
   296
                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
   297
            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
   298
                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
   299
            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
   300
                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
   301
                    '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
   302
                    '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
   303
                    '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
   304
            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
   305
                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
   306
                    '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
   307
            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
   308
        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
   309
            # 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
   310
            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
   311
        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
   312
        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
   313
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
    def subject(self):
4230
6514a76eaa5c 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4023
diff changeset
   315
        entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0)
12046
9056a41d91ba Fix flake8 some errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12043
diff changeset
   316
        return u'%s #%s (%s)' % (self._cw.__('Updated %s' % entity.e_schema),
9056a41d91ba Fix flake8 some errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12043
diff changeset
   317
                                 entity.eid, self.user_data['login'])