[boxes] heavy cleanup / update to cw 3.10 api
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 12 Oct 2010 18:17:53 +0200
changeset 243 42fa2eb965df
parent 242 d62135e85bb9
child 244 f535efbb2be2
[boxes] heavy cleanup / update to cw 3.10 api * moved generalized versions of archive by date or author views to cw.baseviews * refactor boxes as contextual component * make them properly context sensitive
__pkginfo__.py
debian/control
views/boxes.py
views/secondary.py
--- a/__pkginfo__.py	Tue Oct 12 12:03:59 2010 +0200
+++ b/__pkginfo__.py	Tue Oct 12 18:17:53 2010 +0200
@@ -19,7 +19,7 @@
     'Programming Language :: JavaScript',
     ]
 
-__depends__ = {'cubicweb': '>= 3.9.0'}
+__depends__ = {'cubicweb': '>= 3.10.0'}
 __recommends__ = {'cubicweb-tag': None,
                   'cubicweb-preview': None,
                   'cubicweb-comment': '>= 1.6.3',
--- a/debian/control	Tue Oct 12 12:03:59 2010 +0200
+++ b/debian/control	Tue Oct 12 18:17:53 2010 +0200
@@ -9,7 +9,7 @@
 
 Package: cubicweb-blog
 Architecture: all
-Depends: cubicweb-common (>= 3.9.0)
+Depends: cubicweb-common (>= 3.10.0)
 Suggests: cubicweb-comment (>= 1.6.3), cubicweb-tag, cubicweb-datafeed, python-feedparser, python-rdflib
 Recommends: cubicweb-preview
 Description: blog component for the CubicWeb framework
--- a/views/boxes.py	Tue Oct 12 12:03:59 2010 +0200
+++ b/views/boxes.py	Tue Oct 12 18:17:53 2010 +0200
@@ -9,96 +9,94 @@
 
 from logilab.mtconverter import xml_escape
 
-from cubicweb.selectors import one_line_rset, is_instance
-from cubicweb.web import box
-from cubicweb.web.htmlwidgets import BoxLink, BoxWidget, BoxHtml
-
-class BlogArchivesBox(box.BoxTemplate):
-    """blog side box displaying a Blog Archive"""
-    __regid__ = 'blog_archives_box'
-    __select__ = box.BoxTemplate.__select__ & is_instance('Blog', 'BlogEntry', 'MicroBlogEntry')
-    title = _('boxes_blog_archives_box')
-    order = 35
-
-    def call(self, **kwargs):
-        """display blogs archive"""
-        # XXX turn into a selector
-        count_blogentry = self._cw.execute('Any COUNT(B) WHERE B is BlogEntry')
-        if count_blogentry[0][0] > 0:
-            box = BoxWidget(self._cw._(self.title), id=self.__regid__, islist=False)
-            box.append(BoxHtml(self._cw.view('blog_archive', None, maxentries=12)))
-            box.render(self.w)
+from cubicweb import tags
+from cubicweb.selectors import (none_rset, one_line_rset, is_instance,
+                                has_related_entities, match_view)
+from cubicweb.web import component
+from cubicweb.web.views import calendar
 
 
-class BlogsByAuthorBox(box.BoxTemplate):
-    __regid__ = 'blog_summary_box'
-    __select__ = box.BoxTemplate.__select__ & is_instance('Blog', 'BlogEntry', 'MicroBlogEntry')
-    title = _('boxes_blog_summary_box')
+class BlogArchivesBox(component.EntityCtxComponent):
+    """blog side box displaying a Blog Archive"""
+    __regid__ = 'blog.archives_by_date'
+    __select__ = (component.EntityCtxComponent.__select__
+                  & is_instance('Blog', 'MicroBlog')
+                  & has_related_entities('entry_of', 'object'))
+    title = _('blog.archives_by_date')
+    order = 35
+
+    def render_body(self, w):
+        rset = self.entity.related('entry_of', 'object')
+        self._cw.view('cw.archive.by_date', rset, maxentries=12,
+                      basepath=self.entity.rest_path() + '/blogentries',
+                      w=w)
+
+
+class BlogByAuthorBox(component.EntityCtxComponent):
+    __regid__ = 'blog.archives_by_author'
+    __select__ = (component.EntityCtxComponent.__select__
+                  & is_instance('Blog', 'MicroBlogEntry')
+                  & has_related_entities('entry_of', 'object'))
+    title = _('blog.archives_by_author')
     order = 36
 
-    def call(self, view=None, **kwargs):
-        box = BoxWidget(self._cw._(self.title), self.__regid__, islist=True)
-        rql = 'Any U, COUNT(B) GROUPBY U WHERE U is CWUser, ' \
-              'B is BlogEntry, B created_by U'
-        rset = self._cw.execute(rql)
-        for user in rset:
-            euser = self._cw.entity_from_eid(user[0])
-            box.append(BoxLink(self._cw.build_url('blogentry/%s' % euser.login),
-                                              u'%s [%s]' % (euser.name(),
-                                                            user[1])))
-        box.render(self.w)
+    def render_body(self, w):
+        rset = self.entity.related('entry_of', 'object')
+        self._cw.view('cw.archive.by_author', rset, maxentries=12,
+                      basepath=self.entity.rest_path() + '/blogentries',
+                      w=w)
 
 
-class LatestBlogsBox(box.BoxTemplate):
+class LatestBlogsBox(component.CtxComponent):
     """display a box with latest blogs and rss"""
-    __regid__ = 'latest_blogs_box'
-    __select__ = box.BoxTemplate.__select__ & is_instance('Blog', 'BlogEntry', 'MicroBlogEntry')
-    title = _('latest_blogs_box')
-    visible = True # enabled by default
+    __regid__ = 'blog.latest_blogs'
+    __select__ = (component.CtxComponent.__select__
+                  & none_rset() & match_view('index'))
+    title = _('blog.latest_blogs')
     order = 34
     display_see_more_link = True
 
     def latest_blogs_rset(self):
         return self._cw.execute(
-            'Any X,T,CD ORDERBY CD DESC LIMIT 5 WHERE X is BlogEntry, '
+            'Any X,T,CD ORDERBY CD DESC LIMIT 5 WHERE X is IN (MicroBlogEntry, BlogEntry), '
             'X title T, X creation_date CD')
 
-    def call(self, **kwargs):
+    def render_body(self, w):
         # XXX turn into a selector
         rset = self.latest_blogs_rset()
         if not rset:
             return
-        box = BoxWidget(self._cw._(self.title), self.__regid__, islist=True)
-        # TODO - get the date between brakets after link
-        # empty string for title argument to deactivate auto-title
-        for i in xrange(rset.rowcount):
-            entity = rset.get_entity(i, 0)
-            box.append(BoxLink(entity.absolute_url(), xml_escape(entity.dc_title())))
+        w(u'<ul class="boxListing">')
+        for entity in rset.entities():
+            w(u'<li>%s</li>\n' %
+              tags.a(entity.dc_title(), href=entity.absolute_url()))
         rqlst = rset.syntax_tree()
         rqlst.set_limit(None)
         rql = rqlst.as_string(kwargs=rset.args)
         if self.display_see_more_link:
             url = self._cw.build_url('view', rql=rql, page_size=10)
-            box.append(BoxLink(url,  u'[%s]' % self._cw._(u'see more')))
+            w(u'<li>%s</li>\n' %
+              tags.a(u'[%s]' % self._cw._(u'see more'), href=url))
         rss_icon = self._cw.uiprops['RSS_LOGO_16']
         # 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)
-        box.append(BoxLink(rss_url, rss_label))
-        box.render(self.w)
+        w(u'<li>%s</li>\n' %
+          tags.a(rss_label, href=rss_url, escapecontent=False))
+        w(u'</ul>\n')
 
 
 class LatestBlogsBlogBox(LatestBlogsBox):
     """display a box with latest blogs and rss, filtered for a particular blog
     """
-    __select__ = LatestBlogsBox.__select__ & one_line_rset() & is_instance('Blog')
-    title = _('latest_blogs_blog_box')
+    __select__ = (component.CtxComponent.__select__
+                  & one_line_rset() & is_instance('Blog'))
     display_see_more_link = False
 
     def latest_blogs_rset(self):
         blog = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0)
         return self._cw.execute(
-            'Any X,T,CD ORDERBY CD DESC LIMIT 5 WHERE X is BlogEntry, '
+            '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})
--- a/views/secondary.py	Tue Oct 12 12:03:59 2010 +0200
+++ b/views/secondary.py	Tue Oct 12 18:17:53 2010 +0200
@@ -18,63 +18,6 @@
 
 from cubes.blog.views.primary import render_blogentry_title
 
-class BlogEntryArchiveView(StartupView):
-    """control the view of a blog archive"""
-    __regid__ = 'blog_archive'
-    countrql = 'Any COUNT(B) WHERE B is BlogEntry, B creation_date >=  %(firstday)s, B creation_date <= %(lastday)s'
-
-    def represent(self, items, year, month):
-        """represent a single month entry"""
-        firstday = datetime(year, month, 1)
-        lastday = datetime(year, month, monthrange(year, month)[1])
-        rql = ('Any B, BD ORDERBY BD DESC '
-               'WHERE B is BlogEntry, B creation_date BD, '
-               'B creation_date >=  "%s", B creation_date <= "%s"' %
-                (firstday.strftime('%Y-%m-%d'), lastday.strftime('%Y-%m-%d')))
-        args = {'firstday':firstday, 'lastday':lastday}
-        nmb_entries = self._cw.execute(self.countrql, args)[0][0]
-        label = u'%s %s [%s]' % (self._cw._(calendar.MONTHNAMES[month-1]), year,
-                                 nmb_entries)
-        vtitle = '%s %s' % (display_name(self._cw, 'BlogEntry', 'plural'), label)
-        url = xml_escape(self._cw.build_url('view', rql=rql, month=month,
-                                            year=year, vtitle=vtitle))
-        link = u'<a href="%s" title="">%s</a>' % (url, label)
-        items.append( u'<li class="">%s</li>\n' % link )
-
-    def call(self, maxentries=None, **kwargs):
-        """display a list of entities by calling their <item_vid> view
-        """
-        rset = self._cw.execute('Any CD ORDERBY CD DESC WHERE B is BlogEntry, '
-                                'B creation_date CD')
-        blogmonths = []
-        items = []
-        for (blogdate,) in rset:
-            year, month = blogdate.year, blogdate.month
-            if (year, month) not in blogmonths:
-                blogmonths.append( (year, month) )
-        if maxentries is None:
-            displayed_months = blogmonths
-            needmore = False
-        else:
-            needmore = len(blogmonths) > maxentries
-            displayed_months = blogmonths[:maxentries]
-        for year, month in displayed_months:
-            self.represent(items, year, month)
-        if needmore:
-            url = self._cw.build_url('view', vid='blog_archive')
-            link = u'<a href="%s" title="">[%s]</a>' % (
-                url, self._cw._('see more archives'))
-            items.append( u'<li class="">%s</li>\n' % link )
-        w = self.w
-        w(u'<div class="boxFrame">')
-        if items:
-            w(u'<div class="boxContent">\n')
-            w(u'<ul class="boxListing">')
-            w(''.join(items))
-            w(u'</ul>\n</div>\n')
-        w(u'</div>')
-
-
 class BlogEntrySameETypeListView(baseviews.SameETypeListView):
     __select__ = baseviews.SameETypeListView.__select__ & is_instance('BlogEntry')
     countrql = ('Any COUNT(B) WHERE B is BlogEntry, '