Ejemplo n.º 1
0
def add_license_to_db(object_license):
    neo_license = LicenseModel.nodes.get_or_none(
        hashed_sets=object_license.hash())
    if neo_license:
        # update of labels list if needed
        neo_license.labels = list(
            set(object_license.get_labels()).union(neo_license.labels))
        neo_license.save()
    else:
        # license does not exists in db
        license_leaves = get_leaf_licenses()
        neo_license = NeoFactory.NeoLicense(object_license)
        neo_license.save()
        for neo_license_leaf in license_leaves:
            object_license_leaf = ObjectFactory.objectLicense(neo_license_leaf)
            if object_license.is_preceding(object_license_leaf):
                if Constraints.is_compatibility_viable(object_license,
                                                       object_license_leaf):
                    neo_license_leaf.precedings.connect(neo_license)
            else:
                update_licenses_relations_rec(neo_license, object_license,
                                              neo_license_leaf,
                                              object_license_leaf)
    for dataset in object_license.get_datasets():
        neo_dataset = DatasetModel.nodes.get_or_none(hashed_uri=dataset.hash())
        if not neo_dataset:
            neo_dataset = NeoFactory.NeoDataset(dataset)
            neo_dataset.save()
        neo_license.datasets.connect(neo_dataset)
    object_license = ObjectFactory.objectLicense(neo_license)
    return object_license
Ejemplo n.º 2
0
def get_fragment(request, subject, predicate, obj, page, graph):
    fragment = Dataset()
    tpf_url = urlparse(request.build_absolute_uri())
    tpf_url = TPF_URL.format(tpf_url.scheme, tpf_url.netloc, graph)
    licenses = []
    neo_licenses = LicenseModel.nodes.filter(graph__exact=graph)
    if subject and subject.startswith(LICENSE_SUBJECT_PREFIX):
        license_id = subject.split('/')[-1]
        neo_licenses.filter(hashed_sets__exact=license_id)
    for neo_license in neo_licenses:
        license_object = ObjectFactory.objectLicense(neo_license)
        license_object = license_object.to_json()
        license_object['compatible_licenses'] = []
        for compatible_neo_license in neo_license.followings.all():
            compatible_license = ObjectFactory.objectLicense(
                compatible_neo_license)
            license_object['compatible_licenses'].append(
                compatible_license.hash())
        licenses.append(license_object)
    rdf_licenses = get_rdf(licenses, graph).triples((subject, predicate, obj))
    total_nb_triples = 0
    for s, p, o in rdf_licenses:
        fragment.add((s, p, o))
        total_nb_triples += 1
    last_result = True
    nb_triple_per_page = total_nb_triples
    _frament_fill_meta(subject, predicate, obj, page, graph, fragment,
                       last_result, total_nb_triples, nb_triple_per_page,
                       request, tpf_url)
    return fragment
Ejemplo n.º 3
0
def already_follower(object_license, new_neo_license):
    for neo_follower in new_neo_license.followings:
        object_follower = ObjectFactory.objectLicense(neo_follower)
        if object_license != object_follower and object_license.is_following(
                object_follower):
            return True
    return False
Ejemplo n.º 4
0
def add_license_to_db(object_license,
                      method='infimum',
                      license_levels=[],
                      viability_check=True,
                      nb_visit=0,
                      graph='ld'):
    neo_license = LicenseModel.nodes.filter(graph__exact=graph).get_or_none(
        hashed_sets=object_license.hash())
    if neo_license:
        # update of labels list if needed
        neo_license.labels = list(
            set(object_license.get_labels()).union(neo_license.labels))
        neo_license.save()
    else:
        # license does not exists in db
        if method == 'infimum':
            neo_license, nb_visit = update_licenses_relations_infimum(
                object_license, viability_check, nb_visit, graph)
        else:
            neo_license, nb_visit = update_licenses_relations_supremum(
                object_license, viability_check, nb_visit, graph)
        license_levels.append(object_license.get_level())
    for dataset in object_license.get_datasets():
        neo_dataset = DatasetModel.nodes.filter(
            graph__exact=graph).get_or_none(hashed_uri=dataset.hash())
        if not neo_dataset:
            neo_dataset = NeoFactory.NeoDataset(dataset, graph)
            neo_dataset.save()
        neo_license.datasets.connect(neo_dataset)
    object_license = ObjectFactory.objectLicense(neo_license)
    return object_license, nb_visit
Ejemplo n.º 5
0
def already_preceder(object_license, new_neo_license):
    for neo_preceder in new_neo_license.precedings:
        object_preceder = ObjectFactory.objectLicense(neo_preceder)
        if object_license != object_preceder and object_license.is_preceding(
                object_preceder):
            return True
    return False
Ejemplo n.º 6
0
def get_license_search(request, graph):
    query = request.GET.get('query', None)
    label = request.GET.get('label', None)
    permissions = request.GET.get('permissions', None)
    if is_empty(permissions):
        permissions = None
    obligations = request.GET.get('obligations', None)
    if is_empty(obligations):
        obligations = None
    prohibitions = request.GET.get('prohibitions', None)
    if is_empty(prohibitions):
        prohibitions = None
    neo_licenses = LicenseModel.nodes.filter(graph__exact=graph)
    if query:
        neo_licenses = license_filter_labels(query)
    else:
        if label:
            neo_licenses = license_filter_labels(label)
        if permissions:
            neo_licenses = license_filter_sets(permissions, 'permissions')
        if obligations:
            neo_licenses = license_filter_sets(obligations, 'obligations')
        if prohibitions:
            neo_licenses = license_filter_sets(prohibitions, 'prohibitions')
    response_content = []
    for neo_license in neo_licenses:
        license_object = ObjectFactory.objectLicense(neo_license)
        response_content.append(license_object.to_json())
    response = HttpResponse(json.dumps(response_content),
                            content_type='application/json')
    response['Access-Control-Allow-Origin'] = '*'
    return response
Ejemplo n.º 7
0
def update_licenses_relations_rec(new_neo_license, new_object_license,
                                  neo_license, object_license):
    # update precedings and followings of license recursively.
    grand_follower = False
    for neo_license_following in neo_license.followings:
        object_license_following = ObjectFactory.objectLicense(
            neo_license_following)
        if new_object_license.is_following(object_license_following):
            # new license is a follower of a following
            grand_follower = True
        if new_object_license.is_preceding(object_license_following):
            if Constraints.is_compatibility_viable(new_object_license,
                                                   object_license_following):
                neo_license_following.precedings.connect(new_neo_license)
            if new_object_license.is_following(object_license):
                # new_license is between license and its following_license.
                if Constraints.is_compatibility_viable(object_license,
                                                       new_object_license):
                    neo_license.followings.connect(new_neo_license)
                    neo_license.followings.disconnect(neo_license_following)
        else:
            update_licenses_relations_rec(new_neo_license, new_object_license,
                                          neo_license_following,
                                          object_license_following)
    if not grand_follower and new_object_license.is_following(object_license):
        # then its just the next follower of the current license
        if Constraints.is_compatibility_viable(object_license,
                                               new_object_license):
            neo_license.followings.connect(new_neo_license)
Ejemplo n.º 8
0
def rep_search(request):
    graph = 'rep'
    query = request.GET.get('query', '')
    hashed_sets = request.GET.get('license', '')
    sens = request.GET.get('sens', '')
    keywords = query.split()
    results = []
    nodes = []
    links = []
    added_nodes = []
    if sens == 'compatible':
        neo_licenses = get_compliant_licenses(hashed_sets, graph)
    else:
        neo_licenses = get_compatible_licenses(hashed_sets, graph)
    for neo_license in neo_licenses:
        license_object = ObjectFactory.objectLicense(neo_license)
        if keywords:
            license_object = query_filter(license_object, keywords)
        if license_object.datasets:
            results.append(license_object.to_json())
        # we add license and datasets to the graph
        if license_object not in added_nodes:
            nodes.append(D3jsData.license_node(license_object))
            added_nodes.append(license_object)
        for dataset_object in license_object.datasets:
            nodes.append(
                D3jsData.dataset_node(dataset_object,
                                      license_object.get_level()))
            links.append(D3jsData.dataset_link(license_object, dataset_object))
        for compatible_neo_license in neo_license.followings.all():
            compatible_license_object = ObjectFactory.objectLicense(
                compatible_neo_license)
            if compatible_license_object not in added_nodes and compatible_neo_license in neo_licenses:
                nodes.append(D3jsData.license_node(compatible_license_object))
                added_nodes.append(compatible_license_object)
            if compatible_neo_license in neo_licenses:
                links.append(
                    D3jsData.compatible_link(license_object,
                                             compatible_license_object))
    return render(
        request, 'search.html', {
            'results': json.dumps(results),
            'nb_datasets': nb_datasets(results),
            'graph': json.dumps(D3jsData.graph(nodes, links)),
            'classification': graph
        })
Ejemplo n.º 9
0
def get_licenses(request, graph):
    response_content = []
    for neo_license in LicenseModel.nodes.filter(graph__exact=graph):
        license_object = ObjectFactory.objectLicense(neo_license)
        response_content.append(license_object.to_json())
    response = HttpResponse(json.dumps(response_content),
                            content_type='application/json')
    response['Access-Control-Allow-Origin'] = '*'
    return response
Ejemplo n.º 10
0
def get_datasets(request, graph):
    response_content = []
    for neo_dataset in DatasetModel.nodes.filter(graph__exact=graph):
        dataset_object = ObjectFactory.objectDataset(neo_dataset)
        response_content.append(dataset_object.to_json())
    response = HttpResponse(json.dumps(response_content),
                            content_type='application/json')
    response['Access-Control-Allow-Origin'] = '*'
    return response
Ejemplo n.º 11
0
def export_licenses(request, graph, serialization_format):
    licenses = []
    if serialization_format not in ['n3', 'nt', 'xml', 'turtle', 'json-ld']:
        serialization_format = 'turtle'
    for neo_license in LicenseModel.nodes.filter(graph__exact=graph):
        license_object = ObjectFactory.objectLicense(neo_license)
        license_object = license_object.to_json()
        license_object['compatible_licenses'] = []
        for compatible_neo_license in neo_license.followings.all():
            compatible_license = ObjectFactory.objectLicense(
                compatible_neo_license)
            license_object['compatible_licenses'].append(
                compatible_license.hash())
        licenses.append(license_object)
    rdf_licenses = RDFExporter.get_rdf(licenses, graph)
    response = HttpResponse(
        rdf_licenses.serialize(format=serialization_format),
        content_type='text/{}'.format(serialization_format))
    response['Access-Control-Allow-Origin'] = '*'
    return response
Ejemplo n.º 12
0
def get_graph(request):
    nodes = []
    links = []
    for neo_license in LicenseModel.nodes:
        license_object = ObjectFactory.objectLicense(neo_license)
        nodes.append(D3jsData.license_node(license_object))
        for neo_dataset in neo_license.datasets.all():
            dataset_object = ObjectFactory.objectDataset(neo_dataset)
            nodes.append(D3jsData.dataset_node(dataset_object))
            links.append(D3jsData.dataset_link(license_object, dataset_object))
        for compatible_neo_license in neo_license.followings.all():
            compatible_license_object = ObjectFactory.objectLicense(
                compatible_neo_license)
            links.append(
                D3jsData.compatible_link(license_object,
                                         compatible_license_object))
    response = HttpResponse(json.dumps(D3jsData.graph(nodes, links)),
                            content_type='application/json')
    response['Access-Control-Allow-Origin'] = '*'
    return response
Ejemplo n.º 13
0
def get_dataset_by_hash(request, hashed_uri):
    try:
        neo_dataset = DatasetModel.nodes.get(hashed_uri=hashed_uri)
        dataset_object = ObjectFactory.objectDataset(neo_dataset)
        response = HttpResponse(json.dumps(dataset_object.to_json()),
                                content_type='application/json')
    except DoesNotExist:
        response = HttpResponse(
            "{}",
            content_type='application/json',
            status=404,
        )
    response['Access-Control-Allow-Origin'] = '*'
    return response
Ejemplo n.º 14
0
def get_license_by_hash(request, hashed_sets):
    try:
        neo_license = LicenseModel.nodes.get(hashed_sets=hashed_sets)
        license_object = ObjectFactory.objectLicense(neo_license)
        response = HttpResponse(json.dumps(license_object.to_json()),
                                content_type='application/json')
    except DoesNotExist:
        response = HttpResponse(
            "{}",
            content_type='application/json',
            status=404,
        )
    response['Access-Control-Allow-Origin'] = '*'
    return response
Ejemplo n.º 15
0
def get_datasets_of_licenses(request, hashed_sets):
    try:
        neo_license = LicenseModel.nodes.get(hashed_sets=hashed_sets)
        license_datasets = []
        for dataset in neo_license.datasets.all():
            dataset_object = ObjectFactory.objectDataset(dataset)
            license_datasets.append(dataset_object.to_json())
        response = HttpResponse(json.dumps(license_datasets),
                                content_type='application/json')
    except DoesNotExist:
        response = HttpResponse(
            "[]",
            content_type='application/json',
            status=404,
        )
    response['Access-Control-Allow-Origin'] = '*'
    return response
Ejemplo n.º 16
0
def search(request):
    query = request.GET.get('query', '')
    hashed_sets = request.GET.get('license', '')
    sens = request.GET.get('sens', '')
    keywords = query.split()
    results = []
    if sens == 'compatible':
        neo_licenses = get_compliant_licenses(hashed_sets)
    else:
        neo_licenses = get_compatible_licenses(hashed_sets)
    for neo_license in neo_licenses:
        license_object = ObjectFactory.objectLicense(neo_license)
        if keywords:
            license_object = query_filter(license_object, keywords)
        if license_object:
            results.append(license_object.to_json())
    return render(request, 'search.html', {'results': json.dumps(results), 'nb_datasets': nb_datasets(results)})
Ejemplo n.º 17
0
def get_compatible(request, hashed_sets, graph):
    try:
        neo_licenses = get_compliant_licenses(hashed_sets, graph)
        compatible_licenses = []
        for neo_license in neo_licenses:
            license_object = ObjectFactory.objectLicense(neo_license)
            compatible_licenses.append(license_object.to_json())
        response = HttpResponse(json.dumps(compatible_licenses),
                                content_type='application/json')
    except DoesNotExist:
        response = HttpResponse(
            "[]",
            content_type='application/json',
            status=404,
        )
    response['Access-Control-Allow-Origin'] = '*'
    return response
Ejemplo n.º 18
0
def update_licenses_relations_supremum_rec(new_neo_license, new_object_license,
                                           neo_license, object_license,
                                           viability_check, nb_visit,
                                           tested_licenses):
    # update precedings and followings of license recursively.
    if object_license in tested_licenses:
        return nb_visit
    nb_visit += 1
    tested_licenses.append(object_license)
    grand_preceder = False
    for neo_license_preceding in neo_license.precedings:
        object_license_preceding = ObjectFactory.objectLicense(
            neo_license_preceding)
        if already_preceder(object_license_preceding, new_neo_license):
            continue
        if new_object_license.is_following(object_license_preceding) and (
                Constraints.is_compatibility_viable(object_license_preceding,
                                                    new_object_license)
                or not viability_check):
            update_transitivity_preceder(new_neo_license,
                                         object_license_preceding)
            new_neo_license.precedings.connect(neo_license_preceding)
            if new_object_license.is_preceding(object_license) and (
                    Constraints.is_compatibility_viable(
                        new_object_license, object_license)
                    or not viability_check):
                new_neo_license.followings.connect(neo_license)
                neo_license.precedings.disconnect(neo_license_preceding)
        else:
            if new_object_license.is_preceding(object_license_preceding) and (
                    Constraints.is_compatibility_viable(
                        new_object_license, object_license_preceding)
                    or not viability_check):
                grand_preceder = True
            nb_visit = update_licenses_relations_supremum_rec(
                new_neo_license, new_object_license, neo_license_preceding,
                object_license_preceding, viability_check, nb_visit,
                tested_licenses)
    if not grand_preceder and (
            new_object_license.is_preceding(object_license) and
        (Constraints.is_compatibility_viable(
            new_object_license, object_license) or not viability_check)):
        new_neo_license.followings.connect(neo_license)
    return nb_visit
Ejemplo n.º 19
0
def add_dataset(request, graph):
    json_dataset = json.loads(request.body)
    object_dataset = Dataset()
    object_dataset.from_json(json_dataset)
    neo_dataset = NeoFactory.NeoDataset(object_dataset, graph)
    object_dataset = ObjectFactory.objectDataset(neo_dataset)
    try:
        neo_dataset.save()
        response = HttpResponse(
            json.dumps(object_dataset.to_json()),
            content_type='application/json',
            status=201,
        )
    except UniqueProperty:
        response = HttpResponse(
            json.dumps(object_dataset.to_json()),
            content_type='application/json',
            status=409,
        )
    response['Access-Control-Allow-Origin'] = '*'
    return response
Ejemplo n.º 20
0
def update_licenses_relations_infimum(object_license,
                                      viability_check,
                                      nb_visit,
                                      graph='ld'):
    tested_licenses = [object_license]
    license_leaves = get_leaf_licenses(graph)
    neo_license = NeoFactory.NeoLicense(object_license, graph)
    neo_license.save()
    for neo_license_leaf in license_leaves:
        object_license_leaf = ObjectFactory.objectLicense(neo_license_leaf)
        if object_license.is_preceding(object_license_leaf) and (
                Constraints.is_compatibility_viable(object_license,
                                                    object_license_leaf)
                or not viability_check):
            update_transitivity_follower(neo_license, object_license_leaf)
            neo_license_leaf.precedings.connect(neo_license)
        else:
            nb_visit = update_licenses_relations_infimum_rec(
                neo_license, object_license, neo_license_leaf,
                object_license_leaf, viability_check, nb_visit,
                tested_licenses)
    return neo_license, nb_visit
Ejemplo n.º 21
0
def update_licenses_relations_supremum(object_license,
                                       viability_check,
                                       nb_visit,
                                       graph='ld'):
    tested_licenses = [object_license]
    license_roots = get_root_licenses(graph)
    neo_license = NeoFactory.NeoLicense(object_license, graph)
    neo_license.save()
    for neo_license_root in license_roots:
        object_license_root = ObjectFactory.objectLicense(neo_license_root)
        if object_license.is_following(object_license_root) and (
                Constraints.is_compatibility_viable(object_license_root,
                                                    object_license)
                or not viability_check):
            update_transitivity_preceder(neo_license, object_license_root)
            neo_license_root.followings.connect(neo_license)
        else:
            nb_visit = update_licenses_relations_supremum_rec(
                neo_license, object_license, neo_license_root,
                object_license_root, viability_check, nb_visit,
                tested_licenses)
    return neo_license, nb_visit
Ejemplo n.º 22
0
def get_dataset_search(request, graph):
    query = request.GET.get('query', None)
    label = request.GET.get('label', None)
    descr = request.GET.get('descr', None)
    uri = request.GET.get('uri', None)
    neo_datasets = DatasetModel.nodes.filter(graph__exact=graph)
    if query:
        neo_datasets = dataset_filter_search(query, graph)
    else:
        if label:
            neo_datasets = neo_datasets.filter(label__icontains=label)
        if uri:
            neo_datasets = neo_datasets.filter(uri__icontains=uri)
        if descr:
            neo_datasets = neo_datasets.filter(description__icontains=descr)
    response_content = []
    for neo_dataset in neo_datasets:
        dataset_object = ObjectFactory.objectDataset(neo_dataset)
        response_content.append(dataset_object.to_json())
    response = HttpResponse(json.dumps(response_content),
                            content_type='application/json')
    response['Access-Control-Allow-Origin'] = '*'
    return response
Ejemplo n.º 23
0
def update_transitivity_preceder(new_neo_license, new_object_preceder):
    for neo_preceder in new_neo_license.precedings:
        object_preceder = ObjectFactory.objectLicense(neo_preceder)
        if object_preceder.is_preceding(new_object_preceder):
            new_neo_license.precedings.disconnect(neo_preceder)
Ejemplo n.º 24
0
def update_transitivity_follower(new_neo_license, new_object_follower):
    for neo_follower in new_neo_license.followings:
        object_follower = ObjectFactory.objectLicense(neo_follower)
        if object_follower.is_following(new_object_follower):
            new_neo_license.followings.disconnect(neo_follower)