Skip to content

Commit

Permalink
#10 Min fix: same subtemplate for multiple fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastiano-G committed Jul 26, 2024
1 parent f5b19d4 commit 953a335
Show file tree
Hide file tree
Showing 4 changed files with 252 additions and 233 deletions.
24 changes: 21 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ def POST(self, name):
# load the template selected by the user
if 'res_name' in recordData:
if recordData.res_name != 'None':
f = forms.get_form(recordData.res_name)
f = forms.get_form(recordData.res_name,processed_templates=[])
query_templates = u.get_query_templates(recordData.res_name)
extractor = u.has_extractor(recordData.res_name)
return render.record(record_form=f, pageID=name, user=user, alert=block_user,
Expand Down Expand Up @@ -1108,10 +1108,11 @@ def GET(self, name):
results_by_class[res_type] = {'class':res_class, 'results':[res_uri]}

count = len(appears_in)
map_coordinates = (queries.geonames_geocoding(uri)) if uri.startswith("https://sws.geonames.org/") else None

return render.term(user=session['username'], data=data, count=count,
is_git_auth=is_git_auth,project=conf.myProject,base=conf.base,
uri=uri,name=name,results=results_by_class)
uri=uri,name=name,results=results_by_class,map=map_coordinates)

def POST(self,name):
""" controlled vocabulary term web page
Expand Down Expand Up @@ -1392,7 +1393,24 @@ def GET(self):
count_result = [result["count"]["value"] for result in query_results["results"]["bindings"]]
count = int(count_result[0]) if len(count_result) > 0 else 0
counter["count"] = count
else:
elif chart["type"] == "map":
chart_id = str(time.time()).replace('.','-')
query_results = queries.hello_blazegraph(chart["query"])["results"]["bindings"]
stats_result = []
for result in query_results:
geonames = result["geonames"]["value"]
lat, long = queries.geonames_geocoding(geonames)
i = 0
while i < int(result["count"]["value"]):
stats_result.append({
"title" : result["title"]["value"],
"latitude": lat,
"longitude": long
})
i += 1
chart["stats"] = json.dumps(stats_result)
chart["info"] = chart_id
elif chart["type"] == "chart":
chart_id = str(time.time()).replace('.','-')
stats_query = chart["query"]
x_var, x_name = chart["x-axis"].split(",",1)
Expand Down
10 changes: 6 additions & 4 deletions forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def parse_config_variables(text:str, conf):
text = text.replace(k, v)
return text

def get_form(json_form, from_dict=False, supertemplate=False):
def get_form(json_form, from_dict=False, supertemplate=False, processed_templates=[]):
""" read config in 'template-(.*).json' and return a webpy form """
import io
if from_dict == False:
Expand All @@ -52,6 +52,7 @@ def get_form(json_form, from_dict=False, supertemplate=False):

res_class = [t["type"] for t in tpl_list if t["template"] == json_form]
res_class = "; ".join(res_class[0]) if len(res_class) > 0 else "none"
processed_templates.append(json_form)

for field in fields:
if 'hidden' in field and field['hidden'] == 'False': # do not include hidden fields
Expand Down Expand Up @@ -89,7 +90,7 @@ def get_form(json_form, from_dict=False, supertemplate=False):
# dropdown
dropdown_values = [(k,v) for k,v in field['values'].items()] if 'values' in field else None
# subtemplate
data_supertemplate = supertemplate if supertemplate else 'None'
data_supertemplate = 'True' if supertemplate else 'None'

# Text box
if field['type'] == 'Textbox' and field['value'] == 'Literal':
Expand Down Expand Up @@ -247,9 +248,10 @@ def get_form(json_form, from_dict=False, supertemplate=False):
for imported_template in field['import_subtemplate']:
template_dict = next(t for t in tpl_list if t["template"] == imported_template)
resource_class = template_dict['type']
resource_class_string = "; ".join(resource_class)
resource_name = template_dict['name']
dropdown_templates.append((resource_class,resource_name))
params = params + get_form(imported_template, supertemplate=myid)
dropdown_templates.append((resource_class_string,resource_name))
params = params + get_form(imported_template, supertemplate=True, processed_templates=processed_templates) if imported_template not in processed_templates else params
params = params + (form.Dropdown(myid,
description = description,
args=dropdown_templates,
Expand Down
22 changes: 11 additions & 11 deletions static/js/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,9 @@ function searchCatalogueAdvanced(searchterm) {
}

// search catalogue's records belonging to a desired class
function searchCatalogueByClass(searchterm) {

function searchCatalogueByClass(searchterm,fieldId,singleValue) {
// get the required class
console.log(fieldId)
var resource_class = $('#'+searchterm).attr('data-class');
var resource_classes = resource_class.split(';').map(cls => cls.trim()).filter(cls => cls !== "");
var class_triples = resource_classes.map(cls => `?s a <${cls}> .`).join(" ");
Expand All @@ -309,7 +309,6 @@ function searchCatalogueByClass(searchterm) {
// they must belong to the same required class
var yet_to_save_keys = [];
var yet_to_save_resources = [];
console.log('.disambiguate[data-class="' + resource_class + '"]:not([data-subform="'+dataSubform+'"])')
$('.disambiguate[data-class="' + resource_class + '"]:not([data-subform="'+dataSubform+'"]').each(function() {
yet_to_save_keys.push($(this).val());
var key_id = $(this).attr('id');
Expand Down Expand Up @@ -385,12 +384,13 @@ function searchCatalogueByClass(searchterm) {
<i class='far fa-trash-alt'></i>\
</button>\
</section>");

if ($('[name="'+subrecordBase+'-subrecords"]').length) {
$('[name="'+subrecordBase+'-subrecords"]').val($('[name="'+subrecordBase+'-subrecords"]').val()+","+oldID+";"+oldLabel);
if ($('[name="'+fieldId+'-subrecords"]').length && !singleValue) {
$('[name="'+fieldId+'-subrecords"]').val($('[name="'+fieldId+'-subrecords"]').val()+","+oldID+";"+oldLabel);
} else {
const new_sub = $("<input type='hidden' name='"+subrecordBase+"-subrecords' value='"+oldID+";"+oldLabel+"'>")
$('#recordForm, #modifyForm').append(new_sub)
$('[name="'+fieldId+'-subrecords"]').remove();
const new_sub = $("<input type='hidden' name='"+fieldId+"-subrecords' value='"+oldID+";"+oldLabel+"'>")
$('#recordForm, #modifyForm').append(new_sub)
}
});

Expand Down Expand Up @@ -421,10 +421,10 @@ function searchCatalogueByClass(searchterm) {
</button>\
</section>");

if ($('[name="'+subrecordBase+'-subrecords"]').length) {
$('[name="'+subrecordBase+'-subrecords"]').val($('[name="'+subrecordBase+'-subrecords"]').val()+","+target+";"+label);
if ($('[name="'+fieldId+'-subrecords"]').length) {
$('[name="'+fieldId+'-subrecords"]').val($('[name="'+fieldId+'-subrecords"]').val()+","+target+";"+label);
} else {
const new_sub = $("<input type='hidden' name='"+subrecordBase+"-subrecords' value='"+target+";"+label+"'>")
const new_sub = $("<input type='hidden' name='"+fieldId+"-subrecords' value='"+target+";"+label+"'>")
$('#recordForm, #modifyForm').append(new_sub)
}
});
Expand Down
Loading

0 comments on commit 953a335

Please sign in to comment.