def destination_graph(dest_graph_uri=None): if dest_graph_uri: dest_g = Graph(store=rdfstore(), identifier=URIRef(dest_graph_uri)) else: uri = reverse('semantic_store_annotations') dest_g = Graph(store=rdfstore(), identifier=URIRef(uri)) return dest_g
def harvest_collection(col_url, col_uri, store_host, manifest_file=None): store_host = clean_store_host(store_host) with transaction.commit_on_success(): col_g = Graph(store=rdfstore(), identifier=URIRef(col_uri)) collection.fetch_and_parse(col_url, col_g, manifest_file=manifest_file) localize_describes(store_host, col_uri, col_url, col_g) res_uris_urls = collection.aggregated_uris_urls(col_uri, col_g) for res_uri, res_url in res_uris_urls: res_g = Graph(store=rdfstore(), identifier=URIRef(res_uri)) collection.fetch_and_parse(res_url, res_g) for pred in col_res_attributes: for t in res_g.triples((res_uri, pred, None)): col_g.add(t) aggr_uris_urls = collection.aggregated_uris_urls(res_uri, res_g) for aggr_uri, aggr_url in aggr_uris_urls: if aggr_url: collection.fetch_and_parse(aggr_url, res_g) localize_describes(store_host, aggr_uri, aggr_url, res_g) localize_describes(store_host, aggr_uri, aggr_url, col_g) seq_uris_urls = collection.aggregated_seq_uris_urls(res_uri, res_g) for seq_uri, seq_url in seq_uris_urls: page_uris_urls = collection.aggregated_uris_urls( seq_uri, res_g) for page_uri, page_url in page_uris_urls: localize_describes(store_host, page_uri, page_url, res_g) localize_describes(store_host, res_uri, res_url, res_g) localize_describes(store_host, res_uri, res_url, col_g)
def remove_project_text(project_uri, text_uri): # Correctly format project uri and get project graph project_uri = uris.uri('semantic_store_projects', uri=project_uri) project_g = Graph(rdfstore(), identifier=project_uri) project_metadata_g = Graph( rdfstore(), identifier=uris.project_metadata_graph_identifier(p_uri)) # Make text uri a URIRef (so Graph will understand) text_uri = URIRef(text_uri) with transaction.commit_on_success(): for t in specific_resources_subgraph(project_g, text_uri, project_uri): project_g.remove(t) for t in project_g.triples((text_uri, None, None)): # Delete triple about text from project graph project_g.remove(t) project_metadata_g.remove(t) project_g.remove((URIRef(project_uri), NS.ore.aggregates, text_uri)) for text in Text.objects.filter(identifier=text_uri, valid=True).only('valid'): text.valid = False text.save()
def harvest_collection(col_url, col_uri, store_host, manifest_file=None): store_host = clean_store_host(store_host) with transaction.commit_on_success(): col_g = Graph(store=rdfstore(), identifier=URIRef(col_uri)) collection.fetch_and_parse(col_url, col_g, manifest_file=manifest_file) localize_describes(store_host, col_uri, col_url, col_g) res_uris_urls = collection.aggregated_uris_urls(col_uri, col_g) for res_uri, res_url in res_uris_urls: res_g = Graph(store=rdfstore(), identifier=URIRef(res_uri)) collection.fetch_and_parse(res_url, res_g) for pred in col_res_attributes: for t in res_g.triples((res_uri, pred, None)): col_g.add(t) aggr_uris_urls = collection.aggregated_uris_urls(res_uri, res_g) for aggr_uri, aggr_url in aggr_uris_urls: if aggr_url: collection.fetch_and_parse(aggr_url, res_g) localize_describes(store_host, aggr_uri, aggr_url, res_g) localize_describes(store_host, aggr_uri, aggr_url, col_g) seq_uris_urls = collection.aggregated_seq_uris_urls(res_uri, res_g) for seq_uri, seq_url in seq_uris_urls: page_uris_urls = collection.aggregated_uris_urls(seq_uri, res_g) for page_uri, page_url in page_uris_urls: localize_describes(store_host, page_uri, page_url, res_g) localize_describes(store_host, res_uri, res_url, res_g) localize_describes(store_host, res_uri, res_url, col_g)
def remove_canvas_triples(project_uri, canvas_uri, input_graph): project_graph = Graph(store=rdfstore(), identifier=canvas_identifier) project_metadata_g = Graph(rdfstore(), identifier=uris.project_metadata_graph_identifier(project_uri)) removed_graph = Graph() for t in input_graph: if t in project_graph: project_graph.remove(t) project_metadata_g.remove(t) removed_graph.add(t) return removed_graph
def handle(self, *args, **options): col_url = options['url'] col_uri = options['uri'] store_host = options['store_host'] manifest_file = options['manifest_file'] rep_uri = options['rep_uri'] rep_title = options['rep_title'] if ((not ((col_url or manifest_file) and col_uri)) or (not (store_host and rep_uri and rep_title))): print "url or manifest_file and uri arguments are required." exit(0) rep_uri = URIRef(rep_uri) col_uri = URIRef(col_uri) rep_g = Graph(store=rdfstore(), identifier=URIRef(rep_uri)) rep_g.add((rep_uri, NS.rdf['type'], NS.dms['Manifest'])) rep_g.add((rep_uri, NS.rdf['type'], NS.ore['Aggregation'])) rep_g.add((rep_uri, NS.dc['title'], Literal(rep_title))) local_rel_url = reverse('semantic_store_resources', kwargs={'uri': str(rep_uri)}) local_abs_url = "http://%s%s" % (store_host, local_rel_url) rep_g.add((rep_uri, NS.ore['isDescribedBy'], URIRef(local_abs_url))) rep_g.add((rep_uri, NS.ore['aggregates'], col_uri)) rep_g.add((col_uri, NS.ore['isDescribedBy'], URIRef(col_url))) localize_describes(store_host, col_uri, col_url, rep_g) harvest_collection(col_url, col_uri, store_host, manifest_file)
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 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 get_project_metadata_graph(project_uri): """ Returns the database graph used as a cache to be returned when just the project contents overview is requested This should essentially contain the project title and description, what it aggregates, and just enough information for a GUI to render those contents (titles for texts, titles and image annos for canvases, etc.) """ return Graph(store=rdfstore(), identifier=uris.project_metadata_graph_identifier(project_uri))
def handle(self, *args, **options): col_url = options['url'] col_uri = options['uri'] store_host = options['store_host'] manifest_file = options['manifest_file'] rep_uri = options['rep_uri'] rep_title = options['rep_title'] if ((not ((col_url or manifest_file) and col_uri)) or (not (store_host and rep_uri and rep_title))): print "url or manifest_file and uri arguments are required." exit(0) rep_uri = URIRef(rep_uri) col_uri = URIRef(col_uri) rep_g = Graph(store=rdfstore(), identifier=URIRef(rep_uri)) rep_g.add((rep_uri, NS.rdf['type'], NS.dms['Manifest'])) rep_g.add((rep_uri, NS.rdf['type'], NS.ore['Aggregation'])) rep_g.add((rep_uri, NS.dc['title'], Literal(rep_title))) local_rel_url = reverse('semantic_store_resources' , kwargs={'uri': str(rep_uri)}) local_abs_url = "http://%s%s" % (store_host, local_rel_url) rep_g.add((rep_uri, NS.ore['isDescribedBy'], URIRef(local_abs_url))) rep_g.add((rep_uri, NS.ore['aggregates'], col_uri)) rep_g.add((col_uri, NS.ore['isDescribedBy'], URIRef(col_url))) localize_describes(store_host, col_uri, col_url, rep_g) harvest_collection(col_url, col_uri, store_host, manifest_file)
def handle(self, *args, **options): store = rdfstore() for u in User.objects.filter(): user_graph_identifier = uris.uri('semantic_store_users', username=u.username) user_graph = Graph(store=store, identifier=user_graph_identifier) print 'Updating user graph %s' % user_graph_identifier for project in user_graph.objects(None, NS.perm.hasPermissionOver): project_graph_identifier = uris.uri('semantic_store_projects', uri=project) project_graph = Graph(store=store, identifier=project_graph_identifier) if (len( list( project_graph.triples( (project, NS.rdf.type, NS.ore.Aggregation))))): with transaction.commit_on_success(): print '-Updating project graph %s' % project project_graph.add( (project, NS.rdf.type, NS.foaf.Project)) project_graph.add( (project, NS.rdf.type, NS.dm.Project))
def handle(self, *args, **options): username = options['username'] project_title = options['project_title'] res_uri = options['res_uri'] if (not (username and project_title and res_uri)): print "Username, project, and res are required arguments." exit(0) user_uri = uris.uri('semantic_store_users', username=username) user_g = Graph(store=rdfstore(), identifier=user_uri) # Project title data is stored in the named graph for that project, so we need # to query those graphs rather than just the user graph projectsByTitle = self.project_uris_by_title(user_g, user_uri) print "projectsByTitle: %s" % projectsByTitle.items() if project_title not in projectsByTitle: print "No such project with title '%s' found for user '%s'" % \ (project_title, username) print "User %s has projects entitled %s" % (username, projectsByTitle.keys()) exit(0) project_uris = projectsByTitle[project_title] if len(project_uris) > 1: print "More than one project with that title exists." exit(0) else: project_identifier = project_uris[0] with transaction.commit_on_success(): uri = uris.uri('semantic_store_projects', uri=project_identifier) project_g = Graph(store=rdfstore(), identifier=uri) project_g.add((project_identifier, NS.ore['aggregates'], URIRef(res_uri))) main_g = ConjunctiveGraph(store=rdfstore(), identifier=default_identifier) for t in main_g.triples( (URIRef(res_uri), NS.dc['title'], None)): project_g.add(t) for t in main_g.triples( (URIRef(res_uri), NS.ore['isDescribedBy'], None)): project_g.add(t) for t in main_g.triples( (URIRef(res_uri), NS.rdf['type'], None)): project_g.add(t)
def canvases(uri): g = Graph(store=rdfstore(), identifier=URIRef(uri)) subjs = [str(i) for i in g.subjects(NS.rdf['type'], NS.sc['Canvas'])] if len(subjs) == 0: subjs = [str(i) for i in g.subjects(NS.rdf['type'], NS.dms['Canvas'])] return subjs
def update_canvas_graph(project_uri, canvas_uri): identifier = uris.uri("semantic_store_project_canvases", project_uri=project_uri, canvas_uri=canvas_uri) canvas_graph = Graph(rdfstore(), identifier=identifier) for t in generate_canvas_graph(project_uri, canvas_uri): canvas_graph.add(t) return canvas_graph
def harvest_repository(rep_uri, rep_url, store_host, manifest_file=None): store_host = clean_store_host(store_host) with transaction.commit_on_success(): rep_g = Graph(store=rdfstore(), identifier=URIRef(rep_uri)) collection.fetch_and_parse(rep_url, rep_g) localize_describes(store_host, rep_uri, rep_url, rep_g) agg_uris_urls = collection.aggregated_uris_urls(rep_uri, rep_g) for agg_uri, agg_url in agg_uris_urls: harvest_collection(agg_url, agg_uri, store_host, manifest_file)
def handle(self, directory, *args, **kwargs): store = rdfstore() for filename in os.listdir(directory): full_path = os.path.join(directory, filename) if os.path.isfile(full_path) and not filename.startswith('.'): context = URIRef(urllib.unquote(filename[:filename.rfind('.')])) graph = Graph(store, context) graph.parse(full_path, format=guess_format(filename))
def update_specific_resource(graph, project_uri, specific_resource_uri): project_identifier = uris.uri('semantic_store_projects', uri=project_uri) db_project_graph = Graph(store=rdfstore(), identifier=project_identifier) for t in specific_resource_subgraph(graph, specific_resource_uri): db_project_graph.add(t) for selector in graph.objects(specific_resource_uri, NS.oa.hasSelector): for i in graph.triples((selector, None, None)): db_project_graph.set(i)
def handle(self, *args, **options): store = rdfstore() print 'About to update every graph in the store... go grab some coffee.' for identifier in store.contexts(): print '- Updating graph with identifier %s' % identifier graph = Graph(store=store, identifier=identifier) update_old_namespaces(graph) graph.bind('oa', NS.oa)
def update_canvas(project_uri, canvas_uri, input_graph): project_uri = URIRef(project_uri) canvas_uri = URIRef(canvas_uri) project_identifier = uris.uri('semantic_store_projects', uri=project_uri) project_graph = Graph(store=rdfstore(), identifier=project_identifier) project_metadata_g = Graph(rdfstore(), identifier=uris.project_metadata_graph_identifier(project_uri)) if (canvas_uri, NS.dc.title, None) in input_graph: project_graph.remove((canvas_uri, NS.dc.title, None)) project_metadata_g.remove((canvas_uri, NS.dc.title, None)) if (canvas_uri, NS.rdfs.label, None) in input_graph: project_graph.remove((canvas_uri, NS.rdfs.label, None)) project_metadata_g.remove((canvas_uri, NS.rdfs.label, None)) project_graph += input_graph project_metadata_g += canvas_and_images_graph(input_graph, canvas_uri) return project_graph
def handle(self, directory, *args, **kwargs): store = rdfstore() for filename in os.listdir(directory): full_path = os.path.join(directory, filename) if os.path.isfile(full_path) and not filename.startswith('.'): context = URIRef(urllib.unquote( filename[:filename.rfind('.')])) graph = Graph(store, context) graph.parse(full_path, format=guess_format(filename))
def create_project(g): """Creates a project in the database (and the metadata cache) from an input graph""" print "Here 1" query = g.query("""SELECT ?uri ?user WHERE { ?user perm:hasPermissionOver ?uri . ?user rdf:type foaf:Agent . }""", initNs=ns) print "Here 2" for uri in g.subjects(NS.rdf.type, NS.dm.Project): print "Here 3" user = g.value(None, NS.perm.hasPermissionOver, uri) if user: print "Here 3.1" user_obj = User.objects.get(username=user.split('/')[-1]) print "Here 3.2" project_identifier = uris.uri('semantic_store_projects', uri=uri) print "Here 3.3" project_g = Graph(store=rdfstore(), identifier=project_identifier) print "Here 4" for text_uri in g.subjects(NS.rdf.type, NS.dcmitype.Text): text_graph = Graph() text_graph += g.triples((text_uri, None, None)) print "Here 4.1" if user: project_texts.update_project_text(text_graph, uri, text_uri, user_obj) print "Here 5" for t in g: project_g.add(t) for text_uri in g.subjects(NS.rdf.type, NS.dcmitype.Text): project_g.remove((text_uri, NS.cnt.chars, None)) url = uris.url('semantic_store_projects', uri=uri) project_g.set((uri, NS.dcterms['created'], Literal(datetime.utcnow()))) print "before user" if user: print "user is true" project_g.remove((user, None, None)) username = user.split("/")[-1] permissions.grant_full_project_permissions(username, uri) add_project_types(project_g, uri) build_project_metadata_graph(uri) print "Successfully created project with uri " + uri
def update_project_text(g, p_uri, t_uri, user): # Correctly format project uri and get project graph project_uri = uris.uri('semantic_store_projects', uri=p_uri) project_g = Graph(rdfstore(), identifier=project_uri) project_metadata_g = Graph(rdfstore(), identifier=uris.project_metadata_graph_identifier(p_uri)) text_uri = URIRef(t_uri) title = g.value(text_uri, NS.dc.title) or g.value(text_uri, NS.rdfs.label) or Literal("") content_value = g.value(text_uri, NS.cnt.chars) if content_value: content = sanitized_content(content_value) else: content = '' with transaction.commit_on_success(): for t in Text.objects.filter(identifier=t_uri, valid=True): t.valid = False t.save() # While it looks like this would be better with a QuerySet update, we need to fire the save # events to keep the search index up to date. In all forseeable cases, this should only execute # for one Text object anyway. text = Text.objects.create(identifier=t_uri, title=title, content=content, last_user=user, project=p_uri) project_g.add((text_uri, NS.rdf.type, NS.dctypes.Text)) project_g.set((text_uri, NS.dc.title, title)) project_g.set((text_uri, NS.rdfs.label, title)) text_url = URIRef(uris.url('semantic_store_project_texts', project_uri=p_uri, text_uri=text_uri)) project_g.set((text_uri, NS.ore.isDescribedBy, text_url)) if (URIRef(p_uri), NS.ore.aggregates, text_uri) in project_metadata_g: project_metadata_g.add((text_uri, NS.rdf.type, NS.dctypes.Text)) project_metadata_g.set((text_uri, NS.dc.title, title)) project_metadata_g.set((text_uri, NS.rdfs.label, title)) specific_resource_triples = specific_resources_subgraph(g, text_uri, p_uri) for t in specific_resource_triples: project_g.add(t) for t in g.triples((None, NS.rdf.type, NS.oa.TextQuoteSelector)): project_g.set(t)
def permission_updates_are_allowed(request, input_graph): for perm_predicate in PERMISSION_PREDICATES: for user, p, project in input_graph.triples((None, perm_predicate, None)): has_admin_permissions = has_permission_over(project, user=request.user, permission=NS.perm.mayAdminister) project_graph = Graph(store=rdfstore(), identifier=uris.uri('semantic_store_projects', uri=project)) is_empty_project = (project, NS.ore.aggregates, None) not in project_graph if not has_admin_permissions and not is_unowned_project(project) and not is_empty_project: return False return True
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 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 remove_project_text(project_uri, text_uri): # Correctly format project uri and get project graph project_uri = uris.uri('semantic_store_projects', uri=project_uri) project_g = Graph(rdfstore(), identifier=project_uri) project_metadata_g = Graph(rdfstore(), identifier=uris.project_metadata_graph_identifier(p_uri)) # Make text uri a URIRef (so Graph will understand) text_uri = URIRef(text_uri) with transaction.commit_on_success(): for t in specific_resources_subgraph(project_g, text_uri, project_uri): project_g.remove(t) for t in project_g.triples((text_uri, None, None)): # Delete triple about text from project graph project_g.remove(t) project_metadata_g.remove(t) project_g.remove((URIRef(project_uri), NS.ore.aggregates, text_uri)) for text in Text.objects.filter(identifier=text_uri, valid=True).only('valid'): text.valid = False text.save()
def user_graph(request, username=None, user=None): if user is None: user = User.objects.get(username=username) if username is None: username = user.username user_graph = Graph(store=rdfstore(), identifier=USER_GRAPH_IDENTIFIER) user_uri = URIRef(uris.uri('semantic_store_users', username=username)) graph = Graph() graph += user_metadata_graph(user=user) graph += user_graph.triples((user_uri, NS.dm.lastOpenProject, None)) for permission in ProjectPermission.objects.filter(user=user): perm_uri = PERMISSION_URIS_BY_MODEL_VALUE[permission.permission] project_uri = URIRef(permission.identifier) graph.add((user_uri, NS.perm.hasPermissionOver, project_uri)) graph.add((user_uri, perm_uri, project_uri)) # Add metadata info about projects for project in graph.objects(user_uri, NS.perm.hasPermissionOver): project_graph_identifier = URIRef(uris.uri('semantic_store_projects', uri=project)) project_graph = Graph(store=rdfstore(), identifier=project_graph_identifier) project_url = uris.url("semantic_store_projects", uri=project) graph.add((project, NS.ore.isDescribedBy, URIRef(project_url))) for t in metadata_triples(project_graph, project): graph.add(t) #TODO: dm:lastOpenProject return graph
def handle(self, directory, format, overwrite, *args, **kwargs): store = rdfstore() if not os.path.isdir(directory): os.mkdir(directory) if not overwrite and len(os.listdir(directory)) > 0: raise Exception('The directory "%s" is not empty, and the --overwrite flag was not set' % directory) if format.startswith('.'): format = format[1:] for context in store.contexts(): graph = Graph(store, context) filename = urllib.quote(context, '') graph.serialize(os.path.abspath(os.path.join(directory, '%s.%s' % (filename, format))))
def handle(self, *args, **options): store = rdfstore() for u in User.objects.filter(): user_graph_identifier = uris.uri('semantic_store_users', username=u.username) user_graph = Graph(store=store, identifier=user_graph_identifier) print 'Updating user graph %s' % user_graph_identifier for project in user_graph.objects(None, NS.perm.hasPermissionOver): project_graph_identifier = uris.uri('semantic_store_projects', uri=project) project_graph = Graph(store=store, identifier=project_graph_identifier) if (len(list(project_graph.triples((project, NS.rdf.type, NS.ore.Aggregation))))): with transaction.commit_on_success(): print '-Updating project graph %s' % project project_graph.add((project, NS.rdf.type, NS.foaf.Project)) project_graph.add((project, NS.rdf.type, NS.dm.Project))
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 generate_canvas_graph(project_uri, canvas_uri): project_identifier = uris.uri('semantic_store_projects', uri=project_uri) db_project_graph = Graph(store=rdfstore(), identifier=project_identifier) project_graph = db_project_graph memory_graph = Graph() memory_graph += canvas_subgraph(project_graph, canvas_uri, project_uri) for text in memory_graph.subjects(NS.rdf.type, NS.dcmitype.Text): if (text, NS.ore.isDescribedBy, None) not in memory_graph: text_url = uris.url('semantic_store_project_texts', project_uri=project_uri, text_uri=text) memory_graph.add((text, NS.ore.isDescribedBy, text_url)) for canvas in memory_graph.subjects(NS.rdf.type, NS.sc.Canvas): if (canvas, NS.ore.isDescribedBy, None) not in memory_graph: canvas_url = uris.url('semantic_store_project_canvases', project_uri=project_uri, canvas_uri=canvas) memory_graph.add((canvas, NS.ore.isDescribedBy, canvas_url)) return memory_graph
def read_specific_resource(project_uri, specific_resource, source): specific_resource = URIRef(specific_resource) project_identifier = uris.uri('semantic_store_projects', uri=project_uri) db_project_graph = Graph(store=rdfstore(), identifier=project_identifier) project_graph = db_project_graph return_graph = Graph() return_graph += project_graph.triples((specific_resource, None, None)) selectors = project_graph.objects(specific_resource, NS.oa.hasSelector) for selector in selectors: return_graph += project_graph.triples((selector, None, None)) if (URIRef(source), NS.rdf.type, NS.sc.Canvas) in project_graph: return_graph.add((specific_resource, NS.ore.isDescribedBy, URIRef(uris.url("semantic_store_canvas_specific_resource", project_uri=project_uri, canvas_uri=source, specific_resource=specific_resource)))) elif (URIRef(source), NS.rdf.type, NS.dcmitype.Text) in project_graph: return_graph.add((specific_resource, NS.ore.isDescribedBy, URIRef(uris.url("semantic_store_text_specific_resource", project_uri=project_uri, text_uri=source, specific_resource=specific_resource)))) return_graph += resource_annotation_subgraph(project_graph, specific_resource) return return_graph
def read_specific_resource(project_uri, specific_resource, source): specific_resource = URIRef(specific_resource) project_identifier = uris.uri('semantic_store_projects', uri=project_uri) db_project_graph = Graph(store=rdfstore(), identifier=project_identifier) project_graph = db_project_graph return_graph = Graph() return_graph += project_graph.triples((specific_resource, None, None)) selectors = project_graph.objects(specific_resource, NS.oa.hasSelector) for selector in selectors: return_graph += project_graph.triples((selector, None, None)) if (URIRef(source), NS.rdf.type, NS.sc.Canvas) in project_graph: return_graph.add( (specific_resource, NS.ore.isDescribedBy, URIRef( uris.url("semantic_store_canvas_specific_resource", project_uri=project_uri, canvas_uri=source, specific_resource=specific_resource)))) elif (URIRef(source), NS.rdf.type, NS.dcmitype.Text) in project_graph: return_graph.add((specific_resource, NS.ore.isDescribedBy, URIRef( uris.url("semantic_store_text_specific_resource", project_uri=project_uri, text_uri=source, specific_resource=specific_resource)))) return_graph += resource_annotation_subgraph(project_graph, specific_resource) return return_graph
def read_canvas(request, project_uri, canvas_uri): project_identifier = uris.uri('semantic_store_projects', uri=project_uri) db_project_graph = Graph(store=rdfstore(), identifier=project_identifier) return canvas_subgraph(db_project_graph, canvas_uri, project_uri)
def handle(self, *args, **options): with transaction.commit_on_success(): main_graph = ConjunctiveGraph(rdfstore(), identifier=default_identifier) for t in main_graph.triples((None, None, None)): main_graph.remove(t)
def get_project_graph(project_uri): """Returns the database graph used to store general information about the project with the given uri""" return Graph(store=rdfstore(), identifier=uris.uri('semantic_store_projects', uri=project_uri))
def update_project_text(g, p_uri, t_uri, user): logger.debug("************* Updating project") # Correctly format project uri and get project graph project_uri = uris.uri('semantic_store_projects', uri=p_uri) logger.debug("!!!!!!!!!!!!!! Here 1") project_g = Graph(rdfstore(), identifier=project_uri) logger.debug("!!!!!!!!!!!!!! Here 2") project_metadata_g = Graph(rdfstore(), identifier=uris.project_metadata_graph_identifier(p_uri)) logger.debug("!!!!!!!!!!!!!! Here 3") text_uri = URIRef(t_uri) logger.debug("!!!!!!!!!!!!!! Here 4") title = g.value(text_uri, NS.dc.title) or g.value(text_uri, NS.rdfs.label) or Literal("") logger.debug("!!!!!!!!!!!!!! Here 5") content_value = g.value(text_uri, NS.cnt.chars) logger.debug("!!!!!!!!!!!!!! Here 6") logger.debug("g: %s", g) logger.debug("g.value: %s", g.value) logger.debug("text_uri: %s", text_uri) logger.debug("NS.cnt.chars: %s", NS.cnt.chars) logger.debug("content_value: %s", content_value) if content_value: content = sanitized_content(content_value) logger.debug("!!!!!!!!!!!!!! Here 6.1") else: content = '' logger.debug("!!!!!!!!!!!!!! Here 6.2") with transaction.commit_on_success(): logger.debug("!!!!!!!!!!!!!! Here 7") for t in Text.objects.filter(identifier=t_uri, valid=True): logger.debug("!!!!!!!!!!!!!! Here 8") t.valid = False logger.debug("!!!!!!!!!!!!!! Here 9") t.save() logger.debug("!!!!!!!!!!!!!! Here 10") # While it looks like this would be better with a QuerySet update, we need to fire the save # events to keep the search index up to date. In all forseeable cases, this should only execute # for one Text object anyway. text = Text.objects.create(identifier=t_uri, title=title, content=content, last_user=user, project=p_uri) logger.debug("!!!!!!!!!!!!!! Here 11") project_g.add((text_uri, NS.rdf.type, NS.dctypes.Text)) logger.debug("!!!!!!!!!!!!!! Here 12") project_g.set((text_uri, NS.dc.title, title)) logger.debug("!!!!!!!!!!!!!! Here 13") project_g.set((text_uri, NS.rdfs.label, title)) logger.debug("!!!!!!!!!!!!!! Here 14") text_url = URIRef(uris.url('semantic_store_project_texts', project_uri=p_uri, text_uri=text_uri)) logger.debug("!!!!!!!!!!!!!! Here 15") project_g.set((text_uri, NS.ore.isDescribedBy, text_url)) logger.debug("!!!!!!!!!!!!!! Here 16") if (URIRef(p_uri), NS.ore.aggregates, text_uri) in project_metadata_g: logger.debug("!!!!!!!!!!!!!! Here 16.1") project_metadata_g.add((text_uri, NS.rdf.type, NS.dctypes.Text)) logger.debug("!!!!!!!!!!!!!! Here 16.2") project_metadata_g.set((text_uri, NS.dc.title, title)) logger.debug("!!!!!!!!!!!!!! Here 16.3") project_metadata_g.set((text_uri, NS.rdfs.label, title)) logger.debug("!!!!!!!!!!!!!! Here 16.4") logger.debug("!!!!!!!!!!!!!! Here 17") specific_resource_triples = specific_resources_subgraph(g, text_uri, p_uri) logger.debug("!!!!!!!!!!!!!! Here 18") for t in specific_resource_triples: logger.debug("!!!!!!!!!!!!!! Here 19") project_g.add(t) for t in g.triples((None, NS.rdf.type, NS.oa.TextQuoteSelector)): logger.debug("!!!!!!!!!!!!!! Here 20") project_g.set(t)