cw 3.6 api update
authorSandrine Ribeau <sandrine.ribeau@logilab.fr>
Tue, 08 Dec 2009 13:02:38 +0100
changeset 147 aced35dab06d
parent 146 b509fc2c9435
child 148 18c69e7629d4
cw 3.6 api update
entities.py
schema.py
test/unittest_blog.py
views/primary.py
views/secondary.py
--- a/entities.py	Sat Nov 07 19:25:24 2009 +0100
+++ b/entities.py	Tue Dec 08 13:02:38 2009 +0100
@@ -16,7 +16,7 @@
 class Blog(AnyEntity):
     """customized class for Blog entities"""
 
-    id = 'Blog'
+    __regid__ = 'Blog'
     __implements__ = AnyEntity.__implements__ + (ISiocContainer,)
 
     def rss_feed_url(self):
@@ -39,7 +39,7 @@
 
 class BlogEntry(AnyEntity):
     """customized class for BlogEntry entities"""
-    id = 'BlogEntry'
+    __regid__ = 'BlogEntry'
     fetch_attrs, fetch_order = fetch_config(['creation_date', 'title'], order='DESC')
     __implements__ = AnyEntity.__implements__ + (
         ICalendarViews, ICalendarable, ISiocItem, IPrevNext)
@@ -74,7 +74,7 @@
         return []
 
     def postinfo_description(self):
-        _ = self.req._
+        _ = self._cw._
         descr = u'%s %s' % (_('posted on'), self.format_date(self.creation_date))
         return descr
 
@@ -103,12 +103,12 @@
             rql = ('Any B ORDERBY B %s LIMIT 1 '
                    'WHERE B is BlogEntry, B entry_of BL, BL eid %%(blog)s, '
                    'B eid %s %%(eid)s')
-            rset = self.req.execute(rql % (order, operator),
+            rset = self._cw.execute(rql % (order, operator),
                                     {'blog': self.entry_of[0].eid, 'eid': self.eid})
         else:
             rql = ('Any B ORDERBY B %s LIMIT 1 '
                    'WHERE B is BlogEntry, B eid %s %%(eid)s')
-            rset = self.req.execute(rql % (order, operator), {'eid': self.eid})
+            rset = self._cw.execute(rql % (order, operator), {'eid': self.eid})
         if rset:
             return rset.get_entity(0,0)
 
--- a/schema.py	Sat Nov 07 19:25:24 2009 +0100
+++ b/schema.py	Tue Dec 08 13:02:38 2009 +0100
@@ -13,7 +13,7 @@
 
 
 class BlogEntry(WorkflowableEntityType):
-    permissions = {
+    __permissions__ = {
         'read': ('managers', 'users', ERQLExpression('X in_state S, S name "published"'),),
         'add': ('managers', 'users'),
         'update': ('managers', 'owners'),
--- a/test/unittest_blog.py	Sat Nov 07 19:25:24 2009 +0100
+++ b/test/unittest_blog.py	Tue Dec 08 13:02:38 2009 +0100
@@ -2,15 +2,14 @@
 import re
 
 from logilab.common.testlib import unittest_main, mock_object
-from cubicweb.devtools.apptest import ControllerTC
-from cubicweb.devtools.testlib import WebTest
+from cubicweb.devtools.testlib import CubicWebTC
 
 from email.Header import decode_header
 from cubicweb.sobjects.notification import RenderAndSendNotificationView
 from cubicweb.server.hookhelper import SendMailOp
 
 
-class BlogTests(ControllerTC):
+class BlogTestsCubicWebTC(CubicWebTC):
     """test blog specific behaviours"""
 
     def test_notifications(self):
@@ -19,7 +18,7 @@
         self.execute('SET E entry_of B WHERE B eid %(beid)s, E eid %(eeid)s' % {'beid' :cubicweb_blog.eid, 'eeid' : blog_entry_1.eid})
         blog_entry_2 = self.add_entity('BlogEntry', title=u"yes",  content=u"cubicweb yes")
         self.execute('SET E entry_of B WHERE B eid %(beid)s, E eid %(eeid)s' % {'beid' :cubicweb_blog.eid, 'eeid' : blog_entry_2.eid})
-        session = self.session()
+        session = self.session
         for op in session.pending_operations:
             if isinstance(op, RenderAndSendNotificationView):
                 op.precommit_event()
--- a/views/primary.py	Sat Nov 07 19:25:24 2009 +0100
+++ b/views/primary.py	Tue Dec 08 13:02:38 2009 +0100
@@ -29,14 +29,14 @@
     def render_entity_attributes(self, entity):
         super(BlogPrimaryView, self).render_entity_attributes(entity)
         self.w('<a class="right" href="%s">%s <img src="%s" alt="%s"/></a>' % (
-            xml_escape(entity.rss_feed_url()), self.req._(u'subscribe'),
-            self.req.external_resource('RSS_LOGO_16'), self.req._('rss icon')))
+            xml_escape(entity.rss_feed_url()), self._cw._(u'subscribe'),
+            self._cw.external_resource('RSS_LOGO_16'), self._cw._('rss icon')))
 
     def render_entity_relations(self, entity):
         super(BlogPrimaryView, self).render_entity_relations(entity)
         rset = entity.related('entry_of', 'object')
         strio = UStringIO()
-        self.pagination(self.req, rset, strio.write, page_size=10)
+        self.pagination(self._cw, rset, strio.write, page_size=10)
         self.wview('adaptedlist', rset, 'null')
         self.w(strio.getvalue())
 
@@ -53,5 +53,5 @@
     def render_entity_relations(self, entity):
         rset = entity.related('entry_of', 'subject')
         if rset:
-            self.w(self.req._('blogged in '))
+            self.w(self._cw._('blogged in '))
             self.wview('csv', rset, 'null')
--- a/views/secondary.py	Sat Nov 07 19:25:24 2009 +0100
+++ b/views/secondary.py	Tue Dec 08 13:02:38 2009 +0100
@@ -20,7 +20,7 @@
 
 class BlogEntryArchiveView(StartupView):
     """control the view of a blog archive"""
-    id = '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):
@@ -32,10 +32,10 @@
                '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.req.execute(self.countrql, args)[0][0]
-        label = u'%s %s [%s]' % (self.req._(calendar.MONTHNAMES[month-1]), year,
+        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.req, 'BlogEntry', 'plural'), label)
+        vtitle = '%s %s' % (display_name(self._cw, 'BlogEntry', 'plural'), label)
         url = xml_escape(self.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 )
@@ -43,7 +43,7 @@
     def call(self, maxentries=None, **kwargs):
         """display a list of entities by calling their <item_vid> view
         """
-        rset = self.req.execute('Any CD ORDERBY CD DESC WHERE B is BlogEntry, B creation_date CD')
+        rset = self._cw.execute('Any CD ORDERBY CD DESC WHERE B is BlogEntry, B creation_date CD')
 
         blogmonths = []
         items = []
@@ -74,35 +74,35 @@
 
 class BlogEntryArchiveBox(boxes.BoxTemplate):
     """blog side box displaying a Blog Archive"""
-    id = 'blog_archives_box'
+    __regid__ = 'blog_archives_box'
     title = _('boxes_blog_archives_box')
     order = 35
 
     def call(self, **kwargs):
         """display blogs archive"""
         # XXX turn into a selector
-        count_blogentry = self.req.execute('Any COUNT(B) WHERE B is BlogEntry')
+        count_blogentry = self._cw.execute('Any COUNT(B) WHERE B is BlogEntry')
         if count_blogentry[0][0] > 0:
-            box = BoxWidget(self.req._(self.title), id=self.id, islist=False)
+            box = BoxWidget(self._cw._(self.title), id=self.__regid__, islist=False)
             box.append(boxes.BoxHtml(self.view('blog_archive', None, maxentries=12)))
             box.render(self.w)
 
 
 class BlogEntryListBox(boxes.BoxTemplate):
     """display a box with latest blogs and rss"""
-    id = 'blog_latest_box'
+    __regid__ = 'blog_latest_box'
     title = _('blog_latest_box')
     visible = True # enabled by default
     order = 34
 
     def call(self, view=None, **kwargs):
         # XXX turn into a selector
-        rset = self.req.execute('Any X,T,CD ORDERBY CD DESC LIMIT 5 '
+        rset = self._cw.execute('Any X,T,CD ORDERBY CD DESC LIMIT 5 '
                                 'WHERE X is BlogEntry, X title T, '
                                 'X creation_date CD')
         if not rset:
             return
-        box = BoxWidget(self.req._(self.title), self.id, islist=True)
+        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):
@@ -112,28 +112,32 @@
         rqlst.set_limit(None)
         rql = rqlst.as_string(kwargs=rset.args)
         url = self.build_url('view', rql=rql, page_size=10)
-        box.append(BoxLink(url,  u'[%s]' % self.req._(u'see more')))
-        rss_icon = self.req.external_resource('RSS_LOGO_16')
+        box.append(BoxLink(url,  u'[%s]' % self._cw._(u'see more')))
+        rss_icon = self._cw.external_resource('RSS_LOGO_16')
         # FIXME - could use rss_url defined as a property if available
         rss_label = u'%s <img src="%s" alt="%s"/>' % (
-            self.req._(u'subscribe'), rss_icon, self.req._('rss icon'))
+            self._cw._(u'subscribe'), rss_icon, self._cw._('rss icon'))
         rss_url = self.build_url('view', vid='rss', rql=rql)
         box.append(BoxLink(rss_url, rss_label))
         box.render(self.w)
 
+
 class BlogEntrySummary(boxes.BoxTemplate):
-    id = 'blog_summary_box'
+    __regid__ = 'blog_summary_box'
     title = _('boxes_blog_summary_box')
     order = 36
-    __select__ = boxes.BoxTemplate.__select__ & authenticated_user()
+    __select__ = boxes.BoxTemplate.__select__
 
     def call(self, view=None, **kwargs):
-        box = BoxWidget(self.req._(self.title), self.id, islist=True)
-        rql = 'Any FN, SN, L, COUNT(B) GROUPBY L, SN, FN WHERE U is CWUser, U firstname FN, ' \
-              'U surname SN, U login L, B is BlogEntry, B created_by U'
-        rset = self.req.execute(rql)
+        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:
-            box.append(BoxLink(self.build_url('blogentry/%s' % user[2]), u'%s %s [%s]' % (user[0], user[1], user[3])))
+            euser = self._cw.entity_from_eid(user[0])
+            box.append(BoxLink(self.build_url('blogentry/%s' % euser.login),
+                                              u'%s [%s]' % (euser.name(),
+                                                            user[1])))
         box.render(self.w)
 
 ## list views ##################################################################
@@ -143,10 +147,10 @@
     countrql = 'Any COUNT(B) WHERE B is BlogEntry, B creation_date >=  %(firstday)s, B creation_date <= %(lastday)s'
 
     def call(self, **kwargs):
-        self.req.add_css('cubes.blog.css')
+        self._cw.add_css('cubes.blog.css')
         super(BlogEntryAdaptedListView, self).call(**kwargs)
-        if 'year' in self.req.form and 'month' in self.req.form:
-            self.render_next_previous(int(self.req.form['year']), int(self.req.form['month']))
+        if 'year' in self._cw.form and 'month' in self._cw.form:
+            self.render_next_previous(int(self._cw.form['year']), int(self._cw.form['month']))
 
     def render_next_previous(self, year, month):
         if month == 12:
@@ -163,10 +167,10 @@
         self.w(u'<div class="prevnext">')
         self.w(u'<span class="previousmonth">%s</span>' \
                % self.render_link(year, previousmonth,
-                                  xml_escape(u'<< ' + self.req._(u'previous month'))))
+                                  xml_escape(u'<< ' + self._cw._(u'previous month'))))
         self.w(u'<span class="nextmonth">%s</span>' \
                % self.render_link(year, nextmonth,
-                                  xml_escape(self.req._(u'next month') + u' >>')))
+                                  xml_escape(self._cw._(u'next month') + u' >>')))
         self.w(u'</div>')
 
     def render_link(self, year, month, atitle):
@@ -177,13 +181,13 @@
                '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.req.execute(self.countrql, args)[0][0]
-        label = u'%s %s [%s]' % (self.req._(calendar.MONTHNAMES[month-1]), year,
+        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('BlogEntry', 'plural'), label)
         url = xml_escape(self.build_url('view', rql=rql, vtitle=vtitle,
                                         month=month, year=year))
-        if self.req.execute(rql):
+        if self._cw.execute(rql):
             return u'<a href="%s" title="">%s</a>' % (url, atitle)
         return u''
 
@@ -193,13 +197,13 @@
 
 
 class BlogEntryBlogView(EntityView):
-    id = 'blog'
+    __regid__ = 'blog'
     __select__ = implements('BlogEntry')
 
     def cell_call(self, row, col):
         entity = self.entity(row, col)
         w = self.w
-        _ = self.req._
+        _ = self._cw._
         w(u'<div class="post">')
         w(u'<h1>%s</h1>' % entity.view('incontext'))
         w(u'%s ' % entity.postinfo_description())
@@ -220,12 +224,12 @@
 
 
 class BlogEntryPostMetaData(EntityView):
-    id = 'post-reldata'
+    __regid__ = 'post-reldata'
     __select__ = implements('BlogEntry')
 
     def cell_call(self, row, col):
         entity = self.entity(row, col)
-        _ = lambda ertype, form='': display_name(self.req, ertype, form)
+        _ = lambda ertype, form='': display_name(self._cw, ertype, form)
         reldata = []
         w = reldata.append
         if 'comments' in self.schema and 'BlogEntry' in self.schema.rschema('comments').objects():
@@ -242,7 +246,7 @@
                 w(u'%s %s' % (_('tags', 'object'), self.view('csv', tag_rset)))
         rset = entity.related('entry_of', 'subject')
         if rset:
-            w(u'%s %s' % (self.req._('blogged in '),
+            w(u'%s %s' % (self._cw._('blogged in '),
                           self.view('csv', rset, 'null')))
         self.w(u' | '.join(reldata))