[C] hooks: split previous hook.py into package
authorJulien Jehannet <julien.jehannet@logilab.fr>
Tue, 20 Oct 2009 09:47:08 +0200
changeset 73 b3146a828192
parent 72 c47d1759317c
child 74 5deaa2e20bb7
[C] hooks: split previous hook.py into package
hooks/notification.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hooks/notification.py	Tue Oct 20 09:47:08 2009 +0200
@@ -0,0 +1,56 @@
+"""Notification views for keywords / classification schemes
+
+:organization: Logilab
+:copyright: 2003-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
+"""
+__docformat__ = "restructuredtext en"
+
+from logilab.common.decorators import cached
+
+from cubicweb.selectors import implements
+from cubicweb.sobjects.notification import NotificationView
+
+
+class KeywordNotificationView(NotificationView):
+    __select__ = implements('Keyword')
+    msgid_timestamp = True
+
+    def recipients(self):
+        """Returns the project's interested people (entities)"""
+        creator = self.entity(0).created_by[0]
+        if not creator.is_in_group('managers') and creator.primary_email:
+            return [(creator.primary_email[0].address, 'fr')]
+        return []
+
+    def context(self, **kwargs):
+        context = NotificationView.context(self, **kwargs)
+        entity = self.entity(0)
+        context['kw'] = entity.name
+        return context
+
+
+class KeywordNameChanged(KeywordNotificationView):
+    id = 'notif_after_update_entity'
+
+    content = _("keyword name changed from %(oldname)s to %(kw)s")
+
+    @cached
+    def get_oldname(self, entity):
+        session = self.req
+        try:
+            return session.execute('Any N WHERE X eid %(x)s, X name N',
+                                   {'x' : entity.eid}, 'x')[0][0]
+        except IndexError:
+            return u'?'
+
+    def context(self, **kwargs):
+        entity = self.entity(0)
+        context = KeywordNotificationView.context(self, **kwargs)
+        context['oldname'] = self.get_oldname(entity)
+        return context
+
+    def subject(self):
+        entity = self.entity(0)
+        return self.req._('keyword name changed from %s to %s') % (
+            self.get_oldname(entity), entity.name)