Skip to content

Commit

Permalink
#10 Min fix; #6 multiple classes enabled
Browse files Browse the repository at this point in the history
#10 : Knowledge Extraction min fix
#6 : Template creation form has been updated. It is now possible to specify multiple classes for the same template. Consequently, data retrieval functions have been adapted to this new feature.
  • Loading branch information
Sebastiano-G committed Jun 11, 2024
1 parent f5b0871 commit f720c82
Show file tree
Hide file tree
Showing 13 changed files with 614 additions and 300 deletions.
17 changes: 11 additions & 6 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import logging
import cgi
from importlib import reload
import urllib.parse
from urllib.parse import parse_qs
import requests
import web
Expand Down Expand Up @@ -254,7 +255,7 @@ def GET(self, res_name):
tpl_list = json.load(tpl_file)

print(res_name)
res_type = [i['type'] for i in tpl_list if i["short_name"] == res_name][0]
res_type = "; ".join([i['type'] for i in tpl_list if i["short_name"] == res_name][0])
res_full_name = [i['name'] for i in tpl_list if i["short_name"] == res_name][0]

# if does not exist create the template json file
Expand Down Expand Up @@ -411,6 +412,7 @@ def POST(self, page):
web.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')

actions = web.input()
print(actions)
session['ip_address'] = str(web.ctx['ip'])
is_git_auth = github_sync.is_git_auth()

Expand Down Expand Up @@ -519,7 +521,8 @@ def POST(self, page):
elif actions.action.startswith('createTemplate'):
print('create template')
is_git_auth = github_sync.is_git_auth()
res_type = actions.class_uri.strip() if "class_uri" in actions else conf.main_entity
res_type = sorted([ urllib.parse.unquote(actions[class_input].strip()) for class_input in actions if class_input.startswith("uri_class")])
res_type = conf.main_entity if res_type == [] else res_type
res_name = actions.class_name.replace(' ','_').lower() if "class_name" in actions else "not provided"

with open(TEMPLATE_LIST,'r') as tpl_file:
Expand Down Expand Up @@ -963,8 +966,8 @@ def GET(self):
filters_by_template[template["name"]] = filtersBrowse
return render.records(user=session['username'], data=records_by_template,
title='Latest resources', r_base=conf.base,
alll=count_by_template, filters=filters_by_template,is_git_auth=is_git_auth,
project=conf.myProject)
alll=count_by_template, filters=filters_by_template,
is_git_auth=is_git_auth,project=conf.myProject)

def POST(self):
""" EXPLORE page """
Expand All @@ -990,11 +993,13 @@ def GET(self, name):
record = base+name
res_class = queries.getClass(conf.base+name)
data, stage, title, properties, data_labels = None, None, None, None, {}
extractor = queries.retrieve_extractions(conf.base+name) if u.has_extractor(name, modify=True) else False

try:
res_template = u.get_template_from_class(res_class)
data = dict(queries.getData(record+'/',res_template))
stage = data['stage'][0] if 'stage' in data else 'draft'
previous_extractors = u.has_extractor(res_template, name)
extractions_data = queries.retrieve_extractions(previous_extractors)

with open(res_template) as tpl_form:
fields = json.load(tpl_form)
Expand All @@ -1015,7 +1020,7 @@ def GET(self, name):

return render.view(user=session['username'], graphdata=data_labels,
graphID=name, title=title, stage=stage, base=base,properties=properties,
is_git_auth=is_git_auth,project=conf.myProject,knowledge_extractor=extractor)
is_git_auth=is_git_auth,project=conf.myProject,knowledge_extractor=extractions_data)

def POST(self,name):
""" Record web page
Expand Down
51 changes: 31 additions & 20 deletions forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def get_form(json_form, from_dict=False, subtemplate=False):
tpl_list = json.load(tpl_file)

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

for field in fields:
if 'hidden' in field and field['hidden'] == 'False': # do not include hidden fields
Expand All @@ -61,7 +61,6 @@ def get_form(json_form, from_dict=False, subtemplate=False):
pre_a = '<span class="tip" data-toggle="tooltip" data-placement="bottom" title="'
pre_b = '"><i class="fas fa-info-circle"></i></span>'
prepend = pre_a+html.escape(field['prepend'])+pre_b if 'prepend' in field and len(field['prepend']) > 0 else ''
disabled = 'disabled' if 'disabled' in field and field['disabled'] == "True" else ''
classes = field['class'] if 'class' in field and len(field['class']) > 0 else ''
if 'skos' in field:
for vocabulary in field['skos']:
Expand All @@ -77,7 +76,7 @@ def get_form(json_form, from_dict=False, subtemplate=False):
classes = classes+' vocabularyField' if field['type'] == 'Skos' else classes
classes = classes+' oneVocableAccepted' if 'vocables' in field and field['vocables'] == 'oneVocable' else classes
classes = classes+' websitePreview' if field['type'] == 'WebsitePreview' else classes
classes = classes+' ('+res_class+') '+disabled
classes = classes+' disabled' if 'disabled' in field and field['disabled'] == "True" else classes
classes = classes+ ' ' + field['cardinality'] if 'cardinality' in field else classes
autocomplete = field['cache_autocomplete'] if 'cache_autocomplete' in field and len(field['cache_autocomplete']) > 0 else ''
mandatory = field['mandatory'] if 'mandatory' in field and field['mandatory'] == 'True' else 'False'
Expand All @@ -100,8 +99,9 @@ def get_form(json_form, from_dict=False, subtemplate=False):
pre = prepend,
class_= classes,
value=default,
mandatory = mandatory,
lang=conf.mainLang) , )
lang=conf.mainLang,
data_mandatory = mandatory,
data_class=res_class) , )
else:
params = params + (form.Textbox(myid,
type='text',
Expand All @@ -111,8 +111,9 @@ def get_form(json_form, from_dict=False, subtemplate=False):
pre = prepend,
class_= classes,
value=default,
mandatory = mandatory,
lang=conf.mainLang), )
lang=conf.mainLang,
data_mandatory = mandatory,
data_class=res_class), )

# Entities, SKOS thesauri, links
if field['type'] in ['Skos', 'WebsitePreview'] or (field['type'] == 'Textbox' and field['value'] in ['URL', 'URI', 'Place']):
Expand All @@ -123,7 +124,8 @@ def get_form(json_form, from_dict=False, subtemplate=False):
pre = prepend,
class_= classes,
value=default,
mandatory = mandatory), )
data_mandatory = mandatory,
data_class=res_class), )

# Multimedia Link
if field['type'] == 'Multimedia':
Expand All @@ -134,7 +136,8 @@ def get_form(json_form, from_dict=False, subtemplate=False):
pre = prepend,
class_= classes,
value=default,
mandatory = mandatory) , )
data_mandatory = mandatory,
data_class=res_class) , )

# Text box
if field['type'] == 'Textarea':
Expand All @@ -145,8 +148,9 @@ def get_form(json_form, from_dict=False, subtemplate=False):
pre = prepend,
class_= classes,
value=default,
mandatory = mandatory,
lang=conf.mainLang), )
lang=conf.mainLang,
data_mandatory = mandatory,
data_class=res_class), )

if field['type'] == 'Date':
if field['calendar'] == 'Month':
Expand All @@ -155,22 +159,25 @@ def get_form(json_form, from_dict=False, subtemplate=False):
id=myid,
pre = prepend,
class_= classes,
mandatory=mandatory), )
data_mandatory = mandatory,
data_class=res_class), )
elif field['calendar'] == 'Day':
params = params + (form.Date(myid,
description = description,
id=myid,
pre = prepend,
class_= classes,
mandatory=mandatory), )
data_mandatory = mandatory,
data_class=res_class), )
elif field['calendar'] == 'Year':
params = params + (form.Textbox(myid,
description = description,
id=myid,
pre = prepend,
class_= classes,
value=default,
mandatory=mandatory), )
data_mandatory = mandatory,
data_class=res_class), )

if field['type'] == 'Dropdown':
params = params + (form.Dropdown(myid,
Expand All @@ -180,7 +187,8 @@ def get_form(json_form, from_dict=False, subtemplate=False):
id=myid,
pre = prepend,
class_= classes,
mandatory = mandatory), )
data_mandatory = mandatory,
data_class=res_class), )

if field['type'] == 'Checkbox':
prepend_title = '<section class="checkbox_group_label label col-12">'+description+'</section>'
Expand All @@ -192,7 +200,8 @@ def get_form(json_form, from_dict=False, subtemplate=False):
pre = prepend_title+prepend,
class_= classes+' checkbox_group',
checked=False,
mandatory = mandatory), )
data_mandatory = mandatory,
data_class=res_class), )

for value in dropdown_values[1:]:
i += 1
Expand All @@ -203,7 +212,8 @@ def get_form(json_form, from_dict=False, subtemplate=False):
pre = '',
class_= classes+' checkbox_group following_checkbox',
checked=False,
mandatory = mandatory), )
data_mandatory = mandatory,
data_class=res_class), )

# Subtemplate
if field['type'] == 'Subtemplate':
Expand All @@ -215,9 +225,10 @@ def get_form(json_form, from_dict=False, subtemplate=False):
pre = prepend,
class_= classes,
value=default,
mandatory = mandatory,
subtemplate = resource_class,
subtemplateID = field['import_subtemplate']), ) + get_form(field['import_subtemplate'], subtemplate=True)
data_mandatory = mandatory,
data_class=res_class,
data_subtemplate = resource_class,
data_subtemplateID = field['import_subtemplate']), ) + get_form(field['import_subtemplate'], subtemplate=True)

if subtemplate:
return params
Expand Down
22 changes: 11 additions & 11 deletions mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ def inputToRDF(recordData, userID, stage, graphToClear=None,tpl_form=None):
wd.add(( URIRef(base+graph_name+'/'), URIRef('http://dbpedia.org/ontology/currentStatus'), Literal(stage, datatype="http://www.w3.org/2001/XMLSchema#string") ))

# GET VALUES FROM FIELDS, MAP THEIR TYPE, TRANSFORM TO RDF
wd.add(( URIRef(base+graph_name), RDF.type, URIRef(resource_class) )) # type of catalogued resource
for res_class in resource_class:
wd.add(( URIRef(base+graph_name), RDF.type, URIRef(res_class) )) # type of catalogued resource

# if the user did not specify any disambiguate field
is_any_disambiguate = ["yes" for field in fields if field['disambiguate'] == 'True']
Expand Down Expand Up @@ -218,27 +219,26 @@ def inputToRDF(recordData, userID, stage, graphToClear=None,tpl_form=None):
extractions_array = extractions_dict[recordID] if recordID in extractions_dict else []

for extraction in extractions_array:
extraction_num = next(iter(extraction.keys()))
extraction_type = extraction[extraction_num]['type']
extraction_url = extraction[extraction_num]['url']
extraction_num = str(extraction['internalId'])
extraction_type = extraction['metadata']['type']
extraction_url = extraction['metadata']['url']
extraction_access_keys = False
if extraction_type == 'api':
if 'query' in extraction[extraction_num]:
if 'query' in extraction['metadata']:
encoded_query = ''
add_symbol = '?'
for parameter_key,parameter_value in extraction[extraction_num]['query'].items():
for parameter_key,parameter_value in extraction['metadata']['query'].items():
encoded_query += add_symbol + parameter_key + '=' + parameter_value
add_symbol = '&'
extraction_url+=encoded_query
if 'results' in extraction[extraction_num]:
extraction_access_keys = extraction[extraction_num]['results']
if 'results' in extraction['metadata']:
extraction_access_keys = extraction['metadata']['results']
elif extraction_type == 'sparql':
query = extraction[extraction_num]['query'].replace("'","\"")
print(query)
query = extraction['metadata']['query'].replace("'","\"")
encoded_query = urllib.parse.quote(query)
extraction_url+="?query="+encoded_query
elif extraction_type == 'file':
query = extraction[extraction_num]['query']
query = extraction['metadata']['query']
query = query.replace("{", "{{ SERVICE <x-sparql-anything:"+extraction_url+"> {{").replace("}", "}}") if "<x-sparql-anything:" not in query else query
query = query.replace("'","\"")
encoded_query = urllib.parse.quote(query)
Expand Down
Loading

0 comments on commit f720c82

Please sign in to comment.