def change_annotation_state(resource_id, new_graph): ''' Advance the status of an annotation - string **resource_id** the resource URL - string **new_graph** the name of the state where more the annotation ''' old_graph = find_annotation_graph(resource_id) old_g = generate_graph(CharmeMiddleware.get_store(), old_graph) new_g = generate_graph(CharmeMiddleware.get_store(), new_graph) for res in old_g.triples((_formatResourceURIRef(resource_id), None, None)): old_g.remove(res) new_g.add(res) return new_g
def insert_rdf(data, mimetype, graph = None, store=None): ''' Inserts an RDF/json-ld document into the triplestore - string **data** a document - string **mimetype** the document mimetype - string **graph** the graph name - rdflib.Store **store** if none use the return of get_store() ''' if store is None: store = CharmeMiddleware.get_store() tmp_g = Graph() #Necessary as RDFlib does not contain the json-ld lib tmp_g.parse(data = data, format = rdf_format_from_mime(mimetype)) _formatSubmittedAnnotation(tmp_g) final_g = generate_graph(store, graph) for ns in tmp_g.namespaces(): final_g.store.bind(str(ns[0]), ns[1]) for res in tmp_g: final_g.add(res) return final_g
def setUp(self): self.store = CharmeMiddleware.get_store(debug = True) self.graph = 'submitted' self.identifier = '%s/%s' % (SPARQL_DATA, self.graph) self.g = Graph(store=self.store, identifier=self.identifier) self.factory = RequestFactory()
def find_resource_by_id(resource_id): ''' Returns the charme resource associated with the given resource_id * resource_id:String * return: an rdflib.Graph object ''' g = ConjunctiveGraph(store=CharmeMiddleware.get_store()) tmp_g = Graph() for res in g.triples((_formatResourceURIRef(resource_id), None, None)): tmp_g.add(res) return tmp_g
def processDELETE(request): graph = get_graph_from_request(request) g = None query_object = None if graph is None: g = ConjunctiveGraph(store=CharmeMiddleware.get_store()) query_object = ''' DROP DEFAULT; ''' query_object = '' else: g = ConjunctiveGraph(store=CharmeMiddleware.get_store()) query_object = ''' DROP GRAPH <%s>; ''' query_object = query_object % (graph) g.update(query_object) return HttpResponse(status = 204)
def processHEAD(request, return_content = False): ''' Returns an httplib.HTTPRequest ''' graph = get_graph_from_request(request) accept = http_accept(request) if accept not in FORMAT_MAP.values(): return HttpResponse(status = 406) g = None if graph is None: g = ConjunctiveGraph(store=CharmeMiddleware.get_store()) else: g = generate_graph(CharmeMiddleware.get_store(), URIRef(graph)) content = g.serialize(format = rdf_format_from_mime(accept)) if return_content: return HttpResponse(content = content) return HttpResponse()
def setUp(self): self.store = CharmeMiddleware.get_store(debug = True) self.graph = 'submitted' self.identifier = '%s/%s' % (getattr(settings, 'SPARQL_DATA'), self.graph) self.g = Graph(store=self.store, identifier=self.identifier) self.factory = RequestFactory() users = User.objects.filter(username = '******') if users.count() == 0: self.user = User.objects.create_user('Alberto Sordi', '*****@*****.**', 'ammericano')
def processPUT(request): graph = get_graph_from_request(request) payload = request.body g = None query_object = None if graph is None: g = ConjunctiveGraph(store=CharmeMiddleware.get_store()) query_object = ''' DROP SILENT DEFAULT; ''' query_object = '' else: g = ConjunctiveGraph(store=CharmeMiddleware.get_store()) query_object = ''' DROP SILENT GRAPH <%s>; ''' query_object = query_object % (graph) g.update(query_object) insert_rdf(payload, content_type(request), graph) return HttpResponse(status = 204)
def _collect_annotations(graph): ''' Returns a graph containing all the node annotations - string **graph** the graph name ''' g = generate_graph(CharmeMiddleware.get_store(), graph) tmp_g = Graph() try: for res in g.triples((None, None, OA['Annotation'])): tmp_g.add(res) for res in g.triples((None, OA['hasTarget'], None)): tmp_g.add(res) for res in g.triples((None, OA['hasBody'], None)): tmp_g.add(res) except URLError as e: raise StoreConnectionError("Cannot open a connection with triple store \n" + str(e)) return tmp_g
def find_annotation_graph(resource_id): triple = (_formatResourceURIRef(resource_id), None, None) for graph in [ANNO_SUBMITTED, ANNO_STABLE, ANNO_RETIRED, ANNO_INVALID]: new_g = generate_graph(CharmeMiddleware.get_store(), graph) if triple in new_g: return graph
def setUp(self): self.store = CharmeMiddleware.get_store(debug = True) self.factory = RequestFactory()