Newer
Older
Sandrine Ribeau
committed
"""Secondary views for blogs
:organization: Logilab
:copyright: 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
Sandrine Ribeau
committed
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
"""
__docformat__ = "restructuredtext en"
from calendar import monthrange
from datetime import datetime
from logilab.mtconverter import xml_escape
from cubicweb.schema import display_name
from cubicweb.view import EntityView, StartupView
from cubicweb.selectors import paginated_rset, sorted_rset, is_instance
from cubicweb.web.views import baseviews, calendar, navigation
Sandrine Ribeau
committed
Sandrine Ribeau
committed
from cubes.blog.views.primary import render_blogentry_title
Sandrine Ribeau
committed
class BlogEntryArchiveView(StartupView):
"""control the view of a blog archive"""
Sandrine Ribeau
committed
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,
Sandrine Ribeau
committed
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))
Sandrine Ribeau
committed
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')
Sandrine Ribeau
committed
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:
link = u'<a href="%s" title="">[%s]</a>' % (
url, self._cw._('see more archives'))
Sandrine Ribeau
committed
items.append( u'<li class="">%s</li>\n' % link )
self.w(u'<div class="boxFrame">')
if items:
self.w(u'<div class="boxContent">\n')
self.w(u'<ul class="boxListing">')
self.w(''.join(items))
self.w(u'</ul>\n</div>\n')
self.w(u'</div>')
class BlogEntrySameETypeListView(baseviews.SameETypeListView):
__select__ = baseviews.SameETypeListView.__select__ & is_instance('BlogEntry')
countrql = ('Any COUNT(B) WHERE B is BlogEntry, '
'B creation_date >= %(firstday)s, B creation_date <= %(lastday)s')
Sandrine Ribeau
committed
def call(self, **kwargs):
super(BlogEntrySameETypeListView, self).call(**kwargs)
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']))
Sandrine Ribeau
committed
def render_next_previous(self, year, month):
if month == 12:
nextmonth = 1
year = year + 1
else:
nextmonth = month + 1
if month == 1:
previousmonth = 12
year = year - 1
else:
previousmonth = month -1
self.w(u'<div class="prevnext">')
self.w(u'<span class="previousmonth">%s</span>' \
% self.render_link(year, previousmonth,
xml_escape(u'<< ' + self._cw._(u'previous month'))))
Sandrine Ribeau
committed
self.w(u'<span class="nextmonth">%s</span>' \
% self.render_link(year, nextmonth,
Sandrine Ribeau
committed
self.w(u'</div>')
def render_link(self, year, month, atitle):
firstday = datetime(year, month, 1)
lastday = datetime(year, month, monthrange(year, month)[1])
args = {'firstday': firstday, 'lastday': lastday}
nmb_entries = self._cw.execute(self.countrql, args)[0][0]
if not nmb_entries:
return
Sandrine Ribeau
committed
rql = ('Any B, BD ORDERBY BD DESC '
'WHERE B is BlogEntry, B creation_date BD, '
'B creation_date >= "%s", B creation_date <= "%s"' %
Sandrine Ribeau
committed
(firstday.strftime('%Y-%m-%d'), lastday.strftime('%Y-%m-%d')))
label = u'%s %s [%s]' % (self._cw._(calendar.MONTHNAMES[month-1]), year,
Sandrine Ribeau
committed
nmb_entries)
vtitle = '%s %s' % (display_name(self._cw, 'BlogEntry', 'plural'), label)
url = self._cw.build_url('view', rql=rql, vtitle=vtitle,
month=month, year=year)
return u'<a href="%s">%s</a>' % (xml_escape(url), atitle)
Sandrine Ribeau
committed
class BlogEntryBlogView(EntityView):
__select__ = is_instance('BlogEntry')
Sandrine Ribeau
committed
def toolbar_components(self, context):
# copy from PrimaryView
self.w(u'<div class="%s">' % context)
for comp in self._cw.vreg['toolbar'].poss_visible_objects(
self._cw, rset=self.cw_rset, row=self.cw_row, view=self, context=context):
comp.render(w=self.w, row=self.cw_row, view=self)
self.w(u'</div>')
Sandrine Ribeau
committed
w = self.w
Sandrine Ribeau
committed
w(u'<div class="post">')
Sandrine Ribeau
committed
render_blogentry_title(self._cw, w, entity)
Sandrine Ribeau
committed
w(u'<div class="entry">')
body = entity.printable_value('content')
w(body)
w(u'</div>')
w(u'<br class="clear"/>')
Sandrine Ribeau
committed
w(u'<div class="postmetadata">%s</div>' % entity.view('post-reldata'))
w(u'</div>')
class BlogEntryPostMetaData(EntityView):
__select__ = is_instance('BlogEntry')
Sandrine Ribeau
committed
def cell_call(self, row, col):
_ = lambda ertype, form='': display_name(self._cw, ertype, form)
Sandrine Ribeau
committed
reldata = []
w = reldata.append
Sylvain Thénault
committed
schema = self._cw.vreg.schema
if 'comments' in schema and \
'BlogEntry' in schema.rschema('comments').objects():
from cubes.comment.entities import subcomments_count
count = subcomments_count(entity)
Sandrine Ribeau
committed
if count:
url = xml_escape(entity.absolute_url())
Sylvain Thénault
committed
if count > 1:
label = _('Comment', 'plural')
else:
label = _('Comment')
w(u'<a href="%s">%s %s</a>' % (url, count, label))
Sandrine Ribeau
committed
else:
w(u'%s %s' % (count, _('Comment')))
Sylvain Thénault
committed
if 'tags' in schema and 'BlogEntry' in schema.rschema('tags').objects():
Sandrine Ribeau
committed
tag_rset = entity.related('tags', 'object')
if tag_rset:
w(u'%s %s' % (_('tags', 'object'), self._cw.view('csv', tag_rset)))
Sandrine Ribeau
committed
rset = entity.related('entry_of', 'subject')
if rset:
Sandrine Ribeau
committed
self.w(u' | '.join(reldata))
class BlogNavigation(navigation.PageNavigation):
__select__ = paginated_rset() & sorted_rset() & is_instance('BlogEntry')
Sandrine Ribeau
committed
def index_display(self, start, stop):
return u'%s' % (int(start / self.page_size)+1)