def project_uris_by_title(self, user_graph, user_uri): projectsByTitle = defaultdict(list) bind_namespaces(user_graph) for row in user_graph.query(""" SELECT DISTINCT ?project ?title WHERE { ?user ore:aggregates ?project . OPTIONAL {?project dc:title ?title .} } """, initNs=ns, initBindings={'user': URIRef(user_uri)}): project_uri = uris.uri('semantic_store_projects', uri=row[0]) project_graph = Graph(store=rdfstore(), identifier=project_uri) project_resource = Resource(project_graph, URIRef(row[0])) titles = list(project_resource.objects(predicate=NS.dc['title'])) if len(titles) == 0 and row[1]: # The project graph doesn't have a title triple, but the user graph does, so use that projectsByTitle[unicode(row[1])].append(row[0]) else: # Use the project graph's title triples (preferred) for title in titles: projectsByTitle[unicode(title)].append(row[0]) return projectsByTitle
def __init__(self, request, graph, *args, **kwargs): mimetypes = list(accept_mimetypes(request.META['HTTP_ACCEPT'])) if settings.DEBUG and mimetypes[0].strip().lower().endswith('html'): format = NegotiatedGraphResponse.default_format kwargs['mimetype'] = '%s/%s' % ( NegotiatedGraphResponse.default_type, NegotiatedGraphResponse.default_format) else: for mimetype in mimetypes: format = mimetype[mimetype.rfind('/') + 1:].strip().lower() if format in RDFLIB_SERIALIZER_FORMATS: kwargs['mimetype'] = '%s/%s' % ( NegotiatedGraphResponse.default_type, NegotiatedGraphResponse.default_format) break else: format = NegotiatedGraphResponse.default_format kwargs['mimetype'] = '%s/%s' % ( NegotiatedGraphResponse.default_type, NegotiatedGraphResponse.default_format) super(NegotiatedGraphResponse, self).__init__(self, *args, **kwargs) bind_namespaces(graph) self.content = graph.serialize(format=format)
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 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 add_all_users(graph): bind_namespaces(graph) query = graph.query("""SELECT ?user ?email WHERE { ?user perm:hasPermissionOver ?project . ?user foaf:mbox ?email . ?user rdf:type foaf:Agent }""", initNs=ns) for q in query: username = "" # Remove username from customized uri s = q[0].split("/")[-1] username = s.split("'),")[0] email = q[1].split("mailto:")[-1] try: u = User.objects.create_user(username, email, DEFAULT_PASSWORD_STRING) except IntegrityError: transaction.rollback_unless_managed() print "User '%s' already existed in the database, and was not created." % ( username) else: u.save() print "User '%s' was created successfully." % (username)
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 read_all_users(request): g = Graph() bind_namespaces(g) for u in User.objects.filter(): g += user_metadata_graph(user=u) return NegotiatedGraphResponse(request, g)
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 add_all_users(graph): bind_namespaces(graph) query = graph.query("""SELECT ?user ?email WHERE { ?user perm:hasPermissionOver ?project . ?user foaf:mbox ?email . ?user rdf:type foaf:Agent }""", initNs = ns) for q in query: username = "" # Remove username from customized uri s = q[0].split("/")[-1] username = s.split("'),")[0] email = q[1].split("mailto:")[-1] try: u = User.objects.create_user(username, email, DEFAULT_PASSWORD_STRING) except IntegrityError: transaction.rollback_unless_managed() print "User '%s' already existed in the database, and was not created."%(username) else: u.save() print "User '%s' was created successfully."%(username)
def aggregated_uris_urls(uri, g): bind_namespaces(g) query = """SELECT DISTINCT ?resource_uri ?resource_url WHERE { ?uri ore:aggregates ?resource_uri . OPTIONAL { ?resource_url ore:describes ?resource_uri } . OPTIONAL { ?resource_uri ore:isDescribedBy ?resource_url } }""" qres = g.query(query, initNs=ns, initBindings={'uri': URIRef(uri)}) return list(qres)
def find_resource(manifest_uri, g, pred, obj): bind_namespaces(g) query = """SELECT DISTINCT ?resource_uri ?resource_url WHERE { <%s> ore:aggregates ?resource_uri . ?resource_url ore:describes ?resource_uri . ?resource_uri %s "%s" . }""" % (manifest_uri, pred, obj) qres = g.query(query, initNs=ns) return qres
def find_resource(manifest_uri, g, pred, obj): bind_namespaces(g) query = """SELECT DISTINCT ?resource_uri ?resource_url WHERE { <%s> ore:aggregates ?resource_uri . ?resource_url ore:describes ?resource_uri . ?resource_uri %s "%s" . }""" % (manifest_uri, pred, obj) qres = g.query(query, initNs = ns) return qres
def aggregated_uris_urls(uri, g): bind_namespaces(g) query = """SELECT DISTINCT ?resource_uri ?resource_url WHERE { ?uri ore:aggregates ?resource_uri . OPTIONAL { ?resource_url ore:describes ?resource_uri } . OPTIONAL { ?resource_uri ore:isDescribedBy ?resource_url } }""" qres = g.query(query, initNs=ns, initBindings={'uri': URIRef(uri)}) return list(qres)
def image_annotations(manifest_uri, g): bind_namespaces(g) query = """SELECT DISTINCT ?resource_uri ?resource_url WHERE { ?manifest_uri ore:aggregates ?resource_uri . ?resource_uri rdf:type dms:ImageAnnotationList . OPTIONAL { ?resource_url ore:describes ?resource_uri } . OPTIONAL { ?resource_uri ore:isDescribedBy ?resource_url } }""" qres = g.query(query, initNs=ns, initBindings={'manifest_uri': URIRef(manifest_uri)}) return list(qres)
def image_annotations(manifest_uri, g): bind_namespaces(g) query = """SELECT DISTINCT ?resource_uri ?resource_url WHERE { ?manifest_uri ore:aggregates ?resource_uri . ?resource_uri rdf:type dms:ImageAnnotationList . OPTIONAL { ?resource_url ore:describes ?resource_uri } . OPTIONAL { ?resource_uri ore:isDescribedBy ?resource_url } }""" qres = g.query(query, initNs=ns, initBindings={'manifest_uri': URIRef(manifest_uri)}) return list(qres)
def resource_urls(manifest_uri, g): bind_namespaces(g) query = """SELECT DISTINCT ?resource_url WHERE { ?manifest_uri ore:aggregates ?resource_uri . ?resource_url ore:describes ?resource_uri }""" qres = g.query(query, initNs=ns, initBindings={'manifest_uri': URIRef(manifest_uri)}) if len(qres) == 0: query = """SELECT DISTINCT ?resource_url WHERE { ?manifest_uri ore:aggregates ?resource_uri . ?resource_uri ore:isDescribedBy ?resource_url }""" qres = g.query(query, initNs=ns, initBindings={'manifest_uri': URIRef(manifest_uri)}) return qres
def create_project_graph(host, user, title, project): if not project: project = uris.uuid() elif not (type(project) == URIRef): project = URIRef(project) g = Graph() bind_namespaces(g) if not title: title = "Default Project" g.add((project, NS.rdf.type, NS.foaf.Project)) g.add((project, NS.rdf.type, NS.dm.Project)) g.add((project, NS.rdf['type'], NS.dcmitype['Collection'])) g.add((project, NS.rdf['type'], NS.ore['Aggregation'])) g.add((project, NS.dc['title'], Literal(title))) g.add((project, NS.dcterms['created'], Literal(datetime.utcnow()))) return g
def create_project_graph(host, user, title, project): if not project: project = uris.uuid() elif not (type(project) == URIRef): project = URIRef(project) g = Graph() bind_namespaces(g) if not title: title = "Default Project" g.add((project, NS.rdf.type, NS.foaf.Project)) g.add((project, NS.rdf.type, NS.dm.Project)) g.add((project, NS.rdf['type'], NS.dcmitype['Collection'])) g.add((project, NS.rdf['type'], NS.ore['Aggregation'])) g.add((project, NS.dc['title'], Literal(title))) g.add((project, NS.dcterms['created'], Literal(datetime.utcnow()))) return g
def print_triples(triples): """Prints a turtle serialization of an iterable of triples""" g = Graph() bind_namespaces(g) g += triples serialization = g.serialize(format='turtle') # Remove the @prefix section for easier readability last_match = None for match in re.finditer('^@prefix.+\.$', serialization, flags=re.MULTILINE): last_match = match if last_match: print serialization[last_match.end() + 1:].strip('\n') else: print serialization
def print_triples(triples): """Prints a turtle serialization of an iterable of triples""" g = Graph() bind_namespaces(g) g += triples serialization = g.serialize(format='turtle') # Remove the @prefix section for easier readability last_match = None for match in re.finditer('^@prefix.+\.$', serialization, flags=re.MULTILINE): last_match = match if last_match: print serialization[last_match.end() + 1:].strip('\n') else: print serialization
def read_project_text(project_uri, text_uri): # Correctly format project uri and get project graph project_identifier = uris.uri('semantic_store_projects', uri=project_uri) project_g = Graph(rdfstore(), identifier=project_identifier) # Make text uri URIRef (so Graph will understand) text_uri = URIRef(text_uri) # Create an empty graph and bind namespaces text_g = Graph() bind_namespaces(text_g) text_g += resource_annotation_subgraph(project_g, text_uri) text_g += specific_resources_subgraph(project_g, text_uri, project_uri) overwrite_text_graph_from_model(text_uri, project_uri, text_g) # Return graph about text return text_g
def resource_urls(manifest_uri, g): bind_namespaces(g) query = """SELECT DISTINCT ?resource_url WHERE { ?manifest_uri ore:aggregates ?resource_uri . ?resource_url ore:describes ?resource_uri }""" qres = g.query(query, initNs=ns, initBindings={'manifest_uri': URIRef(manifest_uri)}) if len(qres) == 0: query = """SELECT DISTINCT ?resource_url WHERE { ?manifest_uri ore:aggregates ?resource_uri . ?resource_uri ore:isDescribedBy ?resource_url }""" qres = g.query(query, initNs=ns, initBindings={'manifest_uri': URIRef(manifest_uri)}) return qres
def resource_uris_urls_old(manifest_uri, g): bind_namespaces(g) query = """SELECT DISTINCT ?resource_uri ?resource_url WHERE { ?manifest_uri ore:aggregates ?resource_uri . ?resource_url ore:describes ?resource_uri }""" qres = g.query(query, initNs=ns, initBindings={'manifest_uri': URIRef(manifest_uri)}) uris_urls = set(qres) query = """SELECT DISTINCT ?resource_uri ?resource_url WHERE { ?manifest_uri ore:aggregates ?resource_uri . ?resource_uri ore:isDescribedBy ?resource_url }""" qres = g.query(query, initNs=ns, initBindings={'manifest_uri': URIRef(manifest_uri)}) for i in qres: uris_urls.add(i) return uris_urls
def read_project_text(project_uri, text_uri): # Correctly format project uri and get project graph project_identifier = uris.uri('semantic_store_projects', uri=project_uri) project_g = Graph(rdfstore(), identifier=project_identifier) # Make text uri URIRef (so Graph will understand) text_uri = URIRef(text_uri) # Create an empty graph and bind namespaces text_g = Graph() bind_namespaces(text_g) text_g += resource_annotation_subgraph(project_g, text_uri) text_g += specific_resources_subgraph(project_g, text_uri, project_uri) overwrite_text_graph_from_model(text_uri, project_uri, text_g) # Return graph about text return text_g
def page_attributes(g, page_uri, res_uri): bind_namespaces(g) qres = g.query("""SELECT DISTINCT ?res_title ?title ?width ?height ?image WHERE { {?res_uri dc:title ?res_title} UNION {?res_uri rdfs:label ?res_title} . {?page_uri dc:title ?title} UNION {?page_uri rdfs:label ?title} . ?page_uri exif:width ?width . ?page_uri exif:height ?height . ?anno oa:hasTarget ?page_uri . ?anno oa:hasBody ?image . ?image rdf:type dcmitype:Image . }""", initBindings={ 'res_uri': URIRef(res_uri), 'page_uri': URIRef(page_uri) }) if qres: (res_title, page_title, width, height, image) = list(qres)[0] return (unicode(res_title), unicode(page_title), int(width), int(height), unicode(image)) else: return None, None, None, None, None
def __init__(self, request, graph, *args, **kwargs): mimetypes = list(accept_mimetypes(request.META['HTTP_ACCEPT'])) if settings.DEBUG and mimetypes[0].strip().lower().endswith('html'): format = NegotiatedGraphResponse.default_format kwargs['mimetype'] = '%s/%s' % (NegotiatedGraphResponse.default_type, NegotiatedGraphResponse.default_format) else: for mimetype in mimetypes: format = mimetype[mimetype.rfind('/') + 1:].strip().lower() if format in RDFLIB_SERIALIZER_FORMATS: kwargs['mimetype'] = '%s/%s' % (NegotiatedGraphResponse.default_type, NegotiatedGraphResponse.default_format) break else: format = NegotiatedGraphResponse.default_format kwargs['mimetype'] = '%s/%s' % (NegotiatedGraphResponse.default_type, NegotiatedGraphResponse.default_format) super(NegotiatedGraphResponse, self).__init__(self, *args, **kwargs) bind_namespaces(graph) self.content = graph.serialize(format=format)
def resources(request, uri, ext=None): if request.user.is_authenticated(): perms = ProjectPermission.objects.filter(user=request.user) else: perms = [] uri = uri.rstrip('/') store_g = Graph(store=rdfstore(), identifier=URIRef(uri)) g = Graph() g += store_g if len(g) > 0: for i in perms: anno_uri = settings.URI_MINT_BASE \ + "/projects/" + i.identifier \ + "/resources/" + uri \ + "/annotations/" anno_url = reverse('semantic_store_project_annotations', kwargs={'project_uri': i.identifier}) \ + "?uri=" + uri g.add((URIRef(uri), NS.ore['aggregates'], URIRef(anno_uri))) g.add( (URIRef(anno_uri), NS.ore['isDescribedBy'], URIRef(anno_url))) g.add((URIRef(anno_uri), NS.rdf['type'], NS.ore['Aggregation'])) g.add((URIRef(anno_uri), NS.rdf['type'], NS.rdf['List'])) g.add((URIRef(anno_uri), NS.rdf['type'], NS.dms['AnnotationList'])) return NegotiatedGraphResponse(request, g) else: main_graph_store = ConjunctiveGraph(store=rdfstore(), identifier=default_identifier) main_graph = Graph() main_graph += main_graph_store g = Graph() bind_namespaces(g) for t in main_graph.triples((URIRef(uri), None, None)): g.add(t) if len(g) > 0: return NegotiatedGraphResponse(request, g) else: return HttpResponseNotFound()
def page_attributes(g, page_uri, res_uri): bind_namespaces(g) qres = g.query("""SELECT DISTINCT ?res_title ?title ?width ?height ?image WHERE { {?res_uri dc:title ?res_title} UNION {?res_uri rdfs:label ?res_title} . {?page_uri dc:title ?title} UNION {?page_uri rdfs:label ?title} . ?page_uri exif:width ?width . ?page_uri exif:height ?height . ?anno oa:hasTarget ?page_uri . ?anno oa:hasBody ?image . ?image rdf:type dcmitype:Image . }""", initBindings={ 'res_uri': URIRef(res_uri), 'page_uri': URIRef(page_uri) }) if qres: (res_title, page_title, width, height, image) = list(qres)[0] return (unicode(res_title), unicode(page_title), int(width), int(height), unicode(image)) else: return None, None, None, None, None
def resources(request, uri, ext=None): if request.user.is_authenticated(): perms = ProjectPermission.objects.filter(user=request.user) else: perms = [] uri = uri.rstrip('/') store_g = Graph(store=rdfstore(), identifier=URIRef(uri)) g = Graph() g += store_g if len(g) > 0: for i in perms: anno_uri = settings.URI_MINT_BASE \ + "/projects/" + i.identifier \ + "/resources/" + uri \ + "/annotations/" anno_url = reverse('semantic_store_project_annotations', kwargs={'project_uri': i.identifier}) \ + "?uri=" + uri g.add((URIRef(uri), NS.ore['aggregates'], URIRef(anno_uri))) g.add((URIRef(anno_uri), NS.ore['isDescribedBy'], URIRef(anno_url))) g.add((URIRef(anno_uri), NS.rdf['type'], NS.ore['Aggregation'])) g.add((URIRef(anno_uri), NS.rdf['type'], NS.rdf['List'])) g.add((URIRef(anno_uri), NS.rdf['type'], NS.dms['AnnotationList'])) return NegotiatedGraphResponse(request, g) else: main_graph_store = ConjunctiveGraph(store=rdfstore(), identifier=default_identifier) main_graph = Graph() main_graph += main_graph_store g = Graph() bind_namespaces(g) for t in main_graph.triples((URIRef(uri), None, None)): g.add(t) if len(g) > 0: return NegotiatedGraphResponse(request, g) else: return HttpResponseNotFound()
def resource_uris_urls_old(manifest_uri, g): bind_namespaces(g) query = """SELECT DISTINCT ?resource_uri ?resource_url WHERE { ?manifest_uri ore:aggregates ?resource_uri . ?resource_url ore:describes ?resource_uri }""" qres = g.query(query, initNs=ns, initBindings={'manifest_uri': URIRef(manifest_uri)}) uris_urls = set(qres) query = """SELECT DISTINCT ?resource_uri ?resource_url WHERE { ?manifest_uri ore:aggregates ?resource_uri . ?resource_uri ore:isDescribedBy ?resource_url }""" qres = g.query(query, initNs=ns, initBindings={'manifest_uri': URIRef(manifest_uri)}) for i in qres: uris_urls.add(i) return uris_urls
def graph(): g = Graph() bind_namespaces(g) return g
def graph(): g = Graph() bind_namespaces(g) return g
def serialization_iterator(project_uri, format): yield '' export_graph = project_export_graph(project_uri) bind_namespaces(export_graph) yield export_graph.serialize(format=format)
def serialization_iterator(project_uri, format): yield '' export_graph = project_export_graph(project_uri) bind_namespaces(export_graph) yield export_graph.serialize(format=format)