18 # You should have received a copy of the GNU Lesser General Public License along |
18 # You should have received a copy of the GNU Lesser General Public License along |
19 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
19 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
20 |
20 |
21 """Experimental REST API for CubicWeb using Pyramid.""" |
21 """Experimental REST API for CubicWeb using Pyramid.""" |
22 |
22 |
23 from __future__ import absolute_import |
23 import rdflib |
24 |
|
25 |
24 |
26 from pyramid.view import view_config |
25 from pyramid.view import view_config |
27 from cubicweb.pyramid.resources import EntityResource, ETypeResource |
26 from pyramid.response import Response |
|
27 |
|
28 from cubicweb import rdf |
|
29 from cubicweb.pyramid.resources import rdf_context_from_eid, RDFResource |
28 |
30 |
29 |
31 |
30 @view_config( |
32 @view_config( |
31 route_name='cwentities', |
33 route_name='one_entity', |
32 context=EntityResource, |
34 context=RDFResource, |
33 request_method='DELETE') |
35 ) |
34 def delete_entity(context, request): |
36 @view_config( |
35 context.rset.one().cw_delete() |
37 route_name='one_entity_eid', |
36 request.response.status_int = 204 |
38 context=RDFResource, |
37 return request.response |
39 ) |
|
40 def view_entity_as_rdf(context, request): |
|
41 graph = rdflib.ConjunctiveGraph() |
|
42 rdf.add_entity_to_graph(graph, context.entity) |
|
43 rdf_format = rdf.RDF_MIMETYPE_TO_FORMAT[context.mime_type] |
|
44 response = Response(graph.serialize(format=rdf_format)) |
|
45 response.content_type = context.mime_type |
|
46 return response |
38 |
47 |
39 |
48 |
40 def includeme(config): |
49 def includeme(config): |
41 config.include('.predicates') |
50 config.include('.predicates') |
42 config.add_route( |
51 config.add_route( |
43 'cwentities', '/{etype}/*traverse', |
52 'one_entity', |
44 factory=ETypeResource.from_match('etype'), match_is_etype='etype') |
53 '/{etype}/{eid}', |
|
54 factory=rdf_context_from_eid, |
|
55 match_is_etype_and_eid=('etype', 'eid'), |
|
56 ) |
|
57 config.add_route( |
|
58 'one_entity_eid', |
|
59 '/{eid}', |
|
60 factory=rdf_context_from_eid, |
|
61 match_is_eid='eid' |
|
62 ) |
45 config.scan(__name__) |
63 config.scan(__name__) |