--- a/sobjects/supervising.py Tue May 05 17:18:49 2009 +0200
+++ b/sobjects/supervising.py Thu May 14 12:48:11 2009 +0200
@@ -2,13 +2,14 @@
:organization: Logilab
-:copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
"""
__docformat__ = "restructuredtext en"
from cubicweb import UnknownEid
-from cubicweb.common.view import ComponentMixIn, StartupView
+from cubicweb.selectors import none_rset
+from cubicweb.view import Component
from cubicweb.common.mail import format_mail
from cubicweb.server.hooksmanager import Hook
from cubicweb.server.hookhelper import SendMailOp
@@ -18,7 +19,7 @@
events = ('before_add_relation', 'before_delete_relation',
'after_add_entity', 'before_update_entity')
accepts = ('Any',)
-
+
def call(self, session, *args):
dest = self.config['supervising-addrs']
if not dest: # no supervisors, don't do this for nothing...
@@ -26,24 +27,24 @@
self.session = session
if self._call(*args):
SupervisionMailOp(session)
-
+
def _call(self, *args):
- if self._event() == 'update_entity' and args[0].e_schema == 'EUser':
+ if self._event() == 'update_entity' and args[0].e_schema == 'CWUser':
updated = set(args[0].iterkeys())
if not (updated - frozenset(('eid', 'modification_date', 'last_login_time'))):
- # don't record last_login_time update which are done
+ # don't record last_login_time update which are done
# automatically at login time
return False
self.session.add_query_data('pendingchanges', (self._event(), args))
return True
-
+
def _event(self):
return self.event.split('_', 1)[1]
class EntityDeleteHook(SomethingChangedHook):
events = ('before_delete_entity',)
-
+
def _call(self, eid):
entity = self.session.entity(eid)
try:
@@ -79,19 +80,19 @@
changes.remove(change)
if entity.from_state:
try:
- changes.remove( ('delete_relation',
- (entity.wf_info_for[0].eid, 'in_state',
+ changes.remove( ('delete_relation',
+ (entity.wf_info_for[0].eid, 'in_state',
entity.from_state[0].eid)) )
except ValueError:
pass
try:
- changes.remove( ('add_relation',
- (entity.wf_info_for[0].eid, 'in_state',
+ changes.remove( ('add_relation',
+ (entity.wf_info_for[0].eid, 'in_state',
entity.to_state[0].eid)) )
except ValueError:
pass
event = 'change_state'
- change = (event,
+ change = (event,
(entity.wf_info_for[0],
entity.from_state[0], entity.to_state[0]))
changes.append(change)
@@ -114,7 +115,7 @@
# at entity creation time
elif changedescr[1] == 'in_state' and changedescr[0] in added:
index['add_relation'].remove(change)
-
+
except KeyError:
break
for eid in deleted:
@@ -137,17 +138,18 @@
yield change
-class SupervisionEmailView(ComponentMixIn, StartupView):
+class SupervisionEmailView(Component):
"""view implementing the email API for data changes supervision notification
"""
+ __select__ = none_rset()
id = 'supervision_notif'
def recipients(self):
return self.config['supervising-addrs']
-
+
def subject(self):
return self.req._('[%s supervision] changes summary') % self.config.appid
-
+
def call(self, changes):
user = self.req.actual_session().user
self.w(self.req._('user %s has made the following change(s):\n\n')
@@ -161,30 +163,30 @@
return {'eid': entity.eid,
'etype': entity.dc_type().lower(),
'title': entity.dc_title()}
-
+
def add_entity(self, entity):
msg = self.req._('added %(etype)s #%(eid)s (%(title)s)')
self.w(u'%s\n' % (msg % self._entity_context(entity)))
self.w(u' %s' % entity.absolute_url())
-
+
def update_entity(self, entity):
msg = self.req._('updated %(etype)s #%(eid)s (%(title)s)')
self.w(u'%s\n' % (msg % self._entity_context(entity)))
# XXX print changes
self.w(u' %s' % entity.absolute_url())
-
+
def delete_entity(self, eid, etype, title):
msg = self.req._('deleted %(etype)s #%(eid)s (%(title)s)')
etype = display_name(self.req, etype).lower()
self.w(msg % locals())
-
+
def change_state(self, entity, fromstate, tostate):
msg = self.req._('changed state of %(etype)s #%(eid)s (%(title)s)')
self.w(u'%s\n' % (msg % self._entity_context(entity)))
- self.w(_(' from state %(fromstate)s to state %(tostate)s\n' %
+ self.w(_(' from state %(fromstate)s to state %(tostate)s\n' %
{'fromstate': _(fromstate.name), 'tostate': _(tostate.name)}))
self.w(u' %s' % entity.absolute_url())
-
+
def _relation_context(self, fromeid, rtype, toeid):
_ = self.req._
session = self.req.actual_session()
@@ -200,7 +202,7 @@
'frometype': describe(fromeid),
'toeid': toeid,
'toetype': describe(toeid)}
-
+
def add_relation(self, fromeid, rtype, toeid):
msg = self.req._('added relation %(rtype)s from %(frometype)s #%(fromeid)s to %(toetype)s #%(toeid)s')
self.w(msg % self._relation_context(fromeid, rtype, toeid))
@@ -208,8 +210,8 @@
def delete_relation(self, fromeid, rtype, toeid):
msg = self.req._('deleted relation %(rtype)s from %(frometype)s #%(fromeid)s to %(toetype)s #%(toeid)s')
self.w(msg % self._relation_context(fromeid, rtype, toeid))
-
-
+
+
class SupervisionMailOp(SendMailOp):
"""special send email operation which should be done only once for a bunch
of changes
@@ -217,14 +219,14 @@
def _get_view(self):
return self.session.vreg.select_component('supervision_notif',
self.session, None)
-
+
def _prepare_email(self):
session = self.session
config = session.vreg.config
uinfo = {'email': config['sender-addr'],
'name': config['sender-name']}
view = self._get_view()
- content = view.dispatch(changes=session.query_data('pendingchanges'))
+ content = view.render(changes=session.query_data('pendingchanges'))
recipients = view.recipients()
msg = format_mail(uinfo, recipients, content, view.subject(), config=config)
self.to_send = [(msg, recipients)]