web/views/xbel.py
changeset 11057 0b59724cb3f2
parent 11052 058bb3dc685f
child 11058 23eb30449fe5
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
     1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    18 """xbel views"""
       
    19 
       
    20 __docformat__ = "restructuredtext en"
       
    21 from cubicweb import _
       
    22 
       
    23 from six.moves import range
       
    24 
       
    25 from logilab.mtconverter import xml_escape
       
    26 
       
    27 from cubicweb.predicates import is_instance
       
    28 from cubicweb.view import EntityView
       
    29 from cubicweb.web.views.xmlrss import XMLView
       
    30 
       
    31 
       
    32 class XbelView(XMLView):
       
    33     __regid__ = 'xbel'
       
    34     title = _('xbel export')
       
    35     templatable = False
       
    36     content_type = 'text/xml' #application/xbel+xml
       
    37 
       
    38     def cell_call(self, row, col):
       
    39         self.wview('xbelitem', self.cw_rset, row=row, col=col)
       
    40 
       
    41     def call(self):
       
    42         """display a list of entities by calling their <item_vid> view"""
       
    43         self.w(u'<?xml version="1.0" encoding="%s"?>\n' % self._cw.encoding)
       
    44         self.w(u'<!DOCTYPE xbel PUBLIC "+//IDN python.org//DTD XML Bookmark Exchange Language 1.0//EN//XML" "http://www.python.org/topics/xml/dtds/xbel-1.0.dtd">')
       
    45         self.w(u'<xbel version="1.0">')
       
    46         self.w(u'<title>%s</title>' % self._cw._('bookmarks'))
       
    47         for i in range(self.cw_rset.rowcount):
       
    48             self.cell_call(i, 0)
       
    49         self.w(u"</xbel>")
       
    50 
       
    51 
       
    52 class XbelItemView(EntityView):
       
    53     __regid__ = 'xbelitem'
       
    54 
       
    55     def cell_call(self, row, col):
       
    56         entity = self.cw_rset.complete_entity(row, col)
       
    57         self.w(u'<bookmark href="%s">' % xml_escape(self.url(entity)))
       
    58         self.w(u'  <title>%s</title>' % xml_escape(entity.dc_title()))
       
    59         self.w(u'</bookmark>')
       
    60 
       
    61     def url(self, entity):
       
    62         return entity.absolute_url()
       
    63 
       
    64 
       
    65 class XbelItemBookmarkView(XbelItemView):
       
    66     __select__ = is_instance('Bookmark')
       
    67 
       
    68     def url(self, entity):
       
    69         return entity.actual_url()