web/views/sparql.py
changeset 2422 96da7dc42eb5
child 2424 70f85df651a5
equal deleted inserted replaced
2418:8f06e4f02733 2422:96da7dc42eb5
       
     1 """SPARQL integration
       
     2 
       
     3 :organization: Logilab
       
     4 :copyright: 2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
       
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     6 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
       
     7 """
       
     8 import rql
       
     9 from yams import xy
       
    10 
       
    11 from cubicweb.view import StartupView
       
    12 from cubicweb.web import form, formfields, formwidgets as fwdgs
       
    13 from cubicweb.web.views import forms, urlrewrite
       
    14 from cubicweb.spa2rql import Sparql2rqlTranslator
       
    15 
       
    16 
       
    17 class SparqlForm(forms.FieldsForm):
       
    18     id = 'sparql'
       
    19     sparql = formfields.StringField(help=_('type here a sparql qyery'))
       
    20     vid = formfields.StringField(initial='sparql', widget=fwdgs.HiddenInput)
       
    21     form_buttons = [fwdgs.SubmitButton()]
       
    22     @property
       
    23     def action(self):
       
    24         return self.req.url()
       
    25 
       
    26 
       
    27 class SparqlFormView(form.FormViewMixIn, StartupView):
       
    28     id = 'sparql'
       
    29     def call(self):
       
    30         form = self.vreg.select('forms', 'sparql', self.req)
       
    31         self.w(form.form_render())
       
    32         sparql = self.req.form.get('sparql')
       
    33         if sparql:
       
    34             try:
       
    35                 qi = Sparql2rqlTranslator(self.schema).translate(sparql)
       
    36                 rset = self.req.execute(qi.finalize())
       
    37             except rql.TypeResolverException, ex:
       
    38                 self.w(self.req._('can not resolve entity types:') + u' ' + unicode('ex'))
       
    39             except UnsupportedQuery:
       
    40                 self.w(self.req._('we are not yet ready to handle this query'))
       
    41             except xy.UnsupportedVocabulary, ex:
       
    42                 self.w(self.req._('unknown vocabulary:') + u' ' + unicode('ex'))
       
    43             self.wview('table', rset, 'null')