Beispiel #1
0
def add_is_described_bys(request, project_uri, graph):
    for text in graph.subjects(NS.rdf.type, NS.dcmitype.Text):
        text_url = uris.url("semantic_store_project_texts", project_uri=project_uri, text_uri=text)
        graph.add((text, NS.ore.isDescribedBy, text_url))

    for canvas in graph.subjects(NS.rdf.type, NS.sc.Canvas):
        canvas_url = uris.url("semantic_store_project_canvases", project_uri=project_uri, canvas_uri=canvas)
        graph.add((canvas, NS.ore.isDescribedBy, canvas_url))

    for manuscript in graph.subjects(NS.rdf.type, NS.sc.Manifest):
        manuscript_url = uris.url("semantic_store_project_manuscripts", project_uri=project_uri, manuscript_uri=manuscript)
        graph.add((manuscript, NS.ore.isDescribedBy, manuscript_url))
Beispiel #2
0
def add_is_described_bys(request, project_uri, graph):
    for text in graph.subjects(NS.rdf.type, NS.dcmitype.Text):
        text_url = uris.url("semantic_store_project_texts", project_uri=project_uri, text_uri=text)
        graph.add((text, NS.ore.isDescribedBy, text_url))

    for canvas in graph.subjects(NS.rdf.type, NS.sc.Canvas):
        canvas_url = uris.url("semantic_store_project_canvases", project_uri=project_uri, canvas_uri=canvas)
        graph.add((canvas, NS.ore.isDescribedBy, canvas_url))

    for manuscript in graph.subjects(NS.rdf.type, NS.sc.Manifest):
        manuscript_url = uris.url("semantic_store_project_manuscripts", project_uri=project_uri, manuscript_uri=manuscript)
        graph.add((manuscript, NS.ore.isDescribedBy, manuscript_url))
Beispiel #3
0
def anno_lists_subgraph(graph, canvas_uri, project_uri):
    subgraph = Graph()

    lists_uri = graph.value(canvas_uri, NS.sc.hasLists)
    if lists_uri:
        subgraph += list_subgraph(graph, lists_uri)

        for anno_list in canvas_annotation_lists(graph, canvas_uri):
            subgraph += list_subgraph(graph, anno_list)

            hasAnnotations_uri = graph.value(anno_list, NS.sc.hasAnnotations)
            if hasAnnotations_uri:
                subgraph += list_subgraph(graph, hasAnnotations_uri)

                for anno in annotation_list_items(graph, anno_list):
                    subgraph += annotation_subgraph(graph, anno)

                    for s, p, resource in graph.triples_choices(
                        (anno, [NS.oa.hasBody, NS.oa.hasTarget], None)):
                        if (resource, NS.rdf.type, NS.cnt.ContentAsText
                            ) in graph and (resource, NS.rdf.type,
                                            NS.dcmitype.Text) not in graph:
                            transcription_url = URIRef(
                                uris.url('semantic_store_canvas_transcription',
                                         project_uri=project_uri,
                                         canvas_uri=canvas_uri,
                                         transcription_uri=resource))
                            subgraph.add((resource, NS.ore.isDescribedBy,
                                          transcription_url))

    return subgraph
Beispiel #4
0
    def manuscript_graph(self, manuscript_uri, project_uri):
        project_graph = get_project_graph(project_uri)
        subgraph = manuscripts.manuscript_subgraph(project_graph, manuscript_uri)

        for canvas in subgraph.subjects(NS.rdf.type, NS.sc.Canvas):
            if (canvas, NS.ore.isDescribedBy, None) not in subgraph:
                canvas_url = uris.url('semantic_store_project_canvases', project_uri=project_uri, canvas_uri=canvas)
                subgraph.add((canvas, NS.ore.isDescribedBy, canvas_url))

        return subgraph
Beispiel #5
0
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
Beispiel #6
0
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
Beispiel #7
0
    def manuscript_graph(self, manuscript_uri, project_uri):
        project_graph = get_project_graph(project_uri)
        subgraph = manuscripts.manuscript_subgraph(project_graph,
                                                   manuscript_uri)

        for canvas in subgraph.subjects(NS.rdf.type, NS.sc.Canvas):
            if (canvas, NS.ore.isDescribedBy, None) not in subgraph:
                canvas_url = uris.url('semantic_store_project_canvases',
                                      project_uri=project_uri,
                                      canvas_uri=canvas)
                subgraph.add((canvas, NS.ore.isDescribedBy, canvas_url))

        return subgraph
Beispiel #8
0
def search_result_to_dict(result, project_uri, highlighter, title_highlighter):
    stored_fields = result.get_stored_fields()

    d = {
        'uri': stored_fields['identifier'],
        'url': uris.url('semantic_store_project_texts', project_uri=project_uri, text_uri=stored_fields['identifier']),
        'type': NS.dctypes.Text,
        'title': stored_fields['title'],
        'score': result.score,
        'highlighted_text': highlighter.highlight(result.text),
        'highlighted_title': title_highlighter.highlight(stored_fields['title']),
    }

    return d
Beispiel #9
0
def specific_resources_subgraph(graph, source_uri, project_uri):
    specific_resources_graph = Graph()

    if (source_uri, NS.rdf.type, NS.sc.Canvas) in graph:
        source_type = NS.sc.Canvas
    elif (source_uri, NS.rdf.type, NS.dcmitype.Text) in graph:
        source_type = NS.dcmitype.Text
    else:
        source_type = None

    for specific_resource in graph.subjects(NS.oa.hasSource, source_uri):
        selector = graph.value(specific_resource, NS.oa.hasSelector)

        if selector:
            specific_resources_graph += graph.triples_choices(
                ([specific_resource, selector], None, None))

            # Add appropriate ore:isDescribedBy triples for each Specific Resource so the client can request annotations on that specific resource as needed
            if source_type == NS.sc.Canvas:
                specific_resources_graph.add(
                    (specific_resource, NS.ore.isDescribedBy,
                     URIRef(
                         uris.url("semantic_store_canvas_specific_resource",
                                  project_uri=project_uri,
                                  canvas_uri=source_uri,
                                  specific_resource=specific_resource))))
            elif source_type == NS.dcmitype.Text:
                specific_resources_graph.add(
                    (specific_resource, NS.ore.isDescribedBy,
                     URIRef(
                         uris.url("semantic_store_text_specific_resource",
                                  project_uri=project_uri,
                                  text_uri=source_uri,
                                  specific_resource=specific_resource))))

    return specific_resources_graph
Beispiel #10
0
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
Beispiel #11
0
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
Beispiel #12
0
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)
Beispiel #13
0
def anno_lists_subgraph(graph, canvas_uri, project_uri):
    subgraph = Graph()

    lists_uri = graph.value(canvas_uri, NS.sc.hasLists)
    if lists_uri:
        subgraph += list_subgraph(graph, lists_uri)

        for anno_list in canvas_annotation_lists(graph, canvas_uri):
            subgraph += list_subgraph(graph, anno_list)

            hasAnnotations_uri = graph.value(anno_list, NS.sc.hasAnnotations)
            if hasAnnotations_uri:
                subgraph += list_subgraph(graph, hasAnnotations_uri)

                for anno in annotation_list_items(graph, anno_list):
                    subgraph += annotation_subgraph(graph, anno)

                    for s, p, resource in graph.triples_choices((anno, [NS.oa.hasBody, NS.oa.hasTarget], None)):
                        if (resource, NS.rdf.type, NS.cnt.ContentAsText) in graph and (resource, NS.rdf.type, NS.dcmitype.Text) not in graph:
                            transcription_url = URIRef(uris.url('semantic_store_canvas_transcription', project_uri=project_uri, canvas_uri=canvas_uri, transcription_uri=resource))
                            subgraph.add((resource, NS.ore.isDescribedBy, transcription_url))

    return subgraph
Beispiel #14
0
def search_result_to_dict(result, project_uri, highlighter, title_highlighter):
    stored_fields = result.get_stored_fields()

    d = {
        'uri':
        stored_fields['identifier'],
        'url':
        uris.url('semantic_store_project_texts',
                 project_uri=project_uri,
                 text_uri=stored_fields['identifier']),
        'type':
        NS.dctypes.Text,
        'title':
        stored_fields['title'],
        'score':
        result.score,
        'highlighted_text':
        highlighter.highlight(result.text),
        'highlighted_title':
        title_highlighter.highlight(stored_fields['title']),
    }

    return d
Beispiel #15
0
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
Beispiel #16
0
def specific_resources_subgraph(graph, source_uri, project_uri):
    specific_resources_graph = Graph()

    if (source_uri, NS.rdf.type, NS.sc.Canvas) in graph:
        source_type = NS.sc.Canvas
    elif (source_uri, NS.rdf.type, NS.dcmitype.Text) in graph:
        source_type = NS.dcmitype.Text

    for specific_resource in graph.subjects(NS.oa.hasSource, source_uri):
        selector = graph.value(specific_resource, NS.oa.hasSelector)

        if selector:
            specific_resources_graph += graph.triples_choices(([specific_resource, selector], None, None))

            # Add appropriate ore:isDescribedBy triples for each Specific Resource so the client can request annotations on that specific resource as needed
            if source_type == NS.sc.Canvas:
                specific_resources_graph.add((specific_resource, NS.ore.isDescribedBy, URIRef(uris.url("semantic_store_canvas_specific_resource", project_uri=project_uri, canvas_uri=source_uri, specific_resource=specific_resource))))
            elif source_type == NS.dcmitype.Text:
                specific_resources_graph.add((specific_resource, NS.ore.isDescribedBy, URIRef(uris.url("semantic_store_text_specific_resource", project_uri=project_uri, text_uri=source_uri, specific_resource=specific_resource))))

    return specific_resources_graph
Beispiel #17
0
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)