web/views/xmlrss.py
branchtls-sprint
changeset 1477 b056a49c16dc
parent 1135 00eb43688a57
child 1550 8260e857822b
equal deleted inserted replaced
1476:f94b41709ce6 1477:b056a49c16dc
    28     title = _('xml')
    28     title = _('xml')
    29     templatable = False
    29     templatable = False
    30     content_type = 'text/xml'
    30     content_type = 'text/xml'
    31     xml_root = 'rset'
    31     xml_root = 'rset'
    32     item_vid = 'xmlitem'
    32     item_vid = 'xmlitem'
    33     
    33 
    34     def cell_call(self, row, col):
    34     def cell_call(self, row, col):
    35         self.wview(self.item_vid, self.rset, row=row, col=col)
    35         self.wview(self.item_vid, self.rset, row=row, col=col)
    36         
    36 
    37     def call(self):
    37     def call(self):
    38         """display a list of entities by calling their <item_vid> view"""
    38         """display a list of entities by calling their <item_vid> view"""
    39         self.w(u'<?xml version="1.0" encoding="%s"?>\n' % self.req.encoding)
    39         self.w(u'<?xml version="1.0" encoding="%s"?>\n' % self.req.encoding)
    40         self.w(u'<%s size="%s">\n' % (self.xml_root, len(self.rset)))
    40         self.w(u'<%s size="%s">\n' % (self.xml_root, len(self.rset)))
    41         for i in xrange(self.rset.rowcount):
    41         for i in xrange(self.rset.rowcount):
    64                 elif isinstance(value, basestring):
    64                 elif isinstance(value, basestring):
    65                     value = xml_escape(value)
    65                     value = xml_escape(value)
    66                 self.w(u'  <%s>%s</%s>\n' % (attr, value, attr))
    66                 self.w(u'  <%s>%s</%s>\n' % (attr, value, attr))
    67         self.w(u'</%s>\n' % (entity.e_schema))
    67         self.w(u'</%s>\n' % (entity.e_schema))
    68 
    68 
    69     
    69 
    70 class XmlRsetView(AnyRsetView):
    70 class XmlRsetView(AnyRsetView):
    71     """dumps raw rset as xml"""
    71     """dumps raw rset as xml"""
    72     id = 'rsetxml'
    72     id = 'rsetxml'
    73     title = _('xml export')
    73     title = _('xml export')
    74     templatable = False
    74     templatable = False
    75     content_type = 'text/xml'
    75     content_type = 'text/xml'
    76     xml_root = 'rset'
    76     xml_root = 'rset'
    77         
    77 
    78     def call(self):
    78     def call(self):
    79         w = self.w
    79         w = self.w
    80         rset, descr = self.rset, self.rset.description
    80         rset, descr = self.rset, self.rset.description
    81         eschema = self.schema.eschema
    81         eschema = self.schema.eschema
    82         labels = self.columns_labels(False)
    82         labels = self.columns_labels(False)
   101                                     row=rowindex, col=colindex, format='text/plain')
   101                                     row=rowindex, col=colindex, format='text/plain')
   102                 w(simple_sgml_tag(tag, val, **attrs))
   102                 w(simple_sgml_tag(tag, val, **attrs))
   103             w(u' </row>\n')
   103             w(u' </row>\n')
   104         w(u'</%s>\n' % self.xml_root)
   104         w(u'</%s>\n' % self.xml_root)
   105 
   105 
   106     
   106 
   107 # RSS stuff ###################################################################
   107 # RSS stuff ###################################################################
   108 
   108 
   109 class RSSFeedURL(Component):
   109 class RSSFeedURL(Component):
   110     id = 'rss_feed_url'
   110     id = 'rss_feed_url'
   111     __select__ = non_final_entity()
   111     __select__ = non_final_entity()
   112     
   112 
   113     def feed_url(self):
   113     def feed_url(self):
   114         return self.build_url(rql=self.limited_rql(), vid='rss')
   114         return self.build_url(rql=self.limited_rql(), vid='rss')
   115 
   115 
   116 
   116 
   117 class RSSEntityFeedURL(Component):
   117 class RSSEntityFeedURL(Component):
   118     id = 'rss_feed_url'
   118     id = 'rss_feed_url'
   119     __select__ = non_final_entity() & one_line_rset()
   119     __select__ = non_final_entity() & one_line_rset()
   120     
   120 
   121     def feed_url(self):
   121     def feed_url(self):
   122         return self.entity(0, 0).rss_feed_url()
   122         return self.entity(0, 0).rss_feed_url()
   123 
   123 
   124         
   124 
   125 class RSSIconBox(BoxTemplate):
   125 class RSSIconBox(BoxTemplate):
   126     """just display the RSS icon on uniform result set"""
   126     """just display the RSS icon on uniform result set"""
   127     id = 'rss'
   127     id = 'rss'
   128     __select__ = (BoxTemplate.__select__
   128     __select__ = (BoxTemplate.__select__
   129                   & appobject_selectable('components', 'rss_feed_url'))
   129                   & appobject_selectable('components', 'rss_feed_url'))
   130     
   130 
   131     visible = False
   131     visible = False
   132     order = 999
   132     order = 999
   133     
   133 
   134     def call(self, **kwargs):
   134     def call(self, **kwargs):
   135         try:
   135         try:
   136             rss = self.req.external_resource('RSS_LOGO')
   136             rss = self.req.external_resource('RSS_LOGO')
   137         except KeyError:
   137         except KeyError:
   138             self.error('missing RSS_LOGO external resource')
   138             self.error('missing RSS_LOGO external resource')
   146     id = 'rss'
   146     id = 'rss'
   147     title = _('rss')
   147     title = _('rss')
   148     templatable = False
   148     templatable = False
   149     content_type = 'text/xml'
   149     content_type = 'text/xml'
   150     http_cache_manager = MaxAgeHTTPCacheManager
   150     http_cache_manager = MaxAgeHTTPCacheManager
   151     cache_max_age = 60*60*2 # stay in http cache for 2 hours by default 
   151     cache_max_age = 60*60*2 # stay in http cache for 2 hours by default
   152     
   152 
   153     def cell_call(self, row, col):
   153     def _open(self):
   154         self.wview('rssitem', self.rset, row=row, col=col)
   154         req = self.req
   155         
   155         self.w(u'<?xml version="1.0" encoding="%s"?>\n' % req.encoding)
       
   156         self.w(u'<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">\n')
       
   157         self.w(u'  <channel>\n')
       
   158         self.w(u'    <title>%s RSS Feed</title>\n' % html_escape(self.page_title()))
       
   159         self.w(u'    <description>%s</description>\n' % html_escape(req.form.get('vtitle', '')))
       
   160         params = req.form.copy()
       
   161         params.pop('vid', None)
       
   162         self.w(u'    <link>%s</link>\n' % html_escape(self.build_url(**params)))
       
   163 
       
   164     def _close(self):
       
   165         self.w(u'  </channel>\n')
       
   166         self.w(u'</rss>')
       
   167 
   156     def call(self):
   168     def call(self):
   157         """display a list of entities by calling their <item_vid> view"""
   169         """display a list of entities by calling their <item_vid> view"""
   158         req = self.req
   170         self._open()
   159         self.w(u'<?xml version="1.0" encoding="%s"?>\n' % req.encoding)
       
   160         self.w(u'''<rdf:RDF
       
   161  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
       
   162  xmlns:dc="http://purl.org/dc/elements/1.1/"
       
   163  xmlns="http://purl.org/rss/1.0/"
       
   164 >''')
       
   165         self.w(u'  <channel rdf:about="%s">\n' % xml_escape(req.url()))
       
   166         self.w(u'    <title>%s RSS Feed</title>\n' % xml_escape(self.page_title()))
       
   167         self.w(u'    <description>%s</description>\n' % xml_escape(req.form.get('vtitle', '')))
       
   168         params = req.form.copy()
       
   169         params.pop('vid', None)
       
   170         self.w(u'    <link>%s</link>\n' % xml_escape(self.build_url(**params)))
       
   171         self.w(u'    <items>\n')
       
   172         self.w(u'      <rdf:Seq>\n')
       
   173         for entity in self.rset.entities():
       
   174             self.w(u'      <rdf:li resource="%s" />\n' % xml_escape(entity.absolute_url()))
       
   175         self.w(u'      </rdf:Seq>\n')
       
   176         self.w(u'    </items>\n')
       
   177         self.w(u'  </channel>\n')
       
   178         for i in xrange(self.rset.rowcount):
   171         for i in xrange(self.rset.rowcount):
   179             self.cell_call(i, 0)
   172             self.cell_call(i, 0)
   180         self.w(u'</rdf:RDF>')
   173         self._close()
   181 
   174 
       
   175     def cell_call(self, row, col):
       
   176         self.wview('rssitem', self.rset, row=row, col=col)
   182 
   177 
   183 class RssItemView(EntityView):
   178 class RssItemView(EntityView):
   184     id = 'rssitem'
   179     id = 'rssitem'
   185     date_format = '%%Y-%%m-%%dT%%H:%%M%+03i:00' % (timezone / 3600)
   180     date_format = '%%Y-%%m-%%dT%%H:%%M%+03i:00' % (timezone / 3600)
       
   181     add_div_section = False
   186 
   182 
   187     def cell_call(self, row, col):
   183     def cell_call(self, row, col):
   188         entity = self.complete_entity(row, col)
   184         entity = self.complete_entity(row, col)
   189         self.w(u'<item rdf:about="%s">\n' % xml_escape(entity.absolute_url()))
   185         self.w(u'<item>\n')
       
   186         self.w(u'<guid isPermaLink="true">%s</guid>\n' % html_escape(entity.absolute_url()))
       
   187         self.render_title_link(entity)
       
   188         self._marker('description', html_escape(entity.dc_description()))
       
   189         self._marker('dc:date', entity.dc_date(self.date_format))
       
   190         self.render_entity_creator(entity)
       
   191         self.w(u'</item>\n')
       
   192 
       
   193     def render_title_link(self, entity):
   190         self._marker('title', entity.dc_long_title())
   194         self._marker('title', entity.dc_long_title())
   191         self._marker('link', entity.absolute_url())
   195         self._marker('link', entity.absolute_url())
   192         self._marker('description', entity.dc_description())
   196 
   193         self._marker('dc:date', entity.dc_date(self.date_format))
   197     def render_entity_creator(self, entity):
   194         if entity.creator:
   198         if entity.creator:
   195             self.w(u'<author>')
   199             self._marker('dc:creator', entity.dc_creator())
   196             self._marker('name', entity.creator.name())
   200 
   197             email = entity.creator.get_email()
   201 
   198             if email:
       
   199                 self._marker('email', email)
       
   200             self.w(u'</author>')
       
   201         self.w(u'</item>\n')
       
   202         
       
   203     def _marker(self, marker, value):
   202     def _marker(self, marker, value):
   204         if value:
   203         if value:
   205             self.w(u'  <%s>%s</%s>\n' % (marker, xml_escape(value), marker))
   204             self.w(u'  <%s>%s</%s>\n' % (marker, html_escape(value), marker))