author | Fabien Amarger <fabien.amarger@logilab.fr> |
Tue, 10 Mar 2020 17:37:43 +0100 | |
changeset 12912 | 3966f09d5f5c |
parent 12911 | a17cbf539a69 |
permissions | -rw-r--r-- |
11967
83739be20fab
[pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11820
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:
11820
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:
11820
diff
changeset
|
3 |
# |
83739be20fab
[pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11820
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:
11820
diff
changeset
|
5 |
# |
83739be20fab
[pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11820
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:
11820
diff
changeset
|
7 |
# |
83739be20fab
[pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11820
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:
11820
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:
11820
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:
11820
diff
changeset
|
11 |
# any later version. |
83739be20fab
[pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11820
diff
changeset
|
12 |
# |
83739be20fab
[pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11820
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:
11820
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:
11820
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:
11820
diff
changeset
|
16 |
# details. |
83739be20fab
[pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11820
diff
changeset
|
17 |
# |
83739be20fab
[pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11820
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:
11820
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:
11820
diff
changeset
|
20 |
|
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:
12567
diff
changeset
|
21 |
from pyramid.httpexceptions import HTTPNotFound |
11967
83739be20fab
[pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents:
11820
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:
12567
diff
changeset
|
23 |
from cubicweb import rdf |
11599
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
24 |
|
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
25 |
|
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:
12567
diff
changeset
|
26 |
def negociate_mime_type(request, possible_mimetypes): |
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:
12567
diff
changeset
|
27 |
accepted_headers_by_weight = sorted( |
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:
12567
diff
changeset
|
28 |
request.accept.parsed or [], key=lambda h: h[1], reverse=True |
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:
12567
diff
changeset
|
29 |
) |
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:
12567
diff
changeset
|
30 |
mime_type_negociated = None |
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:
12567
diff
changeset
|
31 |
for parsed_header in accepted_headers_by_weight: |
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:
12567
diff
changeset
|
32 |
accepted_mime_type = parsed_header[0] |
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:
12567
diff
changeset
|
33 |
if accepted_mime_type in possible_mimetypes: |
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:
12567
diff
changeset
|
34 |
mime_type_negociated = accepted_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:
12567
diff
changeset
|
35 |
break |
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:
12567
diff
changeset
|
36 |
return mime_type_negociated |
11599
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
37 |
|
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
38 |
|
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:
12567
diff
changeset
|
39 |
def rdf_context_from_eid(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:
12567
diff
changeset
|
40 |
mime_type = negociate_mime_type(request, rdf.RDF_MIMETYPE_TO_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:
12567
diff
changeset
|
41 |
if mime_type is None: |
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:
12567
diff
changeset
|
42 |
raise HTTPNotFound() |
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:
12567
diff
changeset
|
43 |
entity = request.cw_request.entity_from_eid(request.matchdict['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:
12567
diff
changeset
|
44 |
return RDFResource(entity, mime_type) |
11599
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
45 |
|
f8ba6ea94af9
[refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents:
diff
changeset
|
46 |
|
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:
12567
diff
changeset
|
47 |
class 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:
12567
diff
changeset
|
48 |
def __init__(self, entity, 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:
12567
diff
changeset
|
49 |
self.entity = 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:
12567
diff
changeset
|
50 |
self.mime_type = mime_type |