web/views/sparql.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Sat, 17 Jul 2010 09:55:29 +0200
changeset 5986 8339d4739f7f
parent 5621 6f32f08d91f3
child 6582 8eb7883b4223
permissions -rw-r--r--
[images] add link around main image
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     1
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4721
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    18
"""SPARQL integration
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    19
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    20
"""
4716
55b6a3262071 fix some pylint detected errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    21
__docformat__ = "restructuredtext en"
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    22
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    23
from yams import xy
5621
6f32f08d91f3 [sparql] improve error reporting and fix one bug
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5434
diff changeset
    24
from rql import TypeResolverException
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    25
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    26
from lxml import etree
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    27
from lxml.builder import E
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    28
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    29
from cubicweb.view import StartupView, AnyRsetView
2426
0209d9bc45a8 possibility to choose results format, set a nicer file name on sparqlxml view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2424
diff changeset
    30
from cubicweb.web import Redirect, form, formfields, formwidgets as fwdgs
4719
aaed3f813ef8 kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4716
diff changeset
    31
from cubicweb.web.views import forms
2529
736893a990c4 make fyzz/spa2rql a recommends only
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2444
diff changeset
    32
try:
4716
55b6a3262071 fix some pylint detected errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    33
    from cubicweb.spa2rql import Sparql2rqlTranslator, UnsupportedQuery
2529
736893a990c4 make fyzz/spa2rql a recommends only
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2444
diff changeset
    34
except ImportError:
736893a990c4 make fyzz/spa2rql a recommends only
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2444
diff changeset
    35
    # fyzz not available (only a recommends)
736893a990c4 make fyzz/spa2rql a recommends only
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2444
diff changeset
    36
    Sparql2rqlTranslator = None
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    37
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    38
class SparqlForm(forms.FieldsForm):
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2789
diff changeset
    39
    __regid__ = 'sparql'
2444
4e61d9e4befb [cleanup] fix typo
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2426
diff changeset
    40
    sparql = formfields.StringField(help=_('type here a sparql query'))
2426
0209d9bc45a8 possibility to choose results format, set a nicer file name on sparqlxml view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2424
diff changeset
    41
    resultvid = formfields.StringField(choices=((_('table'), 'table'),
0209d9bc45a8 possibility to choose results format, set a nicer file name on sparqlxml view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2424
diff changeset
    42
                                                (_('sparql xml'), 'sparqlxml')),
0209d9bc45a8 possibility to choose results format, set a nicer file name on sparqlxml view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2424
diff changeset
    43
                                       widget=fwdgs.Radio,
4159
6b2b20c73d59 refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4045
diff changeset
    44
                                       value='table')
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    45
    form_buttons = [fwdgs.SubmitButton()]
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    46
    @property
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    47
    def action(self):
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
    48
        return self._cw.url()
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    49
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    50
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    51
class SparqlFormView(form.FormViewMixIn, StartupView):
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2789
diff changeset
    52
    __regid__ = 'sparql'
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    53
    def call(self):
5621
6f32f08d91f3 [sparql] improve error reporting and fix one bug
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5434
diff changeset
    54
        form = self._cw.vreg['forms'].select('sparql', self._cw)
3953
19aefd78f61b [forms] deprecate form_render(**kwargs) in favor of render(formvalues=None, rendervalues=None, renderer=None)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3689
diff changeset
    55
        self.w(form.render())
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
    56
        sparql = self._cw.form.get('sparql')
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
    57
        vid = self._cw.form.get('resultvid', 'table')
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    58
        if sparql:
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    59
            try:
4045
f4a52abb6f4f cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3998
diff changeset
    60
                qinfo = Sparql2rqlTranslator(self._cw.vreg.schema).translate(sparql)
5621
6f32f08d91f3 [sparql] improve error reporting and fix one bug
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5434
diff changeset
    61
            except TypeResolverException, exc:
6f32f08d91f3 [sparql] improve error reporting and fix one bug
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5434
diff changeset
    62
                self.w(self._cw._('can not resolve entity types:') + u' ' + unicode(exc))
2422
96da7dc42eb5 quick and dirty support from simple sparql queries + base ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    63
            except UnsupportedQuery:
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
    64
                self.w(self._cw._('we are not yet ready to handle this query'))
5621
6f32f08d91f3 [sparql] improve error reporting and fix one bug
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5434
diff changeset
    65
            except xy.UnsupportedVocabulary, exc:
6f32f08d91f3 [sparql] improve error reporting and fix one bug
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5434
diff changeset
    66
                self.w(self._cw._('unknown vocabulary:') + u' ' + unicode(exc))
5434
b56420abc00f [sparql] bugfix
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5424
diff changeset
    67
            else:
5621
6f32f08d91f3 [sparql] improve error reporting and fix one bug
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5434
diff changeset
    68
                rql, args = qinfo.finalize()
5434
b56420abc00f [sparql] bugfix
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5424
diff changeset
    69
                if vid == 'sparqlxml':
5621
6f32f08d91f3 [sparql] improve error reporting and fix one bug
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5434
diff changeset
    70
                    url = self._cw.build_url('view', rql=(rql,args), vid=vid)
5434
b56420abc00f [sparql] bugfix
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5424
diff changeset
    71
                    raise Redirect(url)
5621
6f32f08d91f3 [sparql] improve error reporting and fix one bug
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5434
diff changeset
    72
                rset = self._cw.execute(rql, args)
5434
b56420abc00f [sparql] bugfix
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 5424
diff changeset
    73
                self.wview(vid, rset, 'null')
2426
0209d9bc45a8 possibility to choose results format, set a nicer file name on sparqlxml view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2424
diff changeset
    74
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    75
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    76
## sparql resultset views #####################################################
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    77
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    78
YAMS_XMLSCHEMA_MAPPING = {
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    79
    'String': 'string',
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    80
    'Int': 'integer',
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    81
    'Float': 'float',
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    82
    'Boolean': 'boolean',
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    83
    'Datetime': 'dateTime',
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    84
    'Date': 'date',
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    85
    'Time': 'time',
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    86
    # XXX the following types don't have direct mapping
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    87
    'Decimal': 'string',
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    88
    'Interval': 'duration',
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    89
    'Password': 'string',
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    90
    'Bytes': 'base64Binary',
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    91
    }
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    92
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    93
def xmlschema(yamstype):
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    94
    return 'http://www.w3.org/2001/XMLSchema#%s' % YAMS_XMLSCHEMA_MAPPING[yamstype]
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    95
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    96
class SparqlResultXmlView(AnyRsetView):
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    97
    """The spec can be found here: http://www.w3.org/TR/rdf-sparql-XMLres/
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
    98
    """
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2789
diff changeset
    99
    __regid__ = 'sparqlxml'
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   100
    content_type = 'application/sparql-results+xml'
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   101
    templatable = False
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   102
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   103
    def call(self):
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   104
        # XXX handle UNION
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   105
        rqlst = self.cw_rset.syntax_tree().children[0]
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   106
        varnames = [var.name for var in rqlst.selection]
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   107
        results = E.results()
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   108
        for rowidx in xrange(len(self.cw_rset)):
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   109
            result = E.result()
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   110
            for colidx, varname in enumerate(varnames):
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   111
                result.append(self.cell_binding(rowidx, colidx, varname))
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   112
            results.append(result)
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   113
        sparql = E.sparql(E.head(*(E.variable(name=name) for name in varnames)),
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   114
                          results)
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   115
        self.w(u'<?xml version="1.0"?>\n')
2426
0209d9bc45a8 possibility to choose results format, set a nicer file name on sparqlxml view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2424
diff changeset
   116
        self.w(etree.tostring(sparql, encoding=unicode, pretty_print=True))
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   117
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   118
    def cell_binding(self, row, col, varname):
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   119
        celltype = self.cw_rset.description[row][col]
4045
f4a52abb6f4f cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 3998
diff changeset
   120
        if self._cw.vreg.schema.eschema(celltype).final:
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   121
            cellcontent = self.view('cell', self.cw_rset, row=row, col=col)
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   122
            return E.binding(E.literal(cellcontent,
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   123
                                       datatype=xmlschema(celltype)),
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   124
                             name=varname)
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   125
        else:
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   126
            entity = self.cw_rset.get_entity(row, col)
2424
70f85df651a5 simple implementation of sparql XML resultset view
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2422
diff changeset
   127
            return E.binding(E.uri(entity.absolute_url()), name=varname)
2426
0209d9bc45a8 possibility to choose results format, set a nicer file name on sparqlxml view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2424
diff changeset
   128
0209d9bc45a8 possibility to choose results format, set a nicer file name on sparqlxml view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2424
diff changeset
   129
    def set_request_content_type(self):
0209d9bc45a8 possibility to choose results format, set a nicer file name on sparqlxml view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2424
diff changeset
   130
        """overriden to set the correct filetype and filename"""
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   131
        self._cw.set_content_type(self.content_type,
2426
0209d9bc45a8 possibility to choose results format, set a nicer file name on sparqlxml view
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2424
diff changeset
   132
                                  filename='sparql.xml',
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   133
                                  encoding=self._cw.encoding)
2529
736893a990c4 make fyzz/spa2rql a recommends only
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2444
diff changeset
   134
736893a990c4 make fyzz/spa2rql a recommends only
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2444
diff changeset
   135
def registration_callback(vreg):
736893a990c4 make fyzz/spa2rql a recommends only
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2444
diff changeset
   136
    if Sparql2rqlTranslator is not None:
736893a990c4 make fyzz/spa2rql a recommends only
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2444
diff changeset
   137
        vreg.register_all(globals().values(), __name__)