author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 18 Aug 2009 00:22:10 +0200 | |
changeset 2891 | 60afb9705035 |
parent 2880 | bfc8e1831290 |
child 3023 | 7864fee8b4ec |
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 |
0 | 20 |
from cubicweb.server.hookhelper 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 |
""" |
|
29 |
id = '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): |
2818
326375561412
propagate some api changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2789
diff
changeset
|
35 |
mode = self.req.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... |
|
39 |
execute = self.req.unsafe_execute |
|
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': |
|
43 |
lang = self.vreg.property_value('ui.language') |
|
2818
326375561412
propagate some api changes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2789
diff
changeset
|
44 |
dests = zip(self.req.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): |
0 | 57 |
SendMailOp(self.req, 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): |
|
62 |
id = 'notif_status_change' |
|
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 |
1477 | 92 |
id = '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): |
|
2789
39712da6f397
R propagate deprecation of AppObject.entity()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2650
diff
changeset
|
104 |
entity = self.rset.get_entity(self.row or 0, self.col or 0) |
0 | 105 |
content = entity.printable_value(self.content_attr, format='text/plain') |
106 |
if content: |
|
107 |
contentformat = getattr(entity, self.content_attr + '_format', 'text/rest') |
|
108 |
content = normalize_text(content, 80, rest=contentformat=='text/rest') |
|
761 | 109 |
return super(ContentAddedView, self).context(content=content, **kwargs) |
1477 | 110 |
|
0 | 111 |
def subject(self): |
2789
39712da6f397
R propagate deprecation of AppObject.entity()
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2650
diff
changeset
|
112 |
entity = self.rset.get_entity(self.row or 0, self.col or 0) |
0 | 113 |
return u'%s #%s (%s)' % (self.req.__('New %s' % entity.e_schema), |
114 |
entity.eid, self.user_login()) |
|
115 |
||
2880 | 116 |
|
117 |
from logilab.common.deprecation import class_renamed, class_moved, deprecated |
|
118 |
from cubicweb.hooks.notification import RenderAndSendNotificationView |
|
119 |
from cubicweb.common.mail import parse_message_id |
|
120 |
||
738
9b8cb1976992
better name for NormalizedTextView, drop ContentAddedMixIn
sylvain.thenault@logilab.fr
parents:
730
diff
changeset
|
121 |
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
|
122 |
RenderAndSendNotificationView = class_moved(RenderAndSendNotificationView) |
2880 | 123 |
parse_message_id = deprecated('parse_message_id is now defined in cubicweb.common.mail') |