def construct_insert_document(document_name,
                              file_uri,
                              graph=APPLICATION_GRAPH):
    document = {"name": document_name, "created": datetime.now(TIMEZONE)}
    document["uuid"] = generate_uuid()
    document["uri"] = DOCUMENT_BASE_URI + document["uuid"]
    query_template = Template("""
PREFIX dossier: <https://data.vlaanderen.be/ns/dossier#>
PREFIX mu: <http://mu.semte.ch/vocabularies/core/>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX nfo: <http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#>
PREFIX ext: <http://mu.semte.ch/vocabularies/ext/>

INSERT {
    GRAPH $graph {
        $document a dossier:Stuk ;
            mu:uuid $uuid ;
            dct:title $name ;
            dct:created $created ;
            ext:file $file .
    }
}
WHERE {
    GRAPH $graph {
        $file a nfo:FileDataObject .
    }
}
""")
    return query_template.substitute(
        graph=sparql_escape_uri(graph),
        document=sparql_escape_uri(document["uri"]),
        uuid=sparql_escape_string(document["uuid"]),
        name=sparql_escape_string(document["name"]),
        created=sparql_escape_datetime(document["created"]),
        file=sparql_escape_uri(file_uri))
def construct_insert_signinghub_session_query(signinghub_session,
                                              signinghub_session_uri):
    signinghub_token_uri = SIGNINGHUB_TOKEN_BASE_URI + generate_uuid()
    expiry_time = signinghub_session.last_successful_auth_time + signinghub_session.access_token_expiry_time
    query_template = Template("""
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX mu: <http://mu.semte.ch/vocabularies/core/>
PREFIX oauth-2.0: <http://kanselarij.vo.data.gift/vocabularies/oauth-2.0-session/>

INSERT DATA {
    GRAPH $session_graph {
        $signinghub_session a oauth-2.0:OauthSession ;
            oauth-2.0:hasEndpointURI $signinghub_token_endpoint ;
            oauth-2.0:hasTokenValue $token_uri .
        $token_uri a oauth-2.0:BearerToken ;
            dct:created $creation_time ;
            oauth-2.0:hasTokenValue $token_value ;
            oauth-2.0:hasExpirytime $expiry_time .
    }
}""")

    query_string = query_template.substitute(
        session_graph=sparql_escape_uri(SESSION_GRAPH),
        signinghub_session=sparql_escape_uri(signinghub_session_uri),
        signinghub_token_endpoint=sparql_escape_uri(SIGNINGHUB_OAUTH_TOKEN_EP),
        token_uri=sparql_escape_uri(signinghub_token_uri),
        creation_time=sparql_escape_datetime(
            signinghub_session.last_successful_auth_time.astimezone(TIMEZONE)),
        expiry_time=sparql_escape_datetime(expiry_time.astimezone(TIMEZONE)),
        token_value=sparql_escape_string(signinghub_session.access_token))
    return query_string
def construct_get_file_for_document(document_uri,
                                    file_mimetype=None,
                                    graph=APPLICATION_GRAPH):
    if file_mimetype is not None:
        format_filter = "FILTER( CONTAINS( ?format, {} ) )".format(
            sparql_escape_string(file_mimetype))
    else:
        format_filter = ""
    query_template = Template("""
PREFIX dossier: <https://data.vlaanderen.be/ns/dossier#>
PREFIX nfo: <http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX ext: <http://mu.semte.ch/vocabularies/ext/>

SELECT (?file AS ?uri)
WHERE {
    GRAPH $graph {
        $document a dossier:Stuk ;
            ext:file ?file .
        ?file a nfo:FileDataObject ;
            nfo:fileName ?name ;
            dct:format ?format .
        $format_filter
    }
}
LIMIT 1
""")
    return query_template.substitute(graph=sparql_escape_uri(graph),
                                     document=sparql_escape_uri(document_uri),
                                     format_filter=format_filter)
Exemplo n.º 4
0
def construct_get_mandatee_by_id(mandatee_id, graph=APPLICATION_GRAPH):
    query_template = Template("""
PREFIX mandaat: <http://data.vlaanderen.be/ns/mandaat#>
PREFIX mu: <http://mu.semte.ch/vocabularies/core/>

SELECT DISTINCT (?mandatee as ?uri)
WHERE {
    GRAPH $graph {
        ?mandatee a mandaat:Mandataris ;
            mu:uuid $uuid .
    }
}
""")
    return query_template.substitute(graph=sparql_escape_uri(graph),
                                     uuid=sparql_escape_string(mandatee_id))
Exemplo n.º 5
0
def construct_get_file_by_id(file_id, graph=APPLICATION_GRAPH):
    query_template = Template("""
PREFIX mu: <http://mu.semte.ch/vocabularies/core/>
PREFIX nfo: <http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#>

SELECT (?file AS ?uri)
WHERE {
    GRAPH $graph {
        ?file a nfo:FileDataObject ;
            mu:uuid $uuid .
    }
}
LIMIT 1
""")
    return query_template.substitute(graph=sparql_escape_uri(graph),
                                     uuid=sparql_escape_string(file_id))
def construct_get_document_by_uuid(document_uuid, graph=APPLICATION_GRAPH):
    query_template = Template("""
PREFIX dossier: <https://data.vlaanderen.be/ns/dossier#>
PREFIX mu: <http://mu.semte.ch/vocabularies/core/>

SELECT (?document AS ?uri)
WHERE {
    GRAPH $graph {
        ?document a dossier:Stuk ;
            mu:uuid $uuid .
    }
}
LIMIT 1
""")
    return query_template.substitute(graph=sparql_escape_uri(graph),
                                     uuid=sparql_escape_string(document_uuid))
Exemplo n.º 7
0
def assign_signers(signinghub_session: SigningHubSession, signflow_uri: str,
                   signer_uris: typing.List[str]):
    #TODO: validation: ensure signflow is in draft
    mandatees_query_command = _query_mandatees_template.substitute(
        graph=sparql_escape_uri(APPLICATION_GRAPH),
        mandatees=query_result_helpers.sparql_escape_list(
            [sparql_escape_uri(uri) for uri in signer_uris]))
    mandatee_result = query(mandatees_query_command)
    mandatee_records = query_result_helpers.to_recs(mandatee_result)
    mandatee_records_set = {r["mandatee"] for r in mandatee_records}
    mandatees_not_found = [
        uri for uri in signer_uris if uri not in mandatee_records_set
    ]
    if mandatees_not_found:
        raise exceptions.ResourceNotFoundException(
            ','.join(mandatees_not_found))

    signflow_record = signing_flow.get_signing_flow(signflow_uri)
    sh_package_id = signflow_record["sh_package_id"]
    sh_users = [{
        "user_email":
        r["email"],
        "user_name":
        ' '.join(
            [name for name in [r["first_name"], r["family_name"]] if name]),
        "role":
        "SIGNER"
    } for r in mandatee_records]
    signinghub_session.add_users_to_workflow(sh_package_id, sh_users)

    signing_activities = [
        _build_signing_activity(signer_uri) for signer_uri in signer_uris
    ]

    signing_activities_escaped = query_result_helpers.sparql_escape_table([[
        sparql_escape_uri(r["uri"]),
        sparql_escape_string(r["id"]),
        sparql_escape_uri(r["mandatee_uri"])
    ] for r in signing_activities])

    assign_signers_command = _assign_signers_template.substitute(
        graph=sparql_escape_uri(APPLICATION_GRAPH),
        signflow=sparql_escape_uri(signflow_uri),
        signing_activities=signing_activities_escaped)
    update(assign_signers_command)

    return signing_activities
def run_batch(batch_size, graph):

    documents = list(
        map(
            lambda res: res['doc']['value'],
            list(
                helpers.query(construct_select_docs_query(
                    batch_size, graph))['results']['bindings'])))

    res = helpers.query(construct_list_doc_versions_query(
        documents, graph))['results']['bindings']

    res_by_doc = itertools.groupby(res, lambda res: res['doc']['value'])

    triples = []
    for doc_uri, results in res_by_doc:
        results = list(results)
        for i in range(len(results)):
            res = results[i]
            try:
                title = res['stuknummerVR']['value']
            except KeyError:
                title = res['title']['value']
            versioned_title = title + LATIN_ADVERBIAL_NUMERALS[int(
                res['num']['value'])]
            triples.append((
                escape_helpers.sparql_escape_uri(results[i]['ver']['value']),
                'dct:title',
                escape_helpers.sparql_escape_string(versioned_title),
            ))
            if i > 0:
                triples.append((
                    escape_helpers.sparql_escape_uri(
                        results[i]['ver']['value']),
                    'pav:previousVersion',
                    escape_helpers.sparql_escape_uri(
                        results[i - 1]['ver']['value']),
                ))
    if triples:
        query = construct_insert_triples(triples, graph)
        res = helpers.update(query)

    query = construct_migrate_docs(documents, graph)
    res = helpers.update(query)

    return documents
Exemplo n.º 9
0
def construct_insert_file_query(file, physical_file, graph=APPLICATION_GRAPH):
    """
    Construct a SPARQL query for inserting a file.
    :param file: dict containing properties for file
    :param share_uri: 
    :returns: string containing SPARQL query
    """
    query_template = Template("""
PREFIX mu: <http://mu.semte.ch/vocabularies/core/>
PREFIX nfo: <http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#>
PREFIX nie: <http://www.semanticdesktop.org/ontologies/2007/01/19/nie#>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX dbpedia: <http://dbpedia.org/ontology/>

INSERT DATA {
    GRAPH $graph {
        $uri a nfo:FileDataObject ;
            mu:uuid $uuid ;
            nfo:fileName $name ;
            dct:format $mimetype ;
            dct:created $created ;
            nfo:fileSize $size ;
            dbpedia:fileExtension $extension .
        $physical_uri a nfo:FileDataObject ;
            mu:uuid $physical_uuid ;
            nfo:fileName $physical_name ;
            dct:format $mimetype ;
            dct:created $created ;
            nfo:fileSize $size ;
            dbpedia:fileExtension $extension ;
            nie:dataSource $uri .
    }
}
""")
    return query_template.substitute(
        graph=sparql_escape_uri(graph),
        uri=sparql_escape_uri(file["uri"]),
        uuid=sparql_escape_string(file["uuid"]),
        name=sparql_escape_string(file["name"]),
        mimetype=sparql_escape_string(file["mimetype"]),
        created=sparql_escape_datetime(file["created"]),
        size=sparql_escape_int(file["size"]),
        extension=sparql_escape_string(file["extension"]),
        physical_uri=sparql_escape_uri(physical_file["uri"]),
        physical_uuid=sparql_escape_string(physical_file["uuid"]),
        physical_name=sparql_escape_string(physical_file["name"]))
def prepare_signing_flow(signinghub_session: SigningHubSession,
                         signflow_uri: str, piece_uris: typing.List[str]):
    if len(piece_uris) == 0:
        raise exceptions.InvalidArgumentException(
            f"No piece to add specified.")
    if len(piece_uris) > 1:
        raise exceptions.InvalidArgumentException(
            f"Signflow can only add 1 piece.")
    piece_uri = piece_uris[0]

    pieces = signing_flow.get_pieces(signflow_uri)
    piece = query_result_helpers.ensure_1(pieces)
    if piece["uri"] != piece_uri:
        raise exceptions.InvalidStateException(
            f"Piece {piece_uri} is not associated to signflow {signflow_uri}.")

    get_file_query_string = _query_file_template.substitute(
        graph=sparql_escape_uri(APPLICATION_GRAPH),
        piece=sparql_escape_uri(piece_uri),
    )
    file_result = query(get_file_query_string)
    file_records = query_result_helpers.to_recs(file_result)
    file_record = query_result_helpers.ensure_1(file_records)
    piece_uri = file_record["piece"]
    file_name = file_record["piece_name"] + "." + file_record["file_extension"]
    file_path = file_record["file_path"]

    file_path = file_path.replace("share://", "/share/")
    with open(file_path, "rb") as f:
        file_content = f.read()

    preparation_activity_id = generate_uuid()
    preparation_activity_uri = uri.resource.preparation_activity(
        preparation_activity_id)

    signinghub_package = signinghub_session.add_package({
        # package_name: "New Package", # Defaults to "Undefined"
        "workflow_mode":
        "ONLY_OTHERS"  # OVRB staff who prepare the flows will never sign
    })
    signinghub_package_id = signinghub_package["package_id"]

    signinghub_document = signinghub_session.upload_document(
        signinghub_package_id,
        file_content,
        file_name,
        SH_SOURCE,
        convert_document=False)
    signinghub_document_id = signinghub_document["documentid"]

    sh_document_muid = generate_uuid()
    signinghub_document_uri = uri.resource.signinghub_document(
        signinghub_package_id, signinghub_document_id)
    query_string = _update_template.substitute(
        graph=sparql_escape_uri(APPLICATION_GRAPH),
        signflow=sparql_escape_uri(signflow_uri),
        preparation_activity=sparql_escape_uri(preparation_activity_uri),
        preparation_activity_id=sparql_escape_string(preparation_activity_id),
        piece=sparql_escape_uri(piece_uri),
        sh_document=sparql_escape_uri(signinghub_document_uri),
        sh_document_muid=sparql_escape_string(sh_document_muid),
        sh_document_id=sparql_escape_string(str(signinghub_document_id)),
        sh_package_id=sparql_escape_string(str(signinghub_package_id)),
    )
    update(query_string)
Exemplo n.º 11
0
def construct_by_mu_uuid(uuid: str):
    return __signflow_by_uuid_query_template.substitute(
      graph=sparql_escape_uri(APPLICATION_GRAPH),
      uuid=sparql_escape_string(uuid),
    )
Exemplo n.º 12
0
def construct_by_signinghub_id(sh_package_id: str):
    return __signflow_query_template.substitute(
      graph=sparql_escape_uri(APPLICATION_GRAPH),
      signflow='',
      sh_package_id=sparql_escape_string(sh_package_id),
    )