cubicweb/sobjects/notification.py
author Philippe Pepiot <philippe.pepiot@logilab.fr>
Tue, 17 Mar 2020 13:29:08 +0100
branch3.27
changeset 12914 87c3562b3bae
parent 12843 000cd62eda32
permissions -rw-r--r--
[pkg] require python >= 3.4 This avoid pip pulling a version that does not run on python2 when using a python2 environment. Since we already released some 3.27 releases in pypi, I think we should release 3.27.3 and remove releases 3.27.2, 3.27.1 and 3.27.0 from pypi.
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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
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
    26
from logilab.common.registry import yes
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
8931
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    28
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
    29
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
    30
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
    31
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
    32
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
class RecipientsFinder(Component):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
    """this component is responsible to find recipients of a notification
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
    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
    38
    email addresses specified in the configuration are used
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
    """
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163
diff changeset
    40
    __regid__ = 'recipients_finder'
781
323656dd85a9 fix import, use non_final_entity instead of implements('Any')
sylvain.thenault@logilab.fr
parents: 761
diff changeset
    41
    __select__ = yes()
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1263
diff changeset
    42
    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
    43
                'X primary_email E, E address A')
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1398 1410
diff changeset
    44
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
    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
    46
        mode = self._cw.vreg.config['default-recipients-mode']
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
        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
    48
            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
    49
            dests = list(execute(self.user_rql, build_descr=True).entities())
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
        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
    51
            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
    52
            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
    53
        else:  # mode == 'none'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    54
            dests = []
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
        return dests
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1398 1410
diff changeset
    57
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
# abstract or deactivated notification views and mixin ########################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
8931
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    60
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    61
class SkipEmail(Exception):
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    62
    """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
    63
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    64
8932
4ae4242bceb1 [notification] merge BaseNotificationView and NotificationView
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8931
diff changeset
    65
class NotificationView(EntityView):
8931
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    66
    """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
    67
    notification)
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    68
    """
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    69
    # 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
    70
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    71
    msgid_timestamp = True
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
    # to be defined on concrete sub-classes
12046
9056a41d91ba Fix flake8 some errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12043
diff changeset
    74
    content = None  # body of the mail
9056a41d91ba Fix flake8 some errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12043
diff changeset
    75
    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
    76
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    77
    # 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
    78
    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
    79
        """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
    80
        # 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
    81
        # after we commit our transaction.
df34de44f40f [notification] don't block while sending mails
Julien Cristau <julien.cristau@logilab.fr>
parents: 9826
diff changeset
    82
        cnx = self._cw
df34de44f40f [notification] don't block while sending mails
Julien Cristau <julien.cristau@logilab.fr>
parents: 9826
diff changeset
    83
        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
    84
            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
    85
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    86
    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
    87
        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
    88
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    89
    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
    90
        """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
    91
        self._kwargs = kwargs
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    92
        recipients = self.recipients()
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    93
        if not recipients:
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    94
            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
    95
            return
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
    96
        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
    97
            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
    98
            # 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
    99
            # previous email
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   100
            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
   101
                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
   102
                        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
   103
            else:
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   104
                refs = ()
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   105
            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
   106
        else:
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   107
            refs = ()
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   108
            msgid = None
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   109
        req = self._cw
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   110
        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
   111
        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
   112
            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
   113
                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
   114
                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
   115
            else:
8931
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   116
                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
   117
                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
   118
            # 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
   119
            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
   120
                self._cw = cnx
9305
f7a738afc295 [notification] avoid leaking cnxsets (closes #3243810)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9006
diff changeset
   121
                try:
9765
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   122
                    # 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
   123
                    # need a fresh stream at each iteration, reset it explicitly
12843
000cd62eda32 [sobjects/notification] fix broken email notifications
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 12567
diff changeset
   124
                    self._w = None
9765
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   125
                    try:
9770
112c884b2d8d merge 3.18.5 into 3.19 branch
Julien Cristau <julien.cristau@logilab.fr>
parents: 9765 9699
diff changeset
   126
                        # 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
   127
                        #     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
   128
                        #     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
   129
                        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
   130
                        subject = self.subject()
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   131
                    except SkipEmail:
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   132
                        continue
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   133
                    except Exception as ex:
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   134
                        # 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
   135
                        # 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
   136
                        # 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
   137
                        self.exception(str(ex))
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   138
                        continue
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   139
                    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
   140
                                      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
   141
                    yield msg, [emailaddr]
9765
5607ef9ab9f5 [sobjects] update notifications system to 3.19 api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9481
diff changeset
   142
                finally:
9305
f7a738afc295 [notification] avoid leaking cnxsets (closes #3243810)
Julien Cristau <julien.cristau@logilab.fr>
parents: 9006
diff changeset
   143
                    self._cw = req
8931
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   144
9826
7c17659c9eae [notification] drop on commit sending of message feature of NotificationView
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9770
diff changeset
   145
    # recipients handling ######################################################
8931
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   146
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   147
    def recipients(self):
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   148
        """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
   149
        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
   150
        """
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   151
        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
   152
            '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
   153
            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
   154
        return finder.recipients()
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   155
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   156
    # email generation helpers #################################################
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
    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
   159
        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
   160
                                    self.msgid_timestamp)
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 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
   163
        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
   164
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   165
    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
   166
        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
   167
            '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
   168
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   169
    def subject(self):
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   170
        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
   171
        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
   172
        etype = entity.dc_type()
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   173
        eid = entity.eid
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   174
        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
   175
        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
   176
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   177
    def context(self, **kwargs):
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   178
        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
   179
        for key, val in kwargs.items():
12567
26744ad37953 Drop python2 support
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12046
diff changeset
   180
            if val and isinstance(val, str) and val.strip():
12046
9056a41d91ba Fix flake8 some errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12043
diff changeset
   181
                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
   182
        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
   183
                       'eid': entity.eid,
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   184
                       '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
   185
                       'url': entity.absolute_url(),
12046
9056a41d91ba Fix flake8 some errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 12043
diff changeset
   186
                       '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
   187
        return kwargs
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   188
4b195bd82e8b [notification] move notification view in ``sobject.notification``
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8897
diff changeset
   189
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   190
class StatusChangeMixIn(object):
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163
diff changeset
   191
    __regid__ = 'notif_status_change'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   192
    msgid_timestamp = True
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   193
    message = _('status changed')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   194
    content = _("""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   195
%(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
   196
'%(title)s'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   197
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   198
%(comment)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
url: %(url)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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   203
738
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   204
###############################################################################
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   205
# Actual notification views.                                                  #
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
# 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
   208
###############################################################################
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   209
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   210
# 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
   211
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   212
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
   213
    """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
   214
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
    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
   216
    * 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
   217
    * 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
   218
      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
   219
    """
738
9b8cb1976992 better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents: 730
diff changeset
   220
    __abstract__ = True
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3163
diff changeset
   221
    __regid__ = 'notif_after_add_entity'
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   222
    msgid_timestamp = False
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   223
    message = _('new')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   224
    content = """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   225
%(title)s
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   226
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   227
%(content)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
url: %(url)s
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   230
"""
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
   231
    # 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
   232
    content_attr = None
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   233
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   234
    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
   235
        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
   236
        content = entity.printable_value(self.content_attr, format='text/plain')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   237
        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
   238
            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
   239
                                    '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
   240
            # 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
   241
            # #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
   242
            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
   243
                content = normalize_text(content, 80)
761
sylvain.thenault@logilab.fr
parents: 738
diff changeset
   244
        return super(ContentAddedView, self).context(content=content, **kwargs)
1477
b056a49c16dc backport default branch
sylvain.thenault@logilab.fr
parents: 1398 1410
diff changeset
   245
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   246
    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
   247
        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
   248
        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
   249
                                 entity.eid, self.user_data['login'])
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   250
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
   251
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   252
def format_value(value):
12567
26744ad37953 Drop python2 support
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 12046
diff changeset
   253
    if isinstance(value, str):
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
   254
        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
   255
    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
   256
3536
f6c9a5df80fb backport stable branch
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3418 3525
diff changeset
   257
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
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
   259
    """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
   260
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
    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
   262
    * 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
   263
    * 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
   264
      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
   265
    """
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
    __abstract__ = True
4926
626d31035662 3.6 want __regid__, not id
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4719
diff changeset
   267
    __regid__ = 'notif_entity_updated'
10261
b2f7f03f10b3 [notifications] use non-fixed message-id for EntityUpdatedNotificationView
Julien Cristau <julien.cristau@logilab.fr>
parents: 9826
diff changeset
   268
    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
   269
    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
   270
    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
   271
    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
   272
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
   273
2dc3908f667f [notification] add operation responsible for sending email notification when an entity is updated
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3110
diff changeset
   274
%(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
   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
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
   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
9006
e4ea8f9ffa11 notification: use viewargs for notif_entity_updated instead of transaction_data
Julien Cristau <julien.cristau@logilab.fr>
parents: 9005
diff changeset
   279
    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
   280
        context = super(EntityUpdatedNotificationView, self).context(**kwargs)
4230
6514a76eaa5c 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4023
diff changeset
   281
        _ = 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
   282
        formatted_changes = []
4230
6514a76eaa5c 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4023
diff changeset
   283
        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
   284
        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
   285
            # 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
   286
            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
   287
            if rschema.final:
3877
7ca53fc72a0a reldefsecurity branch :
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
   288
                rdef = entity.e_schema.rdef(rschema)
4230
6514a76eaa5c 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4023
diff changeset
   289
                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
   290
                    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
   291
            # 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
   292
            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
   293
                                      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
   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
            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
   296
                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
   297
            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
   298
                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
   299
                    '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
   300
                    '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
   301
                    '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
   302
            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
   303
                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
   304
                    '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
   305
            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
   306
        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
   307
            # 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
   308
            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
   309
        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
   310
        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
   311
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
    def subject(self):
4230
6514a76eaa5c 3.6 api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4023
diff changeset
   313
        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
   314
        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
   315
                                 entity.eid, self.user_data['login'])