cubicweb/pyramid/rest_api.py
author Philippe Pepiot <ph@itsalwaysdns.eu>
Tue, 31 Mar 2020 19:15:03 +0200
changeset 12957 0c973204033a
parent 12911 a17cbf539a69
permissions -rw-r--r--
[server] prevent returning closed cursor to the database pool In since c8c6ad8 init_repository use repo.internal_cnx() instead of repo.system_source.get_connection() so it use the pool and we should not close cursors from the pool before returning it back. Otherwise we may have "connection already closed" error. This bug only trigger when connection-pool-size = 1. Since we are moving to use a dynamic pooler we need to get this fixed. This does not occur with sqlite since the connection wrapper instantiate new cursor everytime, but this occur with other databases.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11967
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
     1
# copyright 2017 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
     2
# copyright 2014-2016 UNLISH S.A.S. (Montpellier, FRANCE), all rights reserved.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
     3
#
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
     4
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
     5
#
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
     6
# This file is part of CubicWeb.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
     7
#
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
     8
# CubicWeb is free software: you can redistribute it and/or modify it under the
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
     9
# terms of the GNU Lesser General Public License as published by the Free
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
    10
# Software Foundation, either version 2.1 of the License, or (at your option)
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
    11
# any later version.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
    12
#
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
    13
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
    14
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
    15
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
    16
# details.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
    17
#
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
    18
# You should have received a copy of the GNU Lesser General Public License along
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
    19
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
    20
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
    21
"""Experimental REST API for CubicWeb using Pyramid."""
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
    22
12911
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    23
import rdflib
11596
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    24
11599
f8ba6ea94af9 [refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11598
diff changeset
    25
from pyramid.view import view_config
12911
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    26
from pyramid.response import Response
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    27
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    28
from cubicweb import rdf
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    29
from cubicweb.pyramid.resources import rdf_context_from_eid, RDFResource
11596
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    30
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    31
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    32
@view_config(
12911
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    33
    route_name='one_entity',
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    34
    context=RDFResource,
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    35
)
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    36
@view_config(
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    37
    route_name='one_entity_eid',
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    38
    context=RDFResource,
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    39
)
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    40
def view_entity_as_rdf(context, request):
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    41
    graph = rdflib.ConjunctiveGraph()
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    42
    rdf.add_entity_to_graph(graph, context.entity)
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    43
    rdf_format = rdf.RDF_MIMETYPE_TO_FORMAT[context.mime_type]
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    44
    response = Response(graph.serialize(format=rdf_format))
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    45
    response.content_type = context.mime_type
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    46
    return response
11596
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    47
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    48
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    49
def includeme(config):
11959
ddc05ce75319 [pyramid] Include 'cubicweb.pyramid.predicates' where it is used
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11811
diff changeset
    50
    config.include('.predicates')
11596
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    51
    config.add_route(
12911
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    52
        'one_entity',
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    53
        '/{etype}/{eid}',
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    54
        factory=rdf_context_from_eid,
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    55
        match_is_etype_and_eid=('etype', 'eid'),
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    56
    )
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    57
    config.add_route(
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    58
        'one_entity_eid',
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    59
        '/{eid}',
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    60
        factory=rdf_context_from_eid,
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    61
        match_is_eid='eid'
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    62
    )
11596
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    63
    config.scan(__name__)