23 from itertools import repeat |
23 from itertools import repeat |
24 |
24 |
25 from six import text_type |
25 from six import text_type |
26 |
26 |
27 from logilab.common.textutils import normalize_text |
27 from logilab.common.textutils import normalize_text |
28 from logilab.common.deprecation import class_renamed, class_moved, deprecated |
|
29 from logilab.common.registry import yes |
28 from logilab.common.registry import yes |
30 |
29 |
31 from cubicweb.entity import Entity |
|
32 from cubicweb.view import Component, EntityView |
30 from cubicweb.view import Component, EntityView |
33 from cubicweb.server.hook import SendMailOp |
31 from cubicweb.server.hook import SendMailOp |
34 from cubicweb.mail import construct_message_id, format_mail |
32 from cubicweb.mail import construct_message_id, format_mail |
35 from cubicweb.server.session import Connection, InternalManager |
33 from cubicweb.server.session import Connection, InternalManager |
36 |
34 |
52 execute = self._cw.execute |
50 execute = self._cw.execute |
53 dests = list(execute(self.user_rql, build_descr=True).entities()) |
51 dests = list(execute(self.user_rql, build_descr=True).entities()) |
54 elif mode == 'default-dest-addrs': |
52 elif mode == 'default-dest-addrs': |
55 lang = self._cw.vreg.property_value('ui.language') |
53 lang = self._cw.vreg.property_value('ui.language') |
56 dests = zip(self._cw.vreg.config['default-dest-addrs'], repeat(lang)) |
54 dests = zip(self._cw.vreg.config['default-dest-addrs'], repeat(lang)) |
57 else: # mode == 'none' |
55 else: # mode == 'none' |
58 dests = [] |
56 dests = [] |
59 return dests |
57 return dests |
60 |
58 |
61 |
59 |
62 # abstract or deactivated notification views and mixin ######################## |
60 # abstract or deactivated notification views and mixin ######################## |
73 # XXX refactor this class to work with len(rset) > 1 |
71 # XXX refactor this class to work with len(rset) > 1 |
74 |
72 |
75 msgid_timestamp = True |
73 msgid_timestamp = True |
76 |
74 |
77 # to be defined on concrete sub-classes |
75 # to be defined on concrete sub-classes |
78 content = None # body of the mail |
76 content = None # body of the mail |
79 message = None # action verb of the subject |
77 message = None # action verb of the subject |
80 |
78 |
81 # this is usually the method to call |
79 # this is usually the method to call |
82 def render_and_send(self, **kwargs): |
80 def render_and_send(self, **kwargs): |
83 """generate and send email messages for this view""" |
81 """generate and send email messages for this view""" |
84 # render_emails changes self._cw so cache it here so all mails are sent |
82 # render_emails changes self._cw so cache it here so all mails are sent |
166 def format_field(self, attr, value): |
164 def format_field(self, attr, value): |
167 return ':%(attr)s: %(value)s' % {'attr': attr, 'value': value} |
165 return ':%(attr)s: %(value)s' % {'attr': attr, 'value': value} |
168 |
166 |
169 def format_section(self, attr, value): |
167 def format_section(self, attr, value): |
170 return '%(attr)s\n%(ul)s\n%(value)s\n' % { |
168 return '%(attr)s\n%(ul)s\n%(value)s\n' % { |
171 'attr': attr, 'ul': '-'*len(attr), 'value': value} |
169 'attr': attr, 'ul': '-' * len(attr), 'value': value} |
172 |
170 |
173 def subject(self): |
171 def subject(self): |
174 entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0) |
172 entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0) |
175 subject = self._cw._(self.message) |
173 subject = self._cw._(self.message) |
176 etype = entity.dc_type() |
174 etype = entity.dc_type() |
180 |
178 |
181 def context(self, **kwargs): |
179 def context(self, **kwargs): |
182 entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0) |
180 entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0) |
183 for key, val in kwargs.items(): |
181 for key, val in kwargs.items(): |
184 if val and isinstance(val, text_type) and val.strip(): |
182 if val and isinstance(val, text_type) and val.strip(): |
185 kwargs[key] = self._cw._(val) |
183 kwargs[key] = self._cw._(val) |
186 kwargs.update({'user': self.user_data['login'], |
184 kwargs.update({'user': self.user_data['login'], |
187 'eid': entity.eid, |
185 'eid': entity.eid, |
188 'etype': entity.dc_type(), |
186 'etype': entity.dc_type(), |
189 'url': entity.absolute_url(), |
187 'url': entity.absolute_url(), |
190 'title': entity.dc_long_title(),}) |
188 'title': entity.dc_long_title()}) |
191 return kwargs |
189 return kwargs |
192 |
190 |
193 |
191 |
194 class StatusChangeMixIn(object): |
192 class StatusChangeMixIn(object): |
195 __regid__ = 'notif_status_change' |
193 __regid__ = 'notif_status_change' |
247 content = normalize_text(content, 80) |
245 content = normalize_text(content, 80) |
248 return super(ContentAddedView, self).context(content=content, **kwargs) |
246 return super(ContentAddedView, self).context(content=content, **kwargs) |
249 |
247 |
250 def subject(self): |
248 def subject(self): |
251 entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0) |
249 entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0) |
252 return u'%s #%s (%s)' % (self._cw.__('New %s' % entity.e_schema), |
250 return u'%s #%s (%s)' % (self._cw.__('New %s' % entity.e_schema), |
253 entity.eid, self.user_data['login']) |
251 entity.eid, self.user_data['login']) |
254 |
252 |
255 |
253 |
256 def format_value(value): |
254 def format_value(value): |
257 if isinstance(value, text_type): |
255 if isinstance(value, text_type): |
258 return u'"%s"' % value |
256 return u'"%s"' % value |
313 context['changes'] = '\n'.join(formatted_changes) |
311 context['changes'] = '\n'.join(formatted_changes) |
314 return context |
312 return context |
315 |
313 |
316 def subject(self): |
314 def subject(self): |
317 entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0) |
315 entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0) |
318 return u'%s #%s (%s)' % (self._cw.__('Updated %s' % entity.e_schema), |
316 return u'%s #%s (%s)' % (self._cw.__('Updated %s' % entity.e_schema), |
319 entity.eid, self.user_data['login']) |
317 entity.eid, self.user_data['login']) |