"""entity classes for Link entities

:organization: Logilab
:copyright: 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
"""
__docformat__ = "restructuredtext en"

from cubicweb.entities import AnyEntity, fetch_config
from cubicweb.web.views.embedding import IEmbedableAdapter
from cubicweb.selectors import is_instance

class Link(AnyEntity):
    """customized class for Link entities"""
    __regid__ = 'Link'
    fetch_attrs, fetch_order = fetch_config(['title', 'url'])


    def dc_title(self):
        return u'%s (%s)' % (self.title, self.url)

    def actual_url(self):
        if not self.embed:
            return self.url
        return self._cw.build_url('embed', url=self.url, rql=self._cw.form.get('rql'))

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

    def embeded_url(self):
        """embed action interface"""
        return self.entity.url
