From 4785e2f5f262433eaa7420895892caa8129e4ec5 Mon Sep 17 00:00:00 2001 From: Sebastiano Giacomini <92300303+Sebastiano-G@users.noreply.github.com> Date: Mon, 19 Feb 2024 11:20:35 +0100 Subject: [PATCH] Fixed #11 ; fixed #10 : multiple subtemplates #11 : removed hidden triples from published records' visualization (app.py); fixed the script for retrieving previously added hidden triples (queries.py, mapping.py); introduced a control algorithm to avoid templates' issues, e.g.: fields can't be both hidden and mandatory (main.js, template.html). #10 : improved graphical visualisation (main.css, main.js); fixed the function for handling multiple inner sub-records --- app.py | 2 +- mapping.py | 31 ++++++++++------- queries.py | 2 +- static/css/main.css | 8 +++++ static/js/main.js | 74 +++++++++++++++++++++++++++++++++++------ templates/template.html | 10 +++--- 6 files changed, 98 insertions(+), 29 deletions(-) diff --git a/app.py b/app.py index 0c6f61b..3a168ec 100644 --- a/app.py +++ b/app.py @@ -994,7 +994,7 @@ def GET(self, name): title = "No title" properties = {field["label"]:[field["property"], field["type"]] for field in fields if 'property' in field} data_labels = { field['label']:v for k,v in data.items() \ - for field in fields if k == field['id']} + for field in fields if k == field['id'] and field['hidden'] == 'False' } except Exception as e: pass diff --git a/mapping.py b/mapping.py index 4c7ae9e..4396c07 100644 --- a/mapping.py +++ b/mapping.py @@ -104,14 +104,17 @@ def inputToRDF(recordData, userID, stage, knowledge_extraction, graphToClear=Non if to_be_saved: for binding in to_be_saved['results']['bindings']: subject = URIRef(binding['subject']['value']) - for predicate, obj in binding.items(): - if predicate != 'subject': - if obj['type'] == 'uri': - object_ = URIRef(obj['value']) - else: - object_ = Literal(obj['value']) - wd.add((subject, URIRef(predicate), object_)) - + for predicate_obj, inner_dict in binding.items(): + if predicate_obj.endswith('_property'): + predicate = URIRef(inner_dict['value']) + obj_value = binding[predicate_obj.replace("_property", "")]['value'] + obj_type = binding[predicate_obj.replace("_property", "")]['type'] + obj = URIRef(obj_value) if obj_type == "uri" else Literal(obj_value, datatype="http://www.w3.org/2001/XMLSchema#string") + label = binding[predicate_obj.replace("_property", "") + "_label"]['value'] if predicate_obj.replace("_property", "") + "_label" in binding else None + wd.add((subject, URIRef(predicate), obj)) + print(subject, predicate, obj, label) + if label: + wd.add((obj, RDFS.label, Literal(label, datatype="http://www.w3.org/2001/XMLSchema#string"))) queries.clearGraph(graphToClear) wd.add(( URIRef(base+graph_name+'/'), PROV.generatedAtTime, Literal(datetime.datetime.now(),datatype=XSD.dateTime) )) wd.add(( URIRef(base+graph_name+'/'), URIRef('http://dbpedia.org/ontology/currentStatus'), Literal(stage, datatype="http://www.w3.org/2001/XMLSchema#string") )) @@ -196,6 +199,7 @@ def inputToRDF(recordData, userID, stage, knowledge_extraction, graphToClear=Non # SUBTEMPLATE elif field['type']=="Subtemplate": subrecords = process_subrecords(recordData, field['id']) if not subrecords_dict else subrecords_dict + print("SUBRECORDS!!!!!!!!!!!!!!!\n", subrecords) if field['id'] in subrecords: for subrecord_idx, subrecord in subrecords[field['id']].items(): ID = str(int(time.time() * 1000)) @@ -244,9 +248,12 @@ def process_subrecords(data, id): for inner_subrecord in inner_subrecords: inner_subrecord_split = inner_subrecord.split('__') inner_prefix, inner_num = inner_subrecord_split[0], inner_subrecord_split[-1] - add_results[inner_prefix] = { - inner_num: process_subrecords(data, inner_subrecord) - } + if inner_prefix in add_results: + add_results[inner_prefix][inner_num] = process_subrecords(data, inner_subrecord) + else: + add_results[inner_prefix] = { + inner_num: process_subrecords(data, inner_subrecord) + } else: imported_values = [import_key for import_key in data.keys() if import_key.startswith(key + "-")] for imported_value in imported_values: @@ -258,7 +265,7 @@ def process_subrecords(data, id): results[prefix] = { num: add_results } elif data[id] != "": for el in data[id].split(','): - results[el.split('-')[0]] = data[el] + results[el.split('__')[0]] = data[el] return results def find_label(tpl, subrecord, alternative_label): diff --git a/queries.py b/queries.py index 45435c4..e40590e 100644 --- a/queries.py +++ b/queries.py @@ -359,7 +359,7 @@ def saveHiddenTriples(graph, tpl): results = [] hidden_fields = [field for field in fields if field['hidden'] == 'True'] - patterns = [ 'OPTIONAL {?subject <'+hidden_field['property']+'> ?'+hidden_field['id']+'.}. ' if hidden_field['value'] in ['Literal','Date','gYearMonth','gYear','URL'] else 'OPTIONAL {?subject <'+hidden_field['property']+'> ?'+hidden_field['id']+'. ?'+hidden_field['id']+' rdfs:label ?'+hidden_field['id']+'_label .} .' for hidden_field in hidden_fields if 'value' in hidden_field and hidden_field['hidden'] == 'True'] + patterns = [ 'OPTIONAL {?subject <'+hidden_field['property']+'> ?'+hidden_field['id']+'. ?subject ?'+hidden_field['id']+'_property ?'+hidden_field['id']+'}. ' if hidden_field['value'] in ['Literal','Date','gYearMonth','gYear','URL'] else 'OPTIONAL {?subject <'+hidden_field['property']+'> ?'+hidden_field['id']+'. ?'+hidden_field['id']+' rdfs:label ?'+hidden_field['id']+'_label . ?subject ?'+hidden_field['id']+'_property ?'+hidden_field['id']+'}.' for hidden_field in hidden_fields if 'value' in hidden_field and hidden_field['hidden'] == 'True'] if patterns != []: patterns_string = ''.join(patterns) queryNGraph = ''' diff --git a/static/css/main.css b/static/css/main.css index ce21220..2048731 100644 --- a/static/css/main.css +++ b/static/css/main.css @@ -2028,6 +2028,14 @@ button#showTemplates { width: 42%; } +input[subtemplate]+.fa-plus-circle { + right: 45px; position: absolute; top: 20px; +} + +.form_row .fa-edit, .form_row .fa-trash-alt { + color: rgba(100,23,180,1); cursor: pointer; +} + /* extra */ .fa-plus-circle, .fa-eye, .fa-trash{color: rgba(100,23,180,1); cursor: pointer;} .link_btn, .btn {white-space: nowrap;} diff --git a/static/js/main.js b/static/js/main.js index a809a66..38b473a 100644 --- a/static/js/main.js +++ b/static/js/main.js @@ -48,6 +48,27 @@ $(document).ready(function() { }; }); + // check templates' constraints + $("#updateTemplate").on('click', function(e) { + e.preventDefault(); + + // make sure the primary key is mandatory + var primary_key = $('.disambiguate[checked="checked"'); + primary_key.parent().parent().find('input[id*="mandatory"]').attr("checked", "checked"); + + // prevent mandatory fields to be hidden + var mandatory_fields = $('input[type="checkbox"][id*="mandatory"][checked="checked"]'); + mandatory_fields.each(function() { + var hidden_field_checkbox = $(this).parent().parent().find('input[type="checkbox"][id*="hidden"]'); + if (hidden_field_checkbox.attr('checked') == 'checked') { + Swal.fire({ title:"Hidden fields cannot be mandatory"}); + return false; + }; + }); + Swal.fire({ title: 'Saved!'}); + setTimeout(function() { document.getElementById('templateForm').submit();}, 500); + }); + // disable forms $(".disabled").attr("disabled","disabled"); @@ -354,7 +375,7 @@ $(document).ready(function() { $(this).parent().parent().hide(); }); var field_name = $(this).parent().prev().text(); - $(this).parent().append(""); + $(this).after(""); }); }); @@ -1302,6 +1323,7 @@ function create_subrecord(resource_class, field_name, el) { // SAVE save_subrecord_btn.on('click', function(e) { // generate a tag + console.log("RQWEQ") var tag_label = subrecord_form.find('.disambiguate').val() || (field_name + "-" + $(".tag-subrecord[class~='"+resource_class+"']").length + 1); var subinputs = []; subrecord_form.find('input:not(.btn)').each(function() { @@ -1311,7 +1333,8 @@ function create_subrecord(resource_class, field_name, el) { }); var subrecord_index = $("[subtemplate='"+resource_class+"']").parent().parent().find('.tag-subrecord').length + 1; var subrecord_id = $("[subtemplate='"+resource_class+"']").attr('id') + "__" + subrecord_index; - $(el).after("
" + tag_label + ""); + console.log(el); + el.after("
" + tag_label + ""); $('#recordForm').append(""); // hide_subform @@ -1353,10 +1376,6 @@ function modify_subrecord(sub_id, keep) { // remove all inputs var inner_inputs = $('#'+sub_id).val().split(","); delete_inner_subrecord(inner_inputs); // collect nested inputs and remove them - $('#'+sub_id+'-tag').next('i').remove(); - $('#'+sub_id+'-tag').next('i').remove(); - $('#'+sub_id+'-tag').remove(); - $('#'+sub_id).remove(); } else { // recollect the first level nested inputs to be displayed @@ -1364,20 +1383,53 @@ function modify_subrecord(sub_id, keep) { // recreate subrecord_section var field_name = $('#'+sub_id+'-tag').parent().prev().text(); - var el = $('#'+sub_id+'-tag').prev('.fa-plus-circle'); + var el = $('#'+sub_id+'-tag').prevAll('.fa-plus-circle').first(); + console.log(el); create_subrecord(original_subtemplate_class, field_name, el); for (let i=0; i"+value_string+""; + var hidden_input = $(this).detach(); + input.after(tag, hidden_input); + }) + } else if ($('input[id*="'+inner_inputs[i]+'__"]')) { + var inner_subrecords = $('input[id*="'+inner_inputs[i]+'__"]'); + inner_subrecords.each(function() { + var inner_subrecord_fields = $(this).val().split(','); + var primary_key = ""; + for (let i=0; i" + primary_key + "" + $('#'+inner_inputs[i]).after(tag); + }) } } } + $('#'+sub_id+'-tag').next('i').remove(); + $('#'+sub_id+'-tag').next('i').remove(); + $('#'+sub_id+'-tag').remove(); + $('#'+sub_id).remove(); } function delete_inner_subrecord(inner_inputs) { diff --git a/templates/template.html b/templates/template.html index 18fb9ad..2ffdb4a 100644 --- a/templates/template.html +++ b/templates/template.html @@ -34,7 +34,8 @@

$res_name

$if f: $for field in f: -
+ $ hidden_style = "style=opacity:0.6" if 'hidden' in field and field["hidden"] == "True" else "" +
$ id = field["id"] $if field['type'] != "KnowledgeExtractor": @@ -223,15 +224,15 @@

$res_name

- $ checked_mandatory = "checked=checked" if 'mandatory'in field and field["mandatory"] == "True" else "" + $ checked_mandatory = "checked=checked" if 'mandatory' in field and field["mandatory"] == "True" else ""
- $ checked_hidden = "checked=checked" if 'hidden'in field and field["hidden"] == "True" else "" + $ checked_hidden = "checked=checked" if 'hidden' in field and field["hidden"] == "True" else "" - +
$else: @@ -311,3 +312,4 @@
Knowledge Extraction
+ \ No newline at end of file