web/views/sparql.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 22 Jul 2009 18:35:08 +0200
changeset 2422 96da7dc42eb5
child 2424 70f85df651a5
permissions -rw-r--r--
quick and dirty support from simple sparql queries + base ui

"""SPARQL integration

:organization: Logilab
:copyright: 2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
"""
import rql
from yams import xy

from cubicweb.view import StartupView
from cubicweb.web import form, formfields, formwidgets as fwdgs
from cubicweb.web.views import forms, urlrewrite
from cubicweb.spa2rql import Sparql2rqlTranslator


class SparqlForm(forms.FieldsForm):
    id = 'sparql'
    sparql = formfields.StringField(help=_('type here a sparql qyery'))
    vid = formfields.StringField(initial='sparql', widget=fwdgs.HiddenInput)
    form_buttons = [fwdgs.SubmitButton()]
    @property
    def action(self):
        return self.req.url()


class SparqlFormView(form.FormViewMixIn, StartupView):
    id = 'sparql'
    def call(self):
        form = self.vreg.select('forms', 'sparql', self.req)
        self.w(form.form_render())
        sparql = self.req.form.get('sparql')
        if sparql:
            try:
                qi = Sparql2rqlTranslator(self.schema).translate(sparql)
                rset = self.req.execute(qi.finalize())
            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')