"""base xml and rss views:organization: Logilab:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr"""__docformat__="restructuredtext en"fromtimeimporttimezonefromlogilab.mtconverterimportxml_escapefromcubicweb.viewimportEntityView,AnyRsetViewfromcubicweb.web.httpcacheimportMaxAgeHTTPCacheManagerfromcubicweb.common.uilibimportsimple_sgml_tagclassXmlView(EntityView):"""xml view for entities"""id='xml'title=_('xml')templatable=Falsecontent_type='text/xml'xml_root='rset'item_vid='xmlitem'defcell_call(self,row,col):self.wview(self.item_vid,self.rset,row=row,col=col)defcall(self):"""display a list of entities by calling their <item_vid> view"""self.w(u'<?xml version="1.0" encoding="%s"?>\n'%self.req.encoding)self.w(u'<%s size="%s">\n'%(self.xml_root,len(self.rset)))foriinxrange(self.rset.rowcount):self.cell_call(i,0)self.w(u'</%s>\n'%self.xml_root)classXmlItemView(EntityView):id='xmlitem'defcell_call(self,row,col):""" element as an item for an xml feed """entity=self.complete_entity(row,col)self.w(u'<%s>\n'%(entity.e_schema))forrschema,attrschemainentity.e_schema.attribute_definitions():attr=rschema.typetry:value=entity[attr]exceptKeyError:# BytescontinueifvalueisnotNone:ifattrschema=='Bytes':frombase64importb64encodevalue='<![CDATA[%s]]>'%b64encode(value.getvalue())elifisinstance(value,basestring):value=xml_escape(value)self.w(u' <%s>%s</%s>\n'%(attr,value,attr))self.w(u'</%s>\n'%(entity.e_schema))classXmlRsetView(AnyRsetView):"""dumps raw rset as xml"""id='rsetxml'title=_('xml export')templatable=Falsecontent_type='text/xml'xml_root='rset'defcall(self):w=self.wrset,descr=self.rset,self.rset.descriptioneschema=self.schema.eschemalabels=self.columns_labels(False)w(u'<?xml version="1.0" encoding="%s"?>\n'%self.req.encoding)w(u'<%s query="%s">\n'%(self.xml_root,xml_escape(rset.printable_rql())))forrowindex,rowinenumerate(self.rset):w(u' <row>\n')forcolindex,valinenumerate(row):etype=descr[rowindex][colindex]tag=labels[colindex]attrs={}if'('intag:attrs['expr']=tagtag='funccall'ifvalisnotNoneandnoteschema(etype).is_final():attrs['eid']=val# csvrow.append(val) # val is eid in that caseval=self.view('textincontext',rset,row=rowindex,col=colindex)else:val=self.view('final',rset,displaytime=True,row=rowindex,col=colindex,format='text/plain')w(simple_sgml_tag(tag,val,**attrs))w(u' </row>\n')w(u'</%s>\n'%self.xml_root)classRssView(XmlView):id='rss'title=_('rss')templatable=Falsecontent_type='text/xml'http_cache_manager=MaxAgeHTTPCacheManagercache_max_age=60*60*2# stay in http cache for 2 hours by default defcell_call(self,row,col):self.wview('rssitem',self.rset,row=row,col=col)defcall(self):"""display a list of entities by calling their <item_vid> view"""req=self.reqself.w(u'<?xml version="1.0" encoding="%s"?>\n'%req.encoding)self.w(u'''<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns="http://purl.org/rss/1.0/">''')self.w(u' <channel rdf:about="%s">\n'%xml_escape(req.url()))self.w(u' <title>%s RSS Feed</title>\n'%xml_escape(self.page_title()))self.w(u' <description>%s</description>\n'%xml_escape(req.form.get('vtitle','')))params=req.form.copy()params.pop('vid',None)self.w(u' <link>%s</link>\n'%xml_escape(self.build_url(**params)))self.w(u' <items>\n')self.w(u' <rdf:Seq>\n')forentityinself.rset.entities():self.w(u' <rdf:li resource="%s" />\n'%xml_escape(entity.absolute_url()))self.w(u' </rdf:Seq>\n')self.w(u' </items>\n')self.w(u' </channel>\n')foriinxrange(self.rset.rowcount):self.cell_call(i,0)self.w(u'</rdf:RDF>')classRssItemView(EntityView):id='rssitem'date_format='%%Y-%%m-%%dT%%H:%%M%+03i:00'%(timezone/3600)defcell_call(self,row,col):entity=self.complete_entity(row,col)self.w(u'<item rdf:about="%s">\n'%xml_escape(entity.absolute_url()))self._marker('title',entity.dc_long_title())self._marker('link',entity.absolute_url())self._marker('description',entity.dc_description())self._marker('dc:date',entity.dc_date(self.date_format))ifentity.creator:self.w(u'<author>')self._marker('name',entity.creator.name())email=entity.creator.get_email()ifemail:self._marker('email',email)self.w(u'</author>')self.w(u'</item>\n')def_marker(self,marker,value):ifvalue:self.w(u' <%s>%s</%s>\n'%(marker,xml_escape(value),marker))