def import_old_data(request): everything_graph = Graph() bind_namespaces(everything_graph) # Either gather post data (must be one project/user graph at a time) if request.method == 'POST': logger.debug('!!!!!!!!!!!!!!! views.py - import_old_data') parse_request_into_graph(request, everything_graph) add_all_users(everything_graph) # Create each user's default project # Due to the structure of the data when exported from the old system, this also # add each annotation to the project as an aggregated resource create_project(everything_graph) # or serialize from a folder, where each file is one project/user graph else: i = 0 for file_name in listdir("output/"): if file_name.startswith('.'): continue try: everything_graph.parse("output/" + file_name, format=guess_format(file_name) or 'turtle') except Exception as e: print "Failed to decode file '%s' with error message '%s'"%(file_name, e.args[-1]) else: add_all_users(everything_graph) create_project(everything_graph) return HttpResponse("I finished migrating data without errors.")
def import_old_data(request): everything_graph = Graph() bind_namespaces(everything_graph) # Either gather post data (must be one project/user graph at a time) if request.method == 'POST': logger.debug('!!!!!!!!!!!!!!! views.py - import_old_data') parse_request_into_graph(request, everything_graph) add_all_users(everything_graph) # Create each user's default project # Due to the structure of the data when exported from the old system, this also # add each annotation to the project as an aggregated resource create_project(everything_graph) # or serialize from a folder, where each file is one project/user graph else: i = 0 for file_name in listdir("output/"): if file_name.startswith('.'): continue try: everything_graph.parse("output/" + file_name, format=guess_format(file_name) or 'turtle') except Exception as e: print "Failed to decode file '%s' with error message '%s'" % ( file_name, e.args[-1]) else: add_all_users(everything_graph) create_project(everything_graph) return HttpResponse("I finished migrating data without errors.")
def update_user(request, username): if request.user.is_authenticated(): try: input_graph = parse_request_into_graph(request) except (ParserError, SyntaxError) as e: return HttpResponse(status=400, content="Unable to parse serialization.\n%s" % e) if permission_updates_are_allowed(request, input_graph): user_uri = URIRef(uris.uri('semantic_store_users', username=username)) user_graph = Graph(store=rdfstore(), identifier=USER_GRAPH_IDENTIFIER) with transaction.commit_on_success(): try: user = User.objects.get(username=username) except ObjectDoesNotExist: return HttpResponse('User %s does not exist' % (username), status=400) else: for t in input_graph.triples((user_uri, NS.dm.lastOpenProject, None)): user_graph.set(t) for project in input_graph.objects(user_uri, NS.perm.mayRead): grant_read_permissions(project, user=user) for project in input_graph.objects(user_uri, NS.perm.mayUpdate): grant_write_permissions(project, user=user) for project in input_graph.objects(user_uri, NS.perm.mayAdminister): grant_admin_permissions(project, user=user) return HttpResponse(status=204) else: return HttpResponseForbidden('The PUT graph contained permissions updates which were not allowed') else: return HttpResponse(status=401)
def update_project_text_from_request(request, project_uri, text_uri): logger.debug("************** update_project_text_from_request") logger.debug("%%%%%%%%%%%%%% request.body:") logger.debug("\r" + request.body) logger.debug("%%%%%%%%%%%%%% project_uri:") logger.debug("\r" + project_uri) logger.debug("%%%%%%%%%%%%%% text_uri:") logger.debug("\r" + text_uri) if request.user.is_authenticated(): if has_permission_over(project_uri, user=request.user, permission=NS.perm.mayUpdate): try: logger.debug( '!!!!!!!!!!!!!!! project_texts.py - update_project_text_from_request' ) g = parse_request_into_graph(request) except (ParserError, SyntaxError) as e: return HttpResponse( status=400, content="Unable to parse serialization. %s" % e) else: # On successful parse, send to basic method update_project_text(g, project_uri, text_uri, request.user) return HttpResponse(status=204) else: return HttpResponseForbidden() else: return HttpResponse(status=401)
def specific_resource_graph(request, project_uri, specific_resource, source): if request.method == 'GET': return NegotiatedGraphResponse(request, read_specific_resource(project_uri, specific_resource, source)) elif request.method == 'PUT': logger.debug('!!!!!!!!!!!!!!! views.py - specific_resource_graph') g = parse_request_into_graph(request) update_specific_resource(g, URIRef(project_uri), URIRef(specific_resource))
def delete_triples_from_project(request, uri): """Deletes the triples in a graph provided by a request object from the project graph. Returns an HttpResponse of all the triples which were successfully removed from the graph.""" if request.user.is_authenticated(): if permissions.has_permission_over(uri, user=request.user, permission=NS.perm.mayUpdate): removed = Graph() bind_namespaces(removed) try: g = parse_request_into_graph(request) except (ParserError, SyntaxError) as e: return HttpResponse(status=400, content="Unable to parse serialization.\n%s" % e) project_g = get_project_graph(uri) project_metadata_g = get_project_metadata_graph(uri) for t in g: if t in project_g: project_g.remove(t) removed.add(t) project_metadata_g.remove(t) return NegotiatedGraphResponse(request, removed) else: return HttpResponseForbidden('User "%s" does not have update permissions over project "%s"' % (request.user.username, uri)) else: return HttpResponse(status=401)
def delete_triples_from_project(request, uri): """Deletes the triples in a graph provided by a request object from the project graph. Returns an HttpResponse of all the triples which were successfully removed from the graph.""" if request.user.is_authenticated(): if permissions.has_permission_over(uri, user=request.user, permission=NS.perm.mayUpdate): removed = Graph() bind_namespaces(removed) try: logger.debug('!!!!!!!!!!!!!!! project_texts.py - delete_triples_from_project') g = parse_request_into_graph(request) except (ParserError, SyntaxError) as e: return HttpResponse(status=400, content="Unable to parse serialization.\n%s" % e) project_g = get_project_graph(uri) project_metadata_g = get_project_metadata_graph(uri) for t in g: if t in project_g: project_g.remove(t) removed.add(t) project_metadata_g.remove(t) return NegotiatedGraphResponse(request, removed) else: return HttpResponseForbidden('User "%s" does not have update permissions over project "%s"' % (request.user.username, uri)) else: return HttpResponse(status=401)
def create_project_text_from_request(request, project_uri, request_text_uri): if request.user.is_authenticated(): if has_permission_over(project_uri, user=request.user, permission=NS.perm.mayUpdate): try: logger.debug('!!!!!!!!!!!!!!! project_texts.py - create_project_text_from_request') g = parse_request_into_graph(request) except (ParserError, SyntaxError) as e: logger.debug('!!!!!!!!!!!!!!! project_texts.py - create_project_text_from_request - EXCEPTION !!!!!!!!') return HttpResponse(status=400, content="Unable to parse serialization.\n%s" % e) else: logger.debug('!!!!!!!!!!!!!!! project_texts.py - create_project_text_from_request - ELSE !!!!!!!!') text_uri = URIRef(uris.uuid()) logger.debug("uris.uuid(): %s", uris.uuid()) logger.debug("text_uri: %s", text_uri) logger.debug('!!!!!!!!!!!!!!! project_texts.py - create_project_text_from_request - CREATING TEXT... !!!!!!!!') update_project_text(g, project_uri, text_uri, request.user) logger.debug('!!!!!!!!!!!!!!! project_texts.py - create_project_text_from_request - CREATING TEXT COMPLETE !!!!!!!!') logger.debug('!!!!!!!!!!!!!!! project_texts.py - create_project_text_from_request - UPDATING... !!!!!!!!') update_project_text(g, project_uri, request_text_uri, request.user) logger.debug('!!!!!!!!!!!!!!! project_texts.py - create_project_text_from_request - UPDATING COMPLETE !!!!!!!!') return NegotiatedGraphResponse(request, read_project_text(project_uri, text_uri)) else: return HttpResponseForbidden() else: return HttpResponse(status=401)
def project_canvases(request, project_uri, canvas_uri): if request.method == 'GET': return NegotiatedGraphResponse(request, read_canvas(request, project_uri, canvas_uri)) elif request.method == 'PUT': input_graph = parse_request_into_graph(request) return NegotiatedGraphResponse(request, update_canvas(project_uri, canvas_uri, input_graph)) else: return HttpResponseNotAllowed(('GET', 'PUT'))
def create_project_from_request(request): """Takes a graph via an http request, and creates a project in the database (and the metadata cache) from an input graph""" try: g = parse_request_into_graph(request) except (ParserError, SyntaxError) as e: return HttpResponse(status=400, content="Unable to parse serialization.\n%s" % e) create_project(g) return HttpResponse("Successfully created the project.")
def specific_resource_graph(request, project_uri, specific_resource, source): if request.method == 'GET': return NegotiatedGraphResponse( request, read_specific_resource(project_uri, specific_resource, source)) elif request.method == 'PUT': g = parse_request_into_graph(request) update_specific_resource(g, URIRef(project_uri), URIRef(specific_resource))
def project_canvases(request, project_uri, canvas_uri): if request.method == 'GET': return NegotiatedGraphResponse( request, read_canvas(request, project_uri, canvas_uri)) elif request.method == 'PUT': input_graph = parse_request_into_graph(request) return NegotiatedGraphResponse( request, update_canvas(project_uri, canvas_uri, input_graph)) else: return HttpResponseNotAllowed(('GET', 'PUT'))
def create_project_from_request(request): """Takes a graph via an http request, and creates a project in the database (and the metadata cache) from an input graph""" try: logger.debug('!!!!!!!!!!!!!!! project_texts.py - create_project_from_request') g = parse_request_into_graph(request) except (ParserError, SyntaxError) as e: return HttpResponse(status=400, content="Unable to parse serialization.\n%s" % e) create_project(g) return HttpResponse("Successfully created the project.")
def create_or_update_annotations(request, dest_graph_uri=None, anno_uri=None): dest_g = destination_graph(dest_graph_uri) annotations_g = graph() try: parse_request_into_graph(request, annotations_g) except (ParserError, SyntaxError) as e: return HttpResponse(status=400, content="Unable to parse serialization.\n%s" % e) anno_uris = annotation_uris(annotations_g) if not anno_uris: return HttpResponse(status=400, content="No %s nodes found." % URIRef(NS.oa['Annotation'])) if not valid_annotations(dest_g, annotations_g, anno_uris): return HttpResponse(status=400, content=validator.failure) with transaction.commit_on_success(): for i in anno_uris: stored_g = update_annotation(request, dest_g, annotations_g, i) return NegotiatedGraphResponse(request, stored_g, status=201)
def create_project_text_from_request(request, project_uri): if request.user.is_authenticated(): if has_permission_over(project_uri, user=request.user, permission=NS.perm.mayUpdate): try: g = parse_request_into_graph(request) except (ParserError, SyntaxError) as e: return HttpResponse(status=400, content="Unable to parse serialization.\n%s" % e) else: text_uri = URIRef(uris.uuid()) update_project_text(g, project_uri, text_uri, request.user) return NegotiatedGraphResponse(request, read_project_text(project_uri, text_uri)) else: return HttpResponseForbidden() else: return HttpResponse(status=401)
def update_project(request, uri): """Updates the project and metadata graph from a put or post request""" if request.user.is_authenticated(): if permissions.has_permission_over(uri, user=request.user, permission=NS.perm.mayUpdate): try: input_graph = parse_request_into_graph(request) except (ParserError, SyntaxError) as e: return HttpResponse(status=400, content="Unable to parse serialization.\n%s" % e) update_project_graph(input_graph, URIRef(uri)) return HttpResponse(status=204) else: return HttpResponseForbidden('User "%s" does not have update permissions over project "%s"' % (request.user.username, uri)) else: return HttpResponse('Unauthorized', status=401)
def create_project_text_from_request(request, project_uri, request_text_uri): if request.user.is_authenticated(): if has_permission_over(project_uri, user=request.user, permission=NS.perm.mayUpdate): try: logger.debug( '!!!!!!!!!!!!!!! project_texts.py - create_project_text_from_request' ) g = parse_request_into_graph(request) except (ParserError, SyntaxError) as e: logger.debug( '!!!!!!!!!!!!!!! project_texts.py - create_project_text_from_request - EXCEPTION !!!!!!!!' ) return HttpResponse( status=400, content="Unable to parse serialization.\n%s" % e) else: logger.debug( '!!!!!!!!!!!!!!! project_texts.py - create_project_text_from_request - ELSE !!!!!!!!' ) text_uri = URIRef(uris.uuid()) logger.debug("uris.uuid(): %s", uris.uuid()) logger.debug("text_uri: %s", text_uri) logger.debug( '!!!!!!!!!!!!!!! project_texts.py - create_project_text_from_request - CREATING TEXT... !!!!!!!!' ) update_project_text(g, project_uri, text_uri, request.user) logger.debug( '!!!!!!!!!!!!!!! project_texts.py - create_project_text_from_request - CREATING TEXT COMPLETE !!!!!!!!' ) logger.debug( '!!!!!!!!!!!!!!! project_texts.py - create_project_text_from_request - UPDATING... !!!!!!!!' ) update_project_text(g, project_uri, request_text_uri, request.user) logger.debug( '!!!!!!!!!!!!!!! project_texts.py - create_project_text_from_request - UPDATING COMPLETE !!!!!!!!' ) return NegotiatedGraphResponse( request, read_project_text(project_uri, text_uri)) else: return HttpResponseForbidden() else: return HttpResponse(status=401)
def remove_triples_from_user(request, username): removed = Graph() user = User.objects.get(username=username) try: input_graph = parse_request_into_graph(request) except (ParserError, SyntaxError) as e: return HttpResponse(status=400, content="Unable to parse serialization.\n%s" % e) with transaction.commit_on_success(): for t in input_graph: if is_allowed_update_triple(t, username, request): removed.add(t) user_uri, perm, project = t revoke_permission_by_uri(perm, project, user=user) else: print "Triple %s was rejected to be removed from user graph" % unicode(t) return HttpResponse(status=204)
def update_project(request, uri): """Updates the project and metadata graph from a put or post request""" if request.user.is_authenticated(): if (permissions.has_permission_over(uri, user=request.user, permission=NS.perm.mayUpdate) or permissions.is_abandoned_project(uri)): logger.debug("$$$$$$$$$$$ update_project (request)") logger.debug(request) try: logger.debug('!!!!!!!!!!!!!!! project_texts.py - update_project') input_graph = parse_request_into_graph(request) except (ParserError, SyntaxError) as e: return HttpResponse(status=400, content="Unable to parse serialization.\n%s" % e) update_project_graph(input_graph, URIRef(uri)) return HttpResponse(status=204) else: return HttpResponseForbidden('User "%s" does not have update permissions over project "%s"' % (request.user.username, uri)) else: return HttpResponse('Unauthorized', status=401)
def update_project_text_from_request(request, project_uri, text_uri): logger.debug("************** update_project_text_from_request") logger.debug("%%%%%%%%%%%%%% request.body:") logger.debug("\r" + request.body) logger.debug("%%%%%%%%%%%%%% project_uri:") logger.debug("\r" + project_uri) logger.debug("%%%%%%%%%%%%%% text_uri:") logger.debug("\r" + text_uri) if request.user.is_authenticated(): if has_permission_over(project_uri, user=request.user, permission=NS.perm.mayUpdate): try: logger.debug('!!!!!!!!!!!!!!! project_texts.py - update_project_text_from_request') g = parse_request_into_graph(request) except (ParserError, SyntaxError) as e: return HttpResponse(status=400, content="Unable to parse serialization. %s" % e) else: # On successful parse, send to basic method update_project_text(g, project_uri, text_uri, request.user) return HttpResponse(status=204) else: return HttpResponseForbidden() else: return HttpResponse(status=401)