Addition context views for navigation boxes (closes #1529146)
authorArthur Lutz <arthur.lutz@logilab.fr>
Thu, 07 Jun 2012 17:07:27 +0200
changeset 279 acc20451d14b
parent 278 bb889339826f
child 280 3ff01e768daa
Addition context views for navigation boxes (closes #1529146)
views/boxes.py
--- a/views/boxes.py	Thu Apr 19 13:59:50 2012 +0200
+++ b/views/boxes.py	Thu Jun 07 17:07:27 2012 +0200
@@ -15,10 +15,10 @@
 from cubicweb.web import component
 
 
-class BlogArchivesBox(component.EntityCtxComponent):
+class BlogArchivesBox(component.CtxComponent):
     """blog side box displaying a Blog Archive"""
     __regid__ = 'blog.archives_by_date'
-    __select__ = (component.EntityCtxComponent.__select__
+    __select__ = (component.CtxComponent.__select__
                   & is_instance('Blog', 'MicroBlog')
                   & has_related_entities('entry_of', 'object'))
     title = _('blog.archives_by_date')
@@ -26,28 +26,56 @@
     context = 'left'
 
     def render_body(self, w):
-        rset = self.entity.related('entry_of', 'object')
+        #FIXME doesn't handle (yet) multiple blogs
+        entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0)
+        rset = entity.related('entry_of', 'object')
         self._cw.view('cw.archive.by_date', rset, maxentries=6,
-                      basepath=self.entity.rest_path() + '/blogentries',
+                      basepath=entity.rest_path() + '/blogentries',
                       w=w)
 
 
-class BlogByAuthorBox(component.EntityCtxComponent):
+class BlogEntryArchivesBox(BlogArchivesBox):
+    __regid__ = 'blog.entry_archives_by_date'
+    __select__ = (component.CtxComponent.__select__
+                  & is_instance('BlogEntry', 'MicroBlogEntry')
+                  & has_related_entities('entry_of', 'subject'))
+
+    def render_body(self, w):
+        entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0)
+        box = self._cw.vreg['ctxcomponents'].select('blog.archives_by_date', self._cw, w=w,
+                                                    rset=entity.related('entry_of', 'subject'))
+        box.render_body(w)
+
+class BlogByAuthorBox(component.CtxComponent):
     __regid__ = 'blog.archives_by_author'
-    __select__ = (component.EntityCtxComponent.__select__
-                  & is_instance('Blog', 'MicroBlogEntry')
+    __select__ = (component.CtxComponent.__select__
+                  & is_instance('Blog', 'MicroBlog')
                   & has_related_entities('entry_of', 'object'))
     title = _('blog.archives_by_author')
     order = 36
     context = 'left'
 
     def render_body(self, w):
-        rset = self.entity.related('entry_of', 'object')
+        #FIXME doesn't handle (yet) multiple blogs
+        entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0)
+        rset = entity.related('entry_of', 'object')
         self._cw.view('cw.archive.by_author', rset,
-                      basepath=self.entity.rest_path() + '/blogentries',
+                      basepath=entity.rest_path() + '/blogentries',
                       w=w)
 
 
+class BlogEntryByAuthorBox(BlogByAuthorBox):
+    __regid__ = 'blog.entry_archives_by_author'
+    __select__ = (component.CtxComponent.__select__
+                  & is_instance('BlogEntry', 'MicroBlogEntry')
+                  & has_related_entities('entry_of', 'subject'))
+
+    def render_body(self, w):
+        entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0)
+        box = self._cw.vreg['ctxcomponents'].select('blog.archives_by_author', self._cw, w=w,
+                                                    rset=entity.related('entry_of', 'subject'))
+        box.render_body(w)
+
 class LatestBlogsBox(component.CtxComponent):
     """display a box with latest blogs and rss"""
     __regid__ = 'blog.latest_blogs'
@@ -83,7 +111,11 @@
         # FIXME - could use rss_url defined as a property if available
         rss_label = u'%s <img src="%s" alt="%s"/>' % (
             self._cw._(u'subscribe'), rss_icon, self._cw._('rss icon'))
-        rss_url = self._cw.build_url('view', vid='rss', rql=rql)
+        blogs = self._cw.execute('Any B,RSS WHERE B is Blog, B rss_url RSS')
+        if len(blogs) == 1:
+            rss_url = blogs[0][1]
+        else:
+            rss_url = self._cw.build_url('view', vid='rss', rql=rql)
         w(u'<li>%s</li>\n' %
           tags.a(rss_label, href=rss_url, escapecontent=False))
         w(u'</ul>\n')
@@ -103,3 +135,22 @@
             'Any X,T,CD ORDERBY CD DESC LIMIT 5 WHERE '
             'X title T, X creation_date CD, X entry_of B, B eid %(b)s',
             {'b': blog.eid})
+
+class LatestBlogsBlogEntryBox(LatestBlogsBox):
+    """display a box with latest blogs and rss, filtered for a particular blog
+    """
+    __select__ = (component.CtxComponent.__select__
+                  & is_instance('BlogEntry')
+                  & has_related_entities('entry_of', 'subject'))
+    display_see_more_link = False
+    contextual = True
+
+    def latest_blogs_rset(self):
+        blogentry = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0)
+        #FIXME doesn't handle (yet) multiple blogs
+        blog = blogentry.related('entry_of', 'subject').get_entity(0,0)
+        return self._cw.execute(
+            'Any X,T,CD ORDERBY CD DESC LIMIT 5 WHERE '
+            'X title T, X creation_date CD, X entry_of B, B eid %(b)s',
+            {'b': blog.eid})
+