new rsetxml view, reusing most code from csvexport view
authorSylvain Thenault <sylvain.thenault@logilab.fr>
Thu, 08 Jan 2009 12:39:46 +0100
changeset 369 c8a6edc224bb
parent 368 84a5106840fa
child 370 7e76f651314b
new rsetxml view, reusing most code from csvexport view
common/view.py
cwconfig.py
web/views/baseviews.py
--- a/common/view.py	Thu Jan 08 12:12:40 2009 +0100
+++ b/common/view.py	Thu Jan 08 12:39:46 2009 +0100
@@ -383,6 +383,27 @@
     __selectors__ = (nonempty_rset,)
     
     category = 'anyrsetview'
+
+    def display_value(self, etype, val):
+        if val is not None and not self.schema.eschema(etype).is_final():
+            # csvrow.append(val) # val is eid in that case
+            return self.view('textincontext', self.rset, 
+                             row=rowindex, col=colindex)
+        return self.view('final', self.rset, displaytime=True,
+                         row=rowindex, col=colindex)
+    
+    def columns_labels(self):
+        rqlstdescr = self.rset.syntax_tree().get_description()[0] # XXX missing Union support
+        labels = []
+        for colindex, attr in enumerate(rqlstdescr):
+            # compute column header
+            if colindex == 0 or attr == 'Any': # find a better label
+                label = ','.join(display_name(self.req, et)
+                                 for et in self.rset.column_types(colindex))
+            else:
+                label = display_name(self.req, attr)
+            labels.append(label)
+        return labels
     
 
 class EmptyRsetView(View):
--- a/cwconfig.py	Thu Jan 08 12:12:40 2009 +0100
+++ b/cwconfig.py	Thu Jan 08 12:39:46 2009 +0100
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
 """common configuration utilities for cubicweb
 
 :organization: Logilab
@@ -164,7 +165,7 @@
         # pyro name server
         ('pyro-ns-host',
          {'type' : 'string',
-          'default': '',
+          'default': u'hpoô',
           'help': 'Pyro name server\'s host. If not set, will be detected by a \
 broadcast query',
           'group': 'pyro-name-server', 'inputlevel': 1,
--- a/web/views/baseviews.py	Thu Jan 08 12:12:40 2009 +0100
+++ b/web/views/baseviews.py	Thu Jan 08 12:39:46 2009 +0100
@@ -607,6 +607,32 @@
         self.w(u'</%s>\n' % (entity.e_schema))
 
 
+    
+class XMLRsetView(AnyRsetView):
+    """dumps xml in CSV"""
+    id = 'rsetxml'
+    title = _('xml export')
+    templatable = False
+    content_type = 'text/xml'
+    xml_root = 'rset'
+        
+    def call(self):
+        w = self.w
+        descr = self.rset.description
+        labels = self.columns_labels()
+        w(u'<?xml version="1.0" encoding="%s"?>\n' % self.req.encoding)
+        w(u'<%s>\n' % self.xml_root)
+        for rowindex, row in enumerate(self.rset):
+            w(u' <row>\n')
+            for colindex, val in enumerate(row):
+                content = self.display_value(descr[rowindex][colindex], val)
+                tag = labels[colindex]
+                w(u' <%s>%s<%s>\n' % (tag, content, tag)
+            writer.writerow(csvrow)
+            w(u' </row>\n')
+        w(u'</%s>\n' % self.xml_root)
+    
+
 class RssView(XmlView):
     id = 'rss'
     title = _('rss')
@@ -697,35 +723,15 @@
         
     def call(self):
         writer = self.csvwriter()
-        writer.writerow(self.get_headers_labels())
+        writer.writerow(self.columns_labels())
         descr = self.rset.description
         for rowindex, row in enumerate(self.rset):
             csvrow = []
             for colindex, val in enumerate(row):
-                etype = descr[rowindex][colindex]
-                if val is not None and not self.schema.eschema(etype).is_final():
-                    # csvrow.append(val) # val is eid in that case
-                    content = self.view('textincontext', self.rset, 
-                                        row=rowindex, col=colindex)
-                else:
-                    content = self.view('final', self.rset, displaytime=True,
-                                        row=rowindex, col=colindex)
+                content = self.display_value(descr[rowindex][colindex], val)
                 csvrow.append(content)                    
             writer.writerow(csvrow)
     
-    def get_headers_labels(self):
-        rqlstdescr = self.rset.syntax_tree().get_description()[0] # XXX missing Union support
-        labels = []
-        for colindex, attr in enumerate(rqlstdescr):
-            # compute column header
-            if colindex == 0 or attr == 'Any': # find a better label
-                label = ','.join(display_name(self.req, et)
-                                 for et in self.rset.column_types(colindex))
-            else:
-                label = display_name(self.req, attr)
-            labels.append(label)
-        return labels
-
     
 class CSVEntityView(CSVMixIn, EntityView):
     """dumps rset's entities (with full set of attributes) in CSV"""