author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Wed, 23 Sep 2009 12:29:31 +0200 | |
changeset 3419 | 32cfd0844243 |
parent 3418 | 7b49fa7e942d |
child 3536 | f6c9a5df80fb |
permissions | -rw-r--r-- |
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2818
diff
changeset
|
1 |
"""some views to handle notification on data changes |
0 | 2 |
|
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1723
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1723
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
2101
08003e0354a7
update transaction data api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
9 |
_ = unicode |
0 | 10 |
|
11 |
from itertools import repeat |
|
12 |
||
13 |
from logilab.common.textutils import normalize_text |
|
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
|
14 |
from logilab.common.deprecation import class_renamed, deprecated |
0 | 15 |
|
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2818
diff
changeset
|
16 |
from cubicweb.selectors import yes |
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
|
17 |
from cubicweb.view import Component |
0 | 18 |
from cubicweb.common.mail import format_mail |
2880 | 19 |
from cubicweb.common.mail import NotificationView |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
20 |
from cubicweb.server.hook import SendMailOp |
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
|
21 |
|
0 | 22 |
|
23 |
class RecipientsFinder(Component): |
|
24 |
"""this component is responsible to find recipients of a notification |
|
25 |
||
26 |
by default user's with their email set are notified if any, else the default |
|
27 |
email addresses specified in the configuration are used |
|
28 |
""" |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
29 |
__regid__ = 'recipients_finder' |
781
323656dd85a9
fix import, use non_final_entity instead of implements('Any')
sylvain.thenault@logilab.fr
parents:
761
diff
changeset
|
30 |
__select__ = yes() |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
31 |
user_rql = ('Any X,E,A WHERE X is CWUser, X in_state S, S name "activated",' |
0 | 32 |
'X primary_email E, E address A') |
1477 | 33 |
|
0 | 34 |
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
|
35 |
mode = self._cw.vreg.config['default-recipients-mode'] |
0 | 36 |
if mode == 'users': |
37 |
# use unsafe execute else we may don't have the right to see users |
|
38 |
# to notify... |
|
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
39 |
execute = self._cw.unsafe_execute |
0 | 40 |
dests = [(u.get_email(), u.property_value('ui.language')) |
41 |
for u in execute(self.user_rql, build_descr=True, propagate=True).entities()] |
|
42 |
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
|
43 |
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
|
44 |
dests = zip(self._cw.vreg.config['default-dest-addrs'], repeat(lang)) |
0 | 45 |
else: # mode == 'none' |
46 |
dests = [] |
|
47 |
return dests |
|
48 |
||
1477 | 49 |
|
0 | 50 |
# abstract or deactivated notification views and mixin ######################## |
51 |
||
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
|
52 |
class NotificationView(NotificationView): |
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
|
53 |
"""overriden to delay actual sending of mails to a commit operation by |
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
54 |
default |
0 | 55 |
""" |
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
|
56 |
def send_on_commit(self, recipients, msg): |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
57 |
SendMailOp(self._cw, recipients=recipients, msg=msg) |
2879
ae26a80c0635
move base NotificationView to cw.common.mail, we may want to use it to send notification from the web ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
58 |
send = send_on_commit |
0 | 59 |
|
60 |
||
61 |
class StatusChangeMixIn(object): |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
62 |
__regid__ = 'notif_status_change' |
0 | 63 |
msgid_timestamp = True |
64 |
message = _('status changed') |
|
65 |
content = _(""" |
|
66 |
%(user)s changed status from <%(previous_state)s> to <%(current_state)s> for entity |
|
67 |
'%(title)s' |
|
68 |
||
69 |
%(comment)s |
|
70 |
||
71 |
url: %(url)s |
|
72 |
""") |
|
73 |
||
74 |
||
738
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
75 |
############################################################################### |
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
76 |
# Actual notification views. # |
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
77 |
# # |
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
78 |
# 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
|
79 |
############################################################################### |
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
80 |
|
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
81 |
# 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
|
82 |
|
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
83 |
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
|
84 |
"""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
|
85 |
|
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
|
86 |
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
|
87 |
* 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
|
88 |
* 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
|
89 |
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
|
90 |
""" |
738
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
91 |
__abstract__ = True |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3163
diff
changeset
|
92 |
__regid__ = 'notif_after_add_entity' |
0 | 93 |
msgid_timestamp = False |
94 |
message = _('new') |
|
95 |
content = """ |
|
96 |
%(title)s |
|
97 |
||
98 |
%(content)s |
|
99 |
||
100 |
url: %(url)s |
|
101 |
""" |
|
102 |
||
103 |
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
|
104 |
entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0) |
0 | 105 |
content = entity.printable_value(self.content_attr, format='text/plain') |
106 |
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
|
107 |
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
|
108 |
'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
|
109 |
# 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
|
110 |
# #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
|
111 |
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
|
112 |
content = normalize_text(content, 80) |
761 | 113 |
return super(ContentAddedView, self).context(content=content, **kwargs) |
1477 | 114 |
|
0 | 115 |
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
|
116 |
entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0) |
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
117 |
return u'%s #%s (%s)' % (self._cw.__('New %s' % entity.e_schema), |
3110
757d36162235
enhance notification mecanism: recipients may return user entities, which will be used to create a fake session so one can check security during notification if necessary
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3051
diff
changeset
|
118 |
entity.eid, self.user_data['login']) |
0 | 119 |
|
2880 | 120 |
|
121 |
from logilab.common.deprecation import class_renamed, class_moved, deprecated |
|
122 |
from cubicweb.hooks.notification import RenderAndSendNotificationView |
|
123 |
from cubicweb.common.mail import parse_message_id |
|
124 |
||
738
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
125 |
NormalizedTextView = class_renamed('NormalizedTextView', ContentAddedView) |
2841
107ba1c45227
rewrite hooks in sobjects as new Hook style into hooks sub-package
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2818
diff
changeset
|
126 |
RenderAndSendNotificationView = class_moved(RenderAndSendNotificationView) |
3083
3084bc9ccc64
wrong merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3072
diff
changeset
|
127 |
parse_message_id = deprecated('parse_message_id is now defined in cubicweb.common.mail')(parse_message_id) |
3084bc9ccc64
wrong merge
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3072
diff
changeset
|
128 |