web/views/xmlrss.py
changeset 6959 037a0277db0a
parent 6864 ea95004494a2
child 6960 822f2530570d
equal deleted inserted replaced
6958:861251f125cf 6959:037a0277db0a
     1 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     3 #
     3 #
     4 # This file is part of CubicWeb.
     4 # This file is part of CubicWeb.
     5 #
     5 #
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
    18 """base xml and rss views"""
    18 """base xml and rss views"""
    19 
    19 
    20 __docformat__ = "restructuredtext en"
    20 __docformat__ = "restructuredtext en"
    21 _ = unicode
    21 _ = unicode
    22 
    22 
       
    23 from base64 import b64encode
    23 from time import timezone
    24 from time import timezone
    24 
    25 
    25 from logilab.mtconverter import xml_escape
    26 from logilab.mtconverter import xml_escape
    26 
    27 
    27 from cubicweb.selectors import (is_instance, non_final_entity, one_line_rset,
    28 from cubicweb.selectors import (is_instance, non_final_entity, one_line_rset,
    29 from cubicweb.view import EntityView, EntityAdapter, AnyRsetView, Component
    30 from cubicweb.view import EntityView, EntityAdapter, AnyRsetView, Component
    30 from cubicweb.view import implements_adapter_compat
    31 from cubicweb.view import implements_adapter_compat
    31 from cubicweb.uilib import simple_sgml_tag
    32 from cubicweb.uilib import simple_sgml_tag
    32 from cubicweb.web import httpcache, component
    33 from cubicweb.web import httpcache, component
    33 
    34 
       
    35 def encode_bytes(value):
       
    36     return '<![CDATA[%s]]>' % b64encode(value.getvalue())
       
    37 
       
    38 # see cubicweb.sobjects.parser.DEFAULT_CONVERTERS
       
    39 SERIALIZERS = {
       
    40     'String': xml_escape,
       
    41     'Bytes': encode_bytes,
       
    42     'Date': lambda x: x.strftime('%Y-%m-%d'),
       
    43     'Datetime': lambda x: x.strftime('%Y-%m-%d %H:%M:%S'),
       
    44     'Time': lambda x: x.strftime('%H:%M:%S'),
       
    45     'Interval': lambda x: x.days * 60*60*24 + x.seconds,
       
    46     }
    34 
    47 
    35 # base xml views ##############################################################
    48 # base xml views ##############################################################
    36 
    49 
    37 class XMLView(EntityView):
    50 class XMLView(EntityView):
    38     """xml view for entities"""
    51     """xml view for entities"""
    59     __regid__ = 'xmlitem'
    72     __regid__ = 'xmlitem'
    60 
    73 
    61     def cell_call(self, row, col):
    74     def cell_call(self, row, col):
    62         """ element as an item for an xml feed """
    75         """ element as an item for an xml feed """
    63         entity = self.cw_rset.complete_entity(row, col)
    76         entity = self.cw_rset.complete_entity(row, col)
    64         self.w(u'<%s>\n' % (entity.e_schema))
    77         self.w(u'<%s eid="%s" cwuri="%s">\n'
       
    78                % (entity.e_schema, entity.eid, xml_escape(entity.cwuri)))
    65         for rschema, attrschema in entity.e_schema.attribute_definitions():
    79         for rschema, attrschema in entity.e_schema.attribute_definitions():
    66             attr = rschema.type
    80             attr = rschema.type
    67             if attr == 'eid':
    81             if attr in ('eid', 'cwuri'):
    68                 value = entity.eid
    82                 continue
    69             else:
    83             else:
    70                 try:
    84                 try:
    71                     value = entity.cw_attr_cache[attr]
    85                     value = entity.cw_attr_cache[attr]
    72                 except KeyError:
    86                 except KeyError:
    73                     # Bytes
    87                     # Bytes
    74                     continue
    88                     continue
    75             if value is not None:
    89             if value is None:
    76                 if attrschema == 'Bytes':
    90                 self.w(u'  <%s/>\n' % attr)
    77                     from base64 import b64encode
    91             else:
    78                     value = '<![CDATA[%s]]>' % b64encode(value.getvalue())
    92                 try:
    79                 elif isinstance(value, basestring):
    93                     value = SERIALIZERS[attrschema](value)
    80                     value = xml_escape(value)
    94                 except KeyError:
       
    95                     pass
    81                 self.w(u'  <%s>%s</%s>\n' % (attr, value, attr))
    96                 self.w(u'  <%s>%s</%s>\n' % (attr, value, attr))
       
    97         for relstr in self._cw.list_form_param('relation'):
       
    98             try:
       
    99                 rtype, role = relstr.split('-')
       
   100             except ValueError:
       
   101                 self.error('badly formated relation name %r', relstr)
       
   102                 continue
       
   103             if role == 'subject':
       
   104                 getrschema = entity.e_schema.subjrels
       
   105             elif role == 'object':
       
   106                 getrschema = entity.e_schema.objrels
       
   107             else:
       
   108                 self.error('badly formated relation name %r', relstr)
       
   109                 continue
       
   110             if not rtype in getrschema:
       
   111                 self.error('unexisting relation %r', relstr)
       
   112                 continue
       
   113             self.w(u'  <%s role="%s">\n' % (rtype, role))
       
   114             for related in entity.related(rtype, role, entities=True):
       
   115                 self.w(u'    <%s eid="%s" cwuri="%s"/>\n'
       
   116                        % (related.e_schema, related.eid,
       
   117                           xml_escape(related.cwuri)))
       
   118             self.w(u'  </%s>\n' % rtype)
    82         self.w(u'</%s>\n' % (entity.e_schema))
   119         self.w(u'</%s>\n' % (entity.e_schema))
    83 
   120 
    84 
   121 
    85 class XMLRsetView(AnyRsetView):
   122 class XMLRsetView(AnyRsetView):
    86     """dumps raw rset as xml"""
   123     """dumps raw rset as xml"""