web/views/sparql.py
changeset 3451 6b46d73823f5
parent 3377 dd9d292b6a6d
child 3460 e4843535db25
equal deleted inserted replaced
3448:495862266785 3451:6b46d73823f5
    30                                        widget=fwdgs.Radio,
    30                                        widget=fwdgs.Radio,
    31                                        initial='table')
    31                                        initial='table')
    32     form_buttons = [fwdgs.SubmitButton()]
    32     form_buttons = [fwdgs.SubmitButton()]
    33     @property
    33     @property
    34     def action(self):
    34     def action(self):
    35         return self.req.url()
    35         return self._cw.url()
    36 
    36 
    37 
    37 
    38 class SparqlFormView(form.FormViewMixIn, StartupView):
    38 class SparqlFormView(form.FormViewMixIn, StartupView):
    39     __regid__ = 'sparql'
    39     __regid__ = 'sparql'
    40     def call(self):
    40     def call(self):
    41         form = self.vreg.select('forms', 'sparql', self.req)
    41         form = self._cw.vreg.select('forms', 'sparql', self._cw)
    42         self.w(form.form_render())
    42         self.w(form.form_render())
    43         sparql = self.req.form.get('sparql')
    43         sparql = self._cw.form.get('sparql')
    44         vid = self.req.form.get('resultvid', 'table')
    44         vid = self._cw.form.get('resultvid', 'table')
    45         if sparql:
    45         if sparql:
    46             try:
    46             try:
    47                 qinfo = Sparql2rqlTranslator(self.schema).translate(sparql)
    47                 qinfo = Sparql2rqlTranslator(self._cw.schema).translate(sparql)
    48             except rql.TypeResolverException, ex:
    48             except rql.TypeResolverException, ex:
    49                 self.w(self.req._('can not resolve entity types:') + u' ' + unicode('ex'))
    49                 self.w(self._cw._('can not resolve entity types:') + u' ' + unicode('ex'))
    50             except UnsupportedQuery:
    50             except UnsupportedQuery:
    51                 self.w(self.req._('we are not yet ready to handle this query'))
    51                 self.w(self._cw._('we are not yet ready to handle this query'))
    52             except xy.UnsupportedVocabulary, ex:
    52             except xy.UnsupportedVocabulary, ex:
    53                 self.w(self.req._('unknown vocabulary:') + u' ' + unicode('ex'))
    53                 self.w(self._cw._('unknown vocabulary:') + u' ' + unicode('ex'))
    54             if vid == 'sparqlxml':
    54             if vid == 'sparqlxml':
    55                 url = self.build_url('view', rql=qinfo.finalize(), vid=vid)
    55                 url = self.build_url('view', rql=qinfo.finalize(), vid=vid)
    56                 raise Redirect(url)
    56                 raise Redirect(url)
    57             rset = self.req.execute(qinfo.finalize())
    57             rset = self._cw.execute(qinfo.finalize())
    58             self.wview(vid, rset, 'null')
    58             self.wview(vid, rset, 'null')
    59 
    59 
    60 
    60 
    61 ## sparql resultset views #####################################################
    61 ## sparql resultset views #####################################################
    62 
    62 
    85     content_type = 'application/sparql-results+xml'
    85     content_type = 'application/sparql-results+xml'
    86     templatable = False
    86     templatable = False
    87 
    87 
    88     def call(self):
    88     def call(self):
    89         # XXX handle UNION
    89         # XXX handle UNION
    90         rqlst = self.rset.syntax_tree().children[0]
    90         rqlst = self.cw_rset.syntax_tree().children[0]
    91         varnames = [var.name for var in rqlst.selection]
    91         varnames = [var.name for var in rqlst.selection]
    92         results = E.results()
    92         results = E.results()
    93         for rowidx in xrange(len(self.rset)):
    93         for rowidx in xrange(len(self.cw_rset)):
    94             result = E.result()
    94             result = E.result()
    95             for colidx, varname in enumerate(varnames):
    95             for colidx, varname in enumerate(varnames):
    96                 result.append(self.cell_binding(rowidx, colidx, varname))
    96                 result.append(self.cell_binding(rowidx, colidx, varname))
    97             results.append(result)
    97             results.append(result)
    98         sparql = E.sparql(E.head(*(E.variable(name=name) for name in varnames)),
    98         sparql = E.sparql(E.head(*(E.variable(name=name) for name in varnames)),
    99                           results)
    99                           results)
   100         self.w(u'<?xml version="1.0"?>\n')
   100         self.w(u'<?xml version="1.0"?>\n')
   101         self.w(etree.tostring(sparql, encoding=unicode, pretty_print=True))
   101         self.w(etree.tostring(sparql, encoding=unicode, pretty_print=True))
   102 
   102 
   103     def cell_binding(self, row, col, varname):
   103     def cell_binding(self, row, col, varname):
   104         celltype = self.rset.description[row][col]
   104         celltype = self.cw_rset.description[row][col]
   105         if self.schema.eschema(celltype).is_final():
   105         if self._cw.schema.eschema(celltype).is_final():
   106             cellcontent = self.view('cell', self.rset, row=row, col=col)
   106             cellcontent = self.view('cell', self.cw_rset, row=row, col=col)
   107             return E.binding(E.literal(cellcontent,
   107             return E.binding(E.literal(cellcontent,
   108                                        datatype=xmlschema(celltype)),
   108                                        datatype=xmlschema(celltype)),
   109                              name=varname)
   109                              name=varname)
   110         else:
   110         else:
   111             entity = self.rset.get_entity(row, col)
   111             entity = self.cw_rset.get_entity(row, col)
   112             return E.binding(E.uri(entity.absolute_url()), name=varname)
   112             return E.binding(E.uri(entity.absolute_url()), name=varname)
   113 
   113 
   114     def set_request_content_type(self):
   114     def set_request_content_type(self):
   115         """overriden to set the correct filetype and filename"""
   115         """overriden to set the correct filetype and filename"""
   116         self.req.set_content_type(self.content_type,
   116         self._cw.set_content_type(self.content_type,
   117                                   filename='sparql.xml',
   117                                   filename='sparql.xml',
   118                                   encoding=self.req.encoding)
   118                                   encoding=self._cw.encoding)
   119 
   119 
   120 def registration_callback(vreg):
   120 def registration_callback(vreg):
   121     if Sparql2rqlTranslator is not None:
   121     if Sparql2rqlTranslator is not None:
   122         vreg.register_all(globals().values(), __name__)
   122         vreg.register_all(globals().values(), __name__)