possibility to choose results format, set a nicer file name on sparqlxml view
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 22 Jul 2009 19:42:10 +0200
changeset 2426 0209d9bc45a8
parent 2425 ba898e4ba09a
child 2427 3e0ef847a546
possibility to choose results format, set a nicer file name on sparqlxml view
web/views/sparql.py
--- a/web/views/sparql.py	Wed Jul 22 19:41:33 2009 +0200
+++ b/web/views/sparql.py	Wed Jul 22 19:42:10 2009 +0200
@@ -5,7 +5,7 @@
 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
 """
-__docformat__ = "restructuredtext en"
+v__docformat__ = "restructuredtext en"
 
 import rql
 from yams import xy
@@ -14,7 +14,7 @@
 from lxml.builder import E
 
 from cubicweb.view import StartupView, AnyRsetView
-from cubicweb.web import form, formfields, formwidgets as fwdgs
+from cubicweb.web import Redirect, form, formfields, formwidgets as fwdgs
 from cubicweb.web.views import forms, urlrewrite
 from cubicweb.spa2rql import Sparql2rqlTranslator
 
@@ -22,7 +22,10 @@
 class SparqlForm(forms.FieldsForm):
     id = 'sparql'
     sparql = formfields.StringField(help=_('type here a sparql qyery'))
-    vid = formfields.StringField(initial='sparql', widget=fwdgs.HiddenInput)
+    resultvid = formfields.StringField(choices=((_('table'), 'table'),
+                                                (_('sparql xml'), 'sparqlxml')),
+                                       widget=fwdgs.Radio,
+                                       initial='table')
     form_buttons = [fwdgs.SubmitButton()]
     @property
     def action(self):
@@ -35,17 +38,22 @@
         form = self.vreg.select('forms', 'sparql', self.req)
         self.w(form.form_render())
         sparql = self.req.form.get('sparql')
+        vid = self.req.form.get('resultvid', 'table')
         if sparql:
             try:
-                qi = Sparql2rqlTranslator(self.schema).translate(sparql)
-                rset = self.req.execute(qi.finalize())
+                qinfo = Sparql2rqlTranslator(self.schema).translate(sparql)
             except rql.TypeResolverException, ex:
                 self.w(self.req._('can not resolve entity types:') + u' ' + unicode('ex'))
             except UnsupportedQuery:
                 self.w(self.req._('we are not yet ready to handle this query'))
             except xy.UnsupportedVocabulary, ex:
                 self.w(self.req._('unknown vocabulary:') + u' ' + unicode('ex'))
-            self.wview('table', rset, 'null')
+            if vid == 'sparqlxml':
+                url = self.build_url('view', rql=qinfo.finalize(), vid=vid)
+                raise Redirect(url)
+            rset = self.req.execute(qinfo.finalize())
+            self.wview(vid, rset, 'null')
+
 
 ## sparql resultset views #####################################################
 
@@ -70,10 +78,9 @@
 class SparqlResultXmlView(AnyRsetView):
     """The spec can be found here: http://www.w3.org/TR/rdf-sparql-XMLres/
     """
-    id = 'sparql'
+    id = 'sparqlxml'
     content_type = 'application/sparql-results+xml'
     templatable = False
-    # XXX use accept-headers-selectors to choose among the sparql result views
 
     def call(self):
         # XXX handle UNION
@@ -88,7 +95,7 @@
         sparql = E.sparql(E.head(*(E.variable(name=name) for name in varnames)),
                           results)
         self.w(u'<?xml version="1.0"?>\n')
-        self.w(etree.tostring(sparql, encoding=unicode))
+        self.w(etree.tostring(sparql, encoding=unicode, pretty_print=True))
 
     def cell_binding(self, row, col, varname):
         celltype = self.rset.description[row][col]
@@ -100,3 +107,9 @@
         else:
             entity = self.entity(row, col)
             return E.binding(E.uri(entity.absolute_url()), name=varname)
+
+    def set_request_content_type(self):
+        """overriden to set the correct filetype and filename"""
+        self.req.set_content_type(self.content_type,
+                                  filename='sparql.xml',
+                                  encoding=self.req.encoding)