author Nicolas Chauvat <nicolas.chauvat@logilab.fr>
Thu, 05 Aug 2010 19:31:27 +0200
changeset 218 45510a9bb345
parent 215 4c9a9b321087
child 222 95ad22474c85
permissions -rw-r--r--
[views] use has_creator when available. add cwuri facet.

"""entity classes for Blog entities

:organization: Logilab
:copyright: 2003-2010 LOGILAB S.A. (Paris, FRANCE)
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
:license: Lesser General Public License version 2 or above - http://www.gnu.org/
__docformat__ = "restructuredtext en"

from logilab.common.date import todate

from cubicweb.entities import AnyEntity, fetch_config
from cubicweb.view import EntityAdapter
from cubicweb.selectors import is_instance

class BlogIFeedAdapter(EntityAdapter):
    __regid__ = 'IFeed'
    __select__ = EntityAdapter.__select__ & is_instance('Blog')

    def rss_feed_url(self):
        if self.entity.rss_url:
            return self.entity.rss_url
        rql = ('Any E ORDERBY D DESC '
               'WHERE E is BlogEntry, E entry_of X, X eid %s, E creation_date D'
        return self._cw.build_url(rql=rql % self.entity.eid, vid='rss',

class BlogISiocContainerAdapter(EntityAdapter):
    __regid__ = 'ISIOCContainer'
    __select__ = EntityAdapter.__select__ & is_instance('Blog')

    def isioc_type(self):
        return 'Weblog'

    def isioc_items(self):
        return self.entity.reverse_entry_of

class BlogEntry(AnyEntity):
    """customized class for BlogEntry entities"""
    __regid__ = 'BlogEntry'
    fetch_attrs, fetch_order = fetch_config(['creation_date', 'title'], order='DESC')

    def dc_title(self):
        return self.title

    def dc_description(self, format='text/plain'):
        return self.printable_value('content', format=format)

    def dc_date(self, date_format=None):# XXX default to ISO 8601 ?
        """return latest modification date of this entity"""
        return self._cw.format_date(self.creation_date, date_format=date_format)

    def parent(self):
        return self.entry_of and self.entry_of[0] or None

class BlogEntryICalendarableAdapter(EntityAdapter):
    __regid__ = 'ICalendarable'
    __select__ = EntityAdapter.__select__ & is_instance('BlogEntry')

    def start(self):
        return self.entity.creation_date

    def stop(self):
        return self.entity.creation_date

class BlogEntryICalendarViewsAdapter(EntityAdapter):
    __regid__ = 'ICalendarViews'
    __select__ = EntityAdapter.__select__ & is_instance('BlogEntry')

    def matching_dates(self, begin, end):
        """calendar views interface"""
        mydate = self.entity.creation_date
        if not mydate:
            return []
        mydate = todate(mydate)
        if begin < mydate < end:
            return [mydate]
        return []

class BlogEntryISiocItemAdapter(EntityAdapter):
    __regid__ = 'ISIOCItem'
    __select__ = EntityAdapter.__select__ & is_instance('BlogEntry')

    def isioc_content(self):
        return self.entity.content

    def isioc_container(self):
        return self.entity.parent()

    def isioc_type(self):
        return 'BlogPost'

    def isioc_replies(self):
        # XXX link to comments
        return []

    def isioc_topics(self):
        # XXX link to tags, folders?
        return []

class BlogEntryIPrevNextAdapter(EntityAdapter):
    __regid__ = 'IPrevNext'
    __select__ = EntityAdapter.__select__ & is_instance('BlogEntry')

    def next_entity(self):
        return self._sibling_entry('ASC', '>')

    def previous_entity(self):
        return self._sibling_entry('DESC', '<')

    def _sibling_entry(self, order, operator):
        if self.entity.entry_of:
            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._cw.execute(rql % (order, operator),
                                    {'blog': self.entity.entry_of[0].eid, 'eid': self.entity.eid})
            rql = ('Any B ORDERBY B %s LIMIT 1 '
                   'WHERE B is BlogEntry, B eid %s %%(eid)s')
            rset = self._cw.execute(rql % (order, operator), {'eid': self.entity.eid})
        if rset:
            return rset.get_entity(0,0)