Skip to content
Snippets Groups Projects
views.py 2.84 KiB
Newer Older
Nicolas Chauvat's avatar
Nicolas Chauvat committed
"""Specific views for links

:organization: Logilab
sylvain thenault's avatar
sylvain thenault committed
:copyright: 2003-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
Nicolas Chauvat's avatar
Nicolas Chauvat committed
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
"""
__docformat__ = "restructuredtext en"
_ = unicode

from logilab.mtconverter import xml_escape
from cubicweb.selectors import is_instance
sylvain thenault's avatar
sylvain thenault committed
from cubicweb.view import EntityView
sylvain thenault's avatar
sylvain thenault committed
from cubicweb.web import uicfg, formwidgets
from cubicweb.web.views import primary, baseviews, xbel, bookmark, embedding

class LinkIEmbedableAdapter(embedding.IEmbedableAdapter):
    __select__ = is_instance('Link')

    def embeded_url(self):
        """embed action interface"""
        return self.entity.url
sylvain thenault's avatar
sylvain thenault committed
for attr in ('title', 'url', 'embed', 'description'):
sylvain thenault's avatar
sylvain thenault committed
    uicfg.primaryview_section.tag_attribute(('Link', attr), 'hidden')
sylvain thenault's avatar
sylvain thenault committed

sylvain thenault's avatar
sylvain thenault committed
uicfg.autoform_field_kwargs.tag_attribute(('Link', 'url'),
                                          {'widget': formwidgets.TextInput})
sylvain thenault's avatar
sylvain thenault committed

sylvain thenault's avatar
sylvain thenault committed

class LinkPrimaryView(primary.PrimaryView):
    __select__ = is_instance('Link')
Nicolas Chauvat's avatar
Nicolas Chauvat committed
    show_attr_label = False

    def render_entity_title(self, entity):
        title = u'<a href="%s">%s</a>' % (xml_escape(entity.actual_url()),
                                          xml_escape(entity.title))
        self.w(u'<h1><span class="etype">%s</span> %s</h1>'
               % (entity.dc_type().capitalize(), title))
Nicolas Chauvat's avatar
Nicolas Chauvat committed

    def summary(self, entity):
        return entity.view('reledit', rtype='description')
sylvain thenault's avatar
sylvain thenault committed

sylvain thenault's avatar
sylvain thenault committed

sylvain thenault's avatar
sylvain thenault committed
class LinkOneLineView(baseviews.OneLineView):
    __select__ = is_instance('Link')
sylvain thenault's avatar
sylvain thenault committed

Nicolas Chauvat's avatar
Nicolas Chauvat committed
    def cell_call(self, row, col):
Sandrine Ribeau's avatar
Sandrine Ribeau committed
        entity = self.cw_rset.complete_entity(row, col)
Nicolas Chauvat's avatar
Nicolas Chauvat committed
        descr = entity.printable_value('description', format='text/plain')
        descr = descr and descr.splitlines()[0]
        values = {'title': xml_escape(entity.title),
                  'url': xml_escape(entity.absolute_url()),
                  'description': xml_escape(descr),
Nicolas Chauvat's avatar
Nicolas Chauvat committed
                  }
        self.w(u'<a href="%(url)s" title="%(description)s">%(title)s</a>'
               % values)
        self.w(u'&nbsp;[<a href="%s">%s</a>]'
               % (xml_escape(entity.actual_url()),
Sandrine Ribeau's avatar
Sandrine Ribeau committed
                  self._cw._('follow')))
sylvain thenault's avatar
sylvain thenault committed
class LinkView(EntityView):
Sandrine Ribeau's avatar
Sandrine Ribeau committed
    __regid__ = 'link'
    __select__ = is_instance('Link')
Nicolas Chauvat's avatar
Nicolas Chauvat committed
    title = _('link')
sylvain thenault's avatar
sylvain thenault committed

Nicolas Chauvat's avatar
Nicolas Chauvat committed
    def cell_call(self, row, col):
Sandrine Ribeau's avatar
Sandrine Ribeau committed
        entity = self.cw_rset.complete_entity(row, col)
        values = {'title': xml_escape(entity.title),
                  'url': xml_escape(entity.actual_url()),
                  'description': xml_escape(entity.printable_value('description')),
Nicolas Chauvat's avatar
Nicolas Chauvat committed
                  }
        self.w(u'<a href="%(url)s" title="%(description)s">%(title)s</a>'
               % values)

sylvain thenault's avatar
sylvain thenault committed
class XbelItemLinkView(xbel.XbelItemView):
    __select__ = is_instance('Link')
Nicolas Chauvat's avatar
Nicolas Chauvat committed

    def url(self, entity):
        return entity.url


sylvain thenault's avatar
sylvain thenault committed
class LinkFollowAction(bookmark.FollowAction):
    __select__ = is_instance('Link')