goa/skel/views.py
changeset 0 b97547f5f1fa
child 1546 68c69980f6a1
equal deleted inserted replaced
-1:000000000000 0:b97547f5f1fa
       
     1 # custom application views
       
     2 
       
     3 from mx.DateTime import DateTime
       
     4 
       
     5 from cubicweb.web.views import baseviews
       
     6 from cubicweb.web.views.boxes import BoxTemplate
       
     7 from cubicweb.web.views.calendar import MONTHNAMES
       
     8 from cubicweb.web.htmlwidgets import BoxLink, BoxWidget
       
     9 
       
    10 _ = unicode
       
    11 
       
    12 
       
    13 class BlogEntryPrimaryView(baseviews.PrimaryView):
       
    14     accepts = ('BlogEntry',)
       
    15     
       
    16     def cell_call(self, row, col):
       
    17         entity = self.entity(row, col)
       
    18         self.w(u'<h1>%s</h1>' % entity.dc_title())
       
    19         entity.view('metadata', w=self.w)
       
    20         self.w(entity.printable_value('text'))
       
    21         
       
    22 
       
    23 class BlogArchiveBox(BoxTemplate):
       
    24     """side box usually displaying some related entities in a primary view"""
       
    25     id = 'blog_archives_box'
       
    26     title = _('blog archives')
       
    27 
       
    28     def call(self, **kwargs):
       
    29         """display a list of entities by calling their <item_vid> view
       
    30         """
       
    31         _ = self.req._
       
    32         rset = self.req.execute('Any CD ORDERBY CD DESC WHERE B is Blog, B creation_date CD')
       
    33         blogmonths = []
       
    34         for (blogdate,) in rset:
       
    35             year, month = blogdate.year, blogdate.month
       
    36             if (year, month) not in blogmonths:
       
    37                 blogmonths.append( (year, month) )
       
    38         box = BoxWidget(_('Blog archives'), id=self.id)
       
    39         for year, month in blogmonths:
       
    40             firstday = DateTime(year, month, 1)
       
    41             lastday = DateTime(year, month, firstday.days_in_month)
       
    42             rql = ('Any B WHERE B is BlogEntry, B creation_date >= "%s", B creation_date <= "%s"'
       
    43                    % (firstday.strftime('%Y-%m-%d'), lastday.strftime('%Y-%m-%d')))
       
    44             url = self.build_url(rql=rql)
       
    45             label = u'%s %s' % (_(MONTHNAMES[month-1]), year)
       
    46             box.append( BoxLink(url, label) )
       
    47         box.render(self.w)
       
    48 
       
    49 
       
    50 
       
    51