def save(self, rdf_obj, validation_status=True): """Method validates and saves passed data for the class Args: rdf_obj -- Current RDF Form class fields validationS valid_required_props = self._validate_required_properties( rdf_obj, old_form_data) validDependancies = self._validate_dependant_props( rdf_obj, old_form_data)""" if not DEBUG: debug = False else: debug = False if debug: print("START RdfClass.save ---------------------------\n") if not validation_status: return self.validate_form_data(rdf_obj) save_data = self._process_class_data(rdf_obj) if debug: print("-------------- Save data:\n",pp.pprint(save_data)) save_query = self._generate_save_query(save_data) if debug: print("save_query: \n", pp.pprint(save_query)) if debug: print("END RdfClass.save ---------------------------\n") return self._run_save_query(save_query)
def save(self, rdf_obj, validation_status=True): """Method validates and saves passed data for the class Args: rdf_obj -- Current RDF Form class fields validationS valid_required_props = self._validate_required_properties( rdf_obj, old_form_data) validDependancies = self._validate_dependant_props( rdf_obj, old_form_data)""" if not DEBUG: debug = False else: debug = False if debug: print("START RdfClass.save ---------------------------\n") if not validation_status: return self.validate_form_data(rdf_obj) save_data = self._process_class_data(rdf_obj) if debug: print("-------------- Save data:\n", pp.pprint(save_data)) save_query = self._generate_save_query(save_data) if debug: print("save_query: \n", pp.pprint(save_query)) if debug: print("END RdfClass.save ---------------------------\n") return self._run_save_query(save_query)
def rdf_generic_api(class_uri, prop_uri): if not DEBUG: debug = False else: debug = True if debug: print("START rdf_generic_api ----------------------------\n") subject_uri = request.args.get("id") data = request.form.get("dataValue") subject_uri = request.form.get("id",subject_uri) if debug: print("class_uri: %s prop_uri: %s" % (class_uri, prop_uri)) if debug: print('subject_uri: ', subject_uri) if debug: print('data: ', data) if prop_uri in ["obi_claimDate","kds_errorLog"] and class_uri == "obi_Assertion": if hasattr(request, "form"): csrf = request.form.get("csrf") else: if debug: print("aborting **************") return abort(400) subject_uri = request.args.get("id") data = request.form.get("dataValue") subject_uri = request.form.get("id",subject_uri) #if debug: print("REQUEST dict: \n", pp.pformat(request.__dict__)) rdf_class = getattr(rdfw(), class_uri) prop_json = rdf_class.kds_properties.get(prop_uri) prop_json['kds_classUri'] = class_uri prop_json['kds_apiFieldName'] = prop_uri prop = RdfProperty(prop_json, data, subject_uri) base_url = "%s%s" % (request.url_root[:-1], url_for("open_badge.base_path")) current_url = request.url base_api_url = "%s%sapi/form_generic_prop/" % (request.url_root[:-1], url_for("open_badge.base_path")) if debug: print('rdf_class: ', rdf_class) print('prop_json: ', prop_json) print('prop: ', prop) print('base_url: ', base_url) print('current_url: ', current_url) print('base_api_url: ', base_api_url) api_url = request.base_url rdf_obj = Api(subject_uri=subject_uri, data_class_uri=class_uri, data_prop_uri=prop_uri, rdf_field_list=[prop_json], prop_list = [prop], base_url=base_url, current_url=current_url, base_api_url=base_api_url, api_url=api_url) if request.method == "POST": save_result = rdf_obj.save() if debug: print("**** save_result:\n",pp.pformat(save_result.__dict__)) if debug: print("END rdf_generic_api POST -------------------------\n") return jsonify(save_result.save_results[0]) else: api_data = rdfw().get_obj_data(rdf_obj) if debug: print("\t**** api_data:\n",pp.pprint(api_data)) if debug: print("END rdf_generic_api GET --------------------------\n") return json.dumps(api_data['obj_json'], indent=4)
def __init__(self, json_obj, class_name, **kwargs): if not DEBUG: debug = False else: debug = False if debug: print("\nSTART RdfClass.init ---------------------------\n") self.class_name = None self.kds_properties = {} for _prop in json_obj: setattr(self, _prop, json_obj[_prop]) setattr(self, "class_name", class_name) # The below accounts for cases where we may be using more than one # repository or triplestore # The 'kds_triplestoreConfigName' is the variable name in the # config file that specifies the URL of the triplestore if not hasattr(self, "kds_triplestoreConfigName"): self.kds_triplestoreConfigName = "TRIPLESTORE_URL" if not hasattr(self, "kds_repositoryConfigName"): self.kds_repositoryConfigName = "REPOSITORY_URL" # The kds_saveLocation specifies where to save the data i.e. # in the triplestore or the repository if not hasattr(self, "kds_saveLocation"): self.kds_saveLocation = kwargs.get("kds_saveLocation", "triplestore") # The kds_queryLocation specifies where to query for class data if not hasattr(self, "kds_queryLocation"): self.kds_queryLocation = "triplestore" # set the triplestore and repository urls for the class self.triplestore_url = fw_config().get(self.kds_triplestoreConfigName) self.repository_url = fw_config().get(self.kds_repositoryConfigName) if not hasattr(self, "kds_subjectPattern"): self.kds_subjectPattern = kwargs.get( "kds_subjectPattern", "!--baseUrl,/,ns,/,!--classPrefix,/,!--className,/,!--uuid") if not hasattr(self, "kds_baseUrl"): self.kds_baseUrl = kwargs.get("kds_baseUrl", fw_config().get(\ "ORGANIZATION",{}).get("url", "NO_BASE_URL")) if debug: pp.pprint(self.__dict__) if debug: print("\nEND RdfClass.init ---------------------------\n")
def __init__(self, json_obj, class_name, **kwargs): if not DEBUG: debug = False else: debug = False if debug: print("\nSTART RdfClass.init ---------------------------\n") self.class_name = None self.kds_properties = {} for _prop in json_obj: setattr(self, _prop, json_obj[_prop]) setattr(self, "class_name", class_name) # The below accounts for cases where we may be using more than one # repository or triplestore # The 'kds_triplestoreConfigName' is the variable name in the # config file that specifies the URL of the triplestore if not hasattr(self, "kds_triplestoreConfigName"): self.kds_triplestoreConfigName = "TRIPLESTORE_URL" if not hasattr(self, "kds_repositoryConfigName"): self.kds_repositoryConfigName = "REPOSITORY_URL" # The kds_saveLocation specifies where to save the data i.e. # in the triplestore or the repository if not hasattr(self, "kds_saveLocation"): self.kds_saveLocation = kwargs.get("kds_saveLocation", "triplestore") # The kds_queryLocation specifies where to query for class data if not hasattr(self, "kds_queryLocation"): self.kds_queryLocation = "triplestore" # set the triplestore and repository urls for the class self.triplestore_url = fw_config().get(self.kds_triplestoreConfigName) self.repository_url = fw_config().get(self.kds_repositoryConfigName) if not hasattr(self, "kds_subjectPattern"): self.kds_subjectPattern = kwargs.get("kds_subjectPattern", "!--baseUrl,/,ns,/,!--classPrefix,/,!--className,/,!--uuid") if not hasattr(self, "kds_baseUrl"): self.kds_baseUrl = kwargs.get("kds_baseUrl", fw_config().get(\ "ORGANIZATION",{}).get("url", "NO_BASE_URL")) if debug: pp.pprint(self.__dict__) if debug: print("\nEND RdfClass.init ---------------------------\n")
def create_data_sparql_query(obj, **kwargs): ''' generates the sparql query for getting an object's data ''' if not DEBUG: debug = False else: debug = False if debug: print("START create_data_sparql_query -----------------------\n") if debug: print("*** kwargs ***: \n%s \n*** obj ***:\n%s" % (pp.pformat(kwargs), pp.pformat(obj.__dict__))) from rdfframework import RdfDataType subject_uri = kwargs.get("subject_uri", obj.data_subject_uri) _class_uri = kwargs.get("class_uri", obj.data_class_uri) _formated_val = None _lookup_triple = "" if obj.rdf_instructions.get("kds_subjectUriTransform"): if obj.rdf_instructions.get("kds_subjectUriTransform") == \ "kdr_UidToRepositoryUri": id_value = kwargs.get("id_value") if kwargs.get("id_value"): _subject_uri = uid_to_repo_uri(id_value) subject_uri = _subject_uri obj.data_subject_uri = _subject_uri elif obj.rdf_instructions.get("kds_subjectUriTransform") == \ "kdr_UidToTriplestoreUri": id_value = kwargs.get("id_value") if kwargs.get("id_value"): rdf_class = getattr(rdfw(), obj.data_class_uri) subject_uri = rdf_class.uri_patterner(id_value) obj.data_subject_uri = subject_uri elif kwargs.get("id_value") or obj.rdf_instructions.get( "kds_lookupPropertyUri"): # find the details for formating the sparql query for the supplied # id_value or lookup via a property Value id_value = kwargs.get("id_value") if not id_value: id_value = subject_uri _kds_propUri = obj.rdf_instructions.get("kds_lookupPropertyUri") _rdf_class_uri = obj.rdf_instructions.get("kds_lookupPropertyClass") if not _rdf_class_uri: _rdf_class_uri = obj.rdf_instructions.get("kds_lookupClassUri") _rdf_class = getattr(rdfw(), _rdf_class_uri) _rdf_prop = _rdf_class.kds_properties[_kds_propUri] _range = make_list(_rdf_prop.get("rdfs_range"))[0] _formated_val = RdfDataType(_range.get("rangeClass")).sparql(id_value) _lookup_triple = "\t{}\n\t{}\n\t".format( make_triple("?subject", "a", iri(uri(_rdf_class.kds_classUri))), make_triple("?subject", iri(uri(_kds_propUri)), _formated_val)) subject_uri = "?subject" subject_lookup = kwargs.get("subject_lookup") if subject_lookup: # subject lookup will pull a subject and all of its related data _kds_propUri = iri(uri(subject_lookup.kds_propUri)) _data_type = uri(make_list(subject_lookup.rdfs_range)[0]) _prop_value = RdfDataType(_data_type).sparql(\ str(subject_lookup.data)) _sparql = render_without_request("sparqlRelatedItemDataTemplate.rq", prefix=rdfw().get_prefix(), kds_propUri=_kds_propUri, prop_value=_prop_value) return _sparql _lookup_class_uri = _class_uri _sparql_args = None _sparql_constructor = copy.deepcopy(obj.dependancies) if debug: print("+++++++++++++++++++++++ Dependancies:") pp.pprint(_sparql_constructor) _base_subject_finder = None _linked_class = None _linked_prop = False _sparql_elements = [] _subform_data = {} _data_list = obj.is_subobj _parent_field = None if is_not_null(subject_uri): # find the primary linkage between the supplied subjectId and # other form classes for _rdf_class in _sparql_constructor: for _prop in _sparql_constructor[_rdf_class]: try: if _class_uri == _prop.get("kds_classUri"): _sparql_args = _prop _linked_class = _rdf_class _sparql_constructor[_rdf_class].remove(_prop) if _rdf_class != _lookup_class_uri: _linked_prop = True except: pass # generate the triple pattern for linked class if debug: print("+++++++++++++++++++++++ SPARQL Constructor") pp.pprint(_sparql_constructor) if _sparql_args: # create a binding for multi-item results if _data_list: _list_binding = "BIND(?classID AS ?itemID) ." else: _list_binding = '' # provide connection triples for the id subject and associated # rdf class format_string = "{}BIND({} AS ?baseSub) .\n\t{}\n\t{}\n\t{}" _base_subject_finder = format_string.format( _lookup_triple, iri(subject_uri), make_triple("?baseSub", "a", iri(uri(_lookup_class_uri))), make_triple("?classID", iri(uri(_sparql_args.get("kds_propUri"))), "?baseSub"), _list_binding) # if there is a linkage between subject_uri and another associated # property in object if _linked_prop: # create a binding for multi-item results if _data_list: _list_binding = "BIND(?s AS ?itemID) ." else: _list_binding = '' format_string = \ "{}BIND({} AS ?baseSub) .\n\t{}\n\t{}\n\t{}\n\t?s ?p ?o ." _sparql_elements.append(format_string.format(\ _lookup_triple, iri(subject_uri), make_triple("?baseSub", "a", iri(uri(_lookup_class_uri))), make_triple("?s", iri(uri(_sparql_args.get("kds_propUri"))), "?baseSub"), _list_binding)) # iterrate though the classes used in the object and generate the # spaqrl triples to pull the data for that class for _rdf_class in _sparql_constructor: if _rdf_class == _class_uri: if _data_list: _list_binding = "BIND(?s AS ?itemID) ." else: _list_binding = '' if is_not_null(_lookup_triple) and is_not_null(_list_binding): format_string = \ "{}BIND({} AS ?basesub).\n\t{}\n\t{}\n\t{}\n\t?s ?p ?o ." _sparql_elements.append( format_string.format( _lookup_triple, iri(subject_uri), make_triple('?baseSub', 'a', iri(uri(_lookup_class_uri))), make_triple( '?classID', iri(uri(_sparql_args.get("kds_propUri"))), '?s'), "BIND(?classID AS ?itemID) .")) else: format_string = \ "\t{}BIND({} AS ?s) .\n\t{}\n\t{}\n\t?s ?p ?o ." _sparql_elements.append( format_string.format( _lookup_triple, iri(subject_uri), make_triple("?s", "a", iri(uri(_lookup_class_uri))), _list_binding)) for _prop in _sparql_constructor[_rdf_class]: if _rdf_class == _class_uri: if _data_list: _list_binding = "BIND(?s AS ?itemID) ." else: _list_binding = '' format_string = \ "\t{}BIND({} AS ?baseSub) .\n\t{}\n\t{}\n\t{}\n\t?s ?p ?o ." _sparql_arg = format_string.format(\ _lookup_triple, iri(subject_uri), make_triple("?baseSub", "a", iri(uri(_lookup_class_uri))), make_triple("?baseSub", iri(uri(_prop.get("kds_propUri"))), "?s"), _list_binding) _sparql_elements.append(_sparql_arg) elif _rdf_class == _linked_class: _sparql_elements.append("\t{}\n\t{}\n\t?s ?p ?o .".format( _base_subject_finder, make_triple("?classID", iri(uri(_prop.get("kds_propUri"))), "?s"))) '''**** The case where an ID looking up a the triples for a non-linked related is not functioning i.e. password ClassID not looking up person org triples if the org class is not used in the form. This may not be a problem ... the below comment out is a start to solving if it is a problem elif _linked_class != self.get_class_name(prop.get(\ "classUri")): _sparql_elements.append( "\t" +_base_subject_finder + "\n " + "\t"+ make_triple("?classID", iri(prop.get(\ "propUri")), "?s") + "\n\t?s ?p ?o .")''' # merge the sparql elements for each class used into one combined # sparql union statement _sparql_unions = "{{\n{}\n}}".format("\n} UNION {\n".join(\ _sparql_elements)) if _data_list: _list_binding = "?itemID" else: _list_binding = '' # render the statment in the jinja2 template _sparql = render_without_request("sparqlItemTemplate.rq", prefix=rdfw().get_prefix(), query=_sparql_unions, list_binding=_list_binding) if debug: print("SPARQL query") print(_sparql) if debug: print("END create_data_sparql_query ---------------------\n") return _sparql
def rdf_generic_api(class_uri, prop_uri): if not DEBUG: debug = False else: debug = True if debug: print("START rdf_generic_api ----------------------------\n") subject_uri = request.args.get("id") data = request.form.get("dataValue") subject_uri = request.form.get("id", subject_uri) if debug: print("class_uri: %s prop_uri: %s" % (class_uri, prop_uri)) if debug: print('subject_uri: ', subject_uri) if debug: print('data: ', data) if prop_uri in ["obi_claimDate", "kds_errorLog" ] and class_uri == "obi_Assertion": if hasattr(request, "form"): csrf = request.form.get("csrf") else: if debug: print("aborting **************") return abort(400) subject_uri = request.args.get("id") data = request.form.get("dataValue") subject_uri = request.form.get("id", subject_uri) #if debug: print("REQUEST dict: \n", pp.pformat(request.__dict__)) rdf_class = getattr(rdfw(), class_uri) prop_json = rdf_class.kds_properties.get(prop_uri) prop_json['kds_classUri'] = class_uri prop_json['kds_apiFieldName'] = prop_uri prop = RdfProperty(prop_json, data, subject_uri) base_url = "%s%s" % (request.url_root[:-1], url_for("app.base_path")) current_url = request.url base_api_url = "%s%sapi/form_generic_prop/" % (request.url_root[:-1], url_for("app.base_path")) if debug: print('rdf_class: ', rdf_class) print('prop_json: ', prop_json) print('prop: ', prop) print('base_url: ', base_url) print('current_url: ', current_url) print('base_api_url: ', base_api_url) api_url = request.base_url rdf_obj = Api(subject_uri=subject_uri, data_class_uri=class_uri, data_prop_uri=prop_uri, rdf_field_list=[prop_json], prop_list=[prop], base_url=base_url, current_url=current_url, base_api_url=base_api_url, api_url=api_url) if request.method == "POST": save_result = rdf_obj.save() if debug: print("**** save_result:\n", pp.pformat(save_result.__dict__)) if debug: print("END rdf_generic_api POST -------------------------\n") return jsonify(save_result.save_results[0]) else: api_data = rdfw().get_obj_data(rdf_obj) if debug: print("\t**** api_data:\n", pp.pprint(api_data)) if debug: print("END rdf_generic_api GET --------------------------\n") return json.dumps(api_data['obj_json'], indent=4)
def create_data_sparql_query(obj, **kwargs): ''' generates the sparql query for getting an object's data ''' if not DEBUG: debug = False else: debug = False if debug: print("START create_data_sparql_query -----------------------\n") if debug: print("*** kwargs ***: \n%s \n*** obj ***:\n%s" % (pp.pformat(kwargs), pp.pformat(obj.__dict__))) from rdfframework import RdfDataType subject_uri = kwargs.get("subject_uri", obj.data_subject_uri) _class_uri = kwargs.get("class_uri", obj.data_class_uri) _formated_val = None _lookup_triple = "" if obj.rdf_instructions.get("kds_subjectUriTransform"): if obj.rdf_instructions.get("kds_subjectUriTransform") == \ "kdr_UidToRepositoryUri": id_value = kwargs.get("id_value") if kwargs.get("id_value"): _subject_uri = uid_to_repo_uri(id_value) subject_uri = _subject_uri obj.data_subject_uri = _subject_uri elif obj.rdf_instructions.get("kds_subjectUriTransform") == \ "kdr_UidToTriplestoreUri": id_value = kwargs.get("id_value") if kwargs.get("id_value"): rdf_class = getattr(rdfw(), obj.data_class_uri) subject_uri = rdf_class.uri_patterner(id_value) obj.data_subject_uri = subject_uri elif kwargs.get("id_value") or obj.rdf_instructions.get("kds_lookupPropertyUri"): # find the details for formating the sparql query for the supplied # id_value or lookup via a property Value id_value = kwargs.get("id_value") if not id_value: id_value = subject_uri _kds_propUri = obj.rdf_instructions.get("kds_lookupPropertyUri") _rdf_class_uri = obj.rdf_instructions.get("kds_lookupPropertyClass") if not _rdf_class_uri: _rdf_class_uri = obj.rdf_instructions.get("kds_lookupClassUri") _rdf_class = getattr(rdfw(),_rdf_class_uri) _rdf_prop = _rdf_class.kds_properties[_kds_propUri] _range = make_list(_rdf_prop.get("rdfs_range"))[0] _formated_val = RdfDataType(_range.get("rangeClass")).sparql(id_value) _lookup_triple = "\t{}\n\t{}\n\t".format( make_triple("?subject", "a", iri(uri(_rdf_class.kds_classUri))), make_triple("?subject", iri(uri(_kds_propUri)), _formated_val)) subject_uri = "?subject" subject_lookup = kwargs.get("subject_lookup") if subject_lookup: # subject lookup will pull a subject and all of its related data _kds_propUri = iri(uri(subject_lookup.kds_propUri)) _data_type = uri(make_list(subject_lookup.rdfs_range)[0]) _prop_value = RdfDataType(_data_type).sparql(\ str(subject_lookup.data)) _sparql = render_without_request("sparqlRelatedItemDataTemplate.rq", prefix=rdfw().get_prefix(), kds_propUri=_kds_propUri, prop_value=_prop_value) return _sparql _lookup_class_uri = _class_uri _sparql_args = None _sparql_constructor = copy.deepcopy(obj.dependancies) if debug: print("+++++++++++++++++++++++ Dependancies:") pp.pprint(_sparql_constructor) _base_subject_finder = None _linked_class = None _linked_prop = False _sparql_elements = [] _subform_data = {} _data_list = obj.is_subobj _parent_field = None if is_not_null(subject_uri): # find the primary linkage between the supplied subjectId and # other form classes for _rdf_class in _sparql_constructor: for _prop in _sparql_constructor[_rdf_class]: try: if _class_uri == _prop.get("kds_classUri"): _sparql_args = _prop _linked_class = _rdf_class _sparql_constructor[_rdf_class].remove(_prop) if _rdf_class != _lookup_class_uri: _linked_prop = True except: pass # generate the triple pattern for linked class if debug: print("+++++++++++++++++++++++ SPARQL Constructor") pp.pprint(_sparql_constructor) if _sparql_args: # create a binding for multi-item results if _data_list: _list_binding = "BIND(?classID AS ?itemID) ." else: _list_binding = '' # provide connection triples for the id subject and associated # rdf class format_string = "{}BIND({} AS ?baseSub) .\n\t{}\n\t{}\n\t{}" _base_subject_finder = format_string.format( _lookup_triple, iri(subject_uri), make_triple("?baseSub", "a", iri(uri(_lookup_class_uri))), make_triple("?classID", iri(uri(_sparql_args.get("kds_propUri"))), "?baseSub"), _list_binding) # if there is a linkage between subject_uri and another associated # property in object if _linked_prop: # create a binding for multi-item results if _data_list: _list_binding = "BIND(?s AS ?itemID) ." else: _list_binding = '' format_string = \ "{}BIND({} AS ?baseSub) .\n\t{}\n\t{}\n\t{}\n\t?s ?p ?o ." _sparql_elements.append(format_string.format(\ _lookup_triple, iri(subject_uri), make_triple("?baseSub", "a", iri(uri(_lookup_class_uri))), make_triple("?s", iri(uri(_sparql_args.get("kds_propUri"))), "?baseSub"), _list_binding)) # iterrate though the classes used in the object and generate the # spaqrl triples to pull the data for that class for _rdf_class in _sparql_constructor: if _rdf_class == _class_uri: if _data_list: _list_binding = "BIND(?s AS ?itemID) ." else: _list_binding = '' if is_not_null(_lookup_triple) and is_not_null(_list_binding): format_string = \ "{}BIND({} AS ?basesub).\n\t{}\n\t{}\n\t{}\n\t?s ?p ?o ." _sparql_elements.append(format_string.format( _lookup_triple, iri(subject_uri), make_triple('?baseSub','a',iri(uri(_lookup_class_uri))), make_triple('?classID',iri(uri(_sparql_args.get("kds_propUri"))),'?s'), "BIND(?classID AS ?itemID) .")) else: format_string = \ "\t{}BIND({} AS ?s) .\n\t{}\n\t{}\n\t?s ?p ?o ." _sparql_elements.append(format_string.format( _lookup_triple, iri(subject_uri), make_triple("?s", "a", iri(uri(_lookup_class_uri))), _list_binding)) for _prop in _sparql_constructor[_rdf_class]: if _rdf_class == _class_uri: if _data_list: _list_binding = "BIND(?s AS ?itemID) ." else: _list_binding = '' format_string = \ "\t{}BIND({} AS ?baseSub) .\n\t{}\n\t{}\n\t{}\n\t?s ?p ?o ." _sparql_arg = format_string.format(\ _lookup_triple, iri(subject_uri), make_triple("?baseSub", "a", iri(uri(_lookup_class_uri))), make_triple("?baseSub", iri(uri(_prop.get("kds_propUri"))), "?s"), _list_binding) _sparql_elements.append(_sparql_arg) elif _rdf_class == _linked_class: _sparql_elements.append( "\t{}\n\t{}\n\t?s ?p ?o .".format( _base_subject_finder, make_triple("?classID", iri(uri(_prop.get("kds_propUri"))), "?s") ) ) '''**** The case where an ID looking up a the triples for a non-linked related is not functioning i.e. password ClassID not looking up person org triples if the org class is not used in the form. This may not be a problem ... the below comment out is a start to solving if it is a problem elif _linked_class != self.get_class_name(prop.get(\ "classUri")): _sparql_elements.append( "\t" +_base_subject_finder + "\n " + "\t"+ make_triple("?classID", iri(prop.get(\ "propUri")), "?s") + "\n\t?s ?p ?o .")''' # merge the sparql elements for each class used into one combined # sparql union statement _sparql_unions = "{{\n{}\n}}".format("\n} UNION {\n".join(\ _sparql_elements)) if _data_list: _list_binding = "?itemID" else: _list_binding = '' # render the statment in the jinja2 template _sparql = render_without_request("sparqlItemTemplate.rq", prefix=rdfw().get_prefix(), query=_sparql_unions, list_binding=_list_binding) if debug: print("SPARQL query") print(_sparql) if debug: print("END create_data_sparql_query ---------------------\n") return _sparql
def __format_data_for_save(self, processed_data, pre_save_data): ''' takes the processed data and formats the values for the sparql query ''' if not DEBUG: debug = False else: debug = False if debug: print("START RdfClass.__format_data_for_save -----------\n") _save_data = [] #if "obi_recipient" in pre_save_data.keys(): # x=y if debug: print("format data***********\n") if debug: pp.pprint(processed_data) # cycle throught the properties in the processed data for _prop_uri, prop in processed_data.items(): # if the property is maked for deletion add it to the save list if isinstance(prop, NotInFormClass): pass elif isinstance(prop, DeleteProperty): _save_data.append([_prop_uri, prop]) # if the property is a file object send it to the repository and # add the new uri to the save data list elif isinstance(prop, FileStorage): _file_iri = save_file_to_repository(\ prop, pre_save_data[_prop_uri][0].get('old')) _save_data.append([_prop_uri, _file_iri]) # otherwise determine the range of the property and format it in # the correct sparl format else: # some properties have more than one option for the object # value i.e. schema:image can either store a ImageObject or # a Url to an image. We need to determine the range options _range_list = make_list(self.kds_properties[_prop_uri].get(\ "rdfs_range", [{}])) _storage_types = set() _data_types = set() # cycle through the range_list and get the sets of options for _range_dict in _range_list: _storage_types.add(_range_dict.get('storageType')) if _range_dict.get('storageType') == "literal": _data_types.add(_range_dict.get('rangeClass')) _data_type = "xsd_string" for _type in _data_types: if "xsd" in _type: _data_type = _type # cycle through the items in the current prop _value_list = make_list(prop) for item in _value_list: if 'object' in _storage_types or 'blanknode' in \ _storage_types: uri_test = uri(item) if debug: print(_prop_uri, " - ", uri_test) if uri_test.startswith("http"): _save_data.append([_prop_uri, iri(uri(item))]) elif 'literal' in _storage_types: _save_data.append([_prop_uri, RdfDataType(\ _data_type).sparql(str(item))]) else: _save_data.append([_prop_uri, iri(uri(item))]) else: _item = str(item).encode('unicode_escape') _save_data.append([_prop_uri, RdfDataType(\ _data_type).sparql(str(item))]) if debug: print("END RdfClass.__format_data_for_save -----------\n") return _save_data
def _generate_save_query(self, save_data_obj, subject_uri=None): if not DEBUG: debug = False else: debug = False if debug: print("START RdfClass._generate_save_query -------------\n") _save_data = save_data_obj.get("data") # find the subject_uri positional argument or look in the save_data_obj # or return <> as a new node if not subject_uri: subject_uri = iri(uri(save_data_obj.get('subjectUri', "<>"))) _save_type = self.kds_storageType if subject_uri == "<>" and _save_type.lower() == "blanknode": _save_type = "blanknode" else: _save_type = "object" new_status = False if self.kds_saveLocation == "triplestore" and subject_uri == "<>": subject_uri = iri(self.new_uri()) new_status = True _bn_insert_clause = [] _insert_clause = "" _delete_clause = "" _where_clause = "" _prop_set = set() i = 1 if debug: print("save data in generateSaveQuery\n", \ pp.pprint(_save_data)) # test to see if there is data to save if len(_save_data) > 0: for prop in _save_data: _prop_set.add(uri(prop[0])) _prop_iri = iri(uri(prop[0])) if not isinstance(prop[1], DeleteProperty): _obj_val = uri(prop[1]) _insert_clause += "{}\n".format(\ make_triple(subject_uri, _prop_iri, _obj_val)) _bn_insert_clause.append("\t{} {}".format(_prop_iri, _obj_val)) if subject_uri != '<>' and not new_status: for prop in _prop_set: _prop_iri = iri(uri(prop)) _delete_clause += "{}\n".format(\ make_triple(subject_uri, _prop_iri, "?"+str(i))) _where_clause += "OPTIONAL {{ {} }} .\n".format(\ make_triple(subject_uri, _prop_iri, "?"+str(i))) i += 1 else: _obj_val = iri(uri(self.kds_classUri)) _insert_clause += make_triple(subject_uri, "a", _obj_val) + \ "\n" _bn_insert_clause.append("\t a {}".format(_obj_val)) if _save_type == "blanknode": _save_query = "[\n{}\n]".format(";\n".join(_bn_insert_clause)) else: if subject_uri != '<>' and not new_status: save_query_template = Template("""{{ prefix }} DELETE \n{ {{ _delete_clause }} } INSERT \n{ {{ _insert_clause }} } WHERE \n{ {{ _where_clause }} }""") _save_query = save_query_template.render( prefix=rdfw().get_prefix(), _delete_clause=_delete_clause, _insert_clause=_insert_clause, _where_clause=_where_clause) else: _save_query = "{}\n\n{}".format( rdfw().get_prefix("turtle"), _insert_clause) if debug: print(_save_query) if debug: print("END RdfClass._generate_save_query -------------\n") return {"query":_save_query, "subjectUri":subject_uri, "new_status":new_status} else: if debug: print("END RdfClass._generate_save_query -------------\n") return {"subjectUri":subject_uri}
def _process_class_data(self, rdf_obj): '''Reads through the processors in the defination and processes the data for saving''' if not DEBUG: debug = False else: debug = False if debug: print("START rdfclass.RdfClass._process_class_data ------\n") _pre_save_data = {} _save_data = {} _processed_data = {} obj = {} _required_props = self.list_required() _calculated_props = self._get_calculated_properties() _old_data = self._select_class_query_data(rdf_obj.query_data) # cycle through the form class data and add old, new, doNotSave and # processors for each property _class_obj_props = rdf_obj.class_grouping.get(self.kds_classUri,[]) subject_uri = "<>" for prop in _class_obj_props: if hasattr(prop, "subject_uri"): if prop.subject_uri is not None: subject_uri = prop.subject_uri break subject_uri = _old_data.get("!!!!subjectUri", "<>") for prop in _class_obj_props: if debug: print("prop dict ----------------------\n", pp.pprint(prop.__dict__), "\n---------------------\n") _prop_uri = prop.kds_propUri if debug: if _prop_uri == "schema_image": x=1 # gather all of the processors for the property _class_prop = self.kds_properties.get(_prop_uri,{}) _class_prop_processors = make_list(_class_prop.get("kds_propertyProcessing")) _form_prop_processors = make_list(prop.kds_processors) # clean the list of processors by sending a list based on # precedence i.e. the form processors should override the rdf_class # processors processors = clean_processors([_form_prop_processors, _class_prop_processors], self.kds_classUri) # remove the property from the list of required properties # required properties not in the form will need to be addressed _required_prop = False if _prop_uri in _required_props: _required_props.remove(_prop_uri) _required_prop = True # remove the property from the list of calculated properties # calculated properties not in the form will need to be addressed if _prop_uri in _calculated_props: _calculated_props.remove(_prop_uri) # add the information to the _pre_save_data object if not _pre_save_data.get(_prop_uri): _pre_save_data[_prop_uri] =\ {"new":prop.data, "old":prop.old_data, "classUri": prop.kds_classUri, "required": _required_prop, "editable": prop.editable, "doNotSave": prop.doNotSave, "processors": processors} else: _temp_list = make_list(_pre_save_data[_prop_uri]) _temp_list.append(\ {"new":prop.data, "old":prop.old_data, "classUri": prop.kds_classUri, "required": _required_prop, "editable": prop.editable, "doNotSave": prop.doNotSave, "processors": processors}) _pre_save_data[_prop_uri] = _temp_list # now deal with missing required properties. cycle through the # remaing properties and add them to the _pre_save_data object for _prop_uri in _required_props: _class_prop = self.kds_properties.get(_prop_uri,{}) _class_prop_processors = clean_processors([make_list(\ _class_prop.get("kds_propertyProcessing"))], self.kds_classUri) if _prop_uri == "schema_alternativeName": x=1 # remove the prop from the remaining calculated props if _prop_uri in _calculated_props: _calculated_props.remove(_prop_uri) if not _pre_save_data.get(_prop_uri): _pre_save_data[_prop_uri] =\ {"new":NotInFormClass(), "old":_old_data.get(_prop_uri), "doNotSave":False, "classUri": self.kds_classUri, "required": True, "editable": True, "processors":_class_prop_processors, "defaultVal":_class_prop.get("kds_defaultVal")} if debug: print("psave: ", _pre_save_data[_prop_uri]) else: _temp_list = make_list(_pre_save_data[_prop_uri]) _pre_save_data[_prop_uri] = _temp_list.append(\ {"new":NotInFormClass(), "old":_old_data.get(_prop_uri), "doNotSave": False, "classUri": self.kds_classUri, "editable": True, "processors":_class_prop_processors, "defaultVal":_class_prop.get("kds_defaultVal")}) # now deal with missing calculated properties. cycle through the # remaing properties and add them to the _pre_save_data object if debug: print("calc props: ", _calculated_props) for _prop_uri in _calculated_props: if debug: print("########### _calculated_props: ") _class_prop = self.kds_properties.get(_prop_uri,{}) _class_prop_processors = clean_processors([make_list(\ _class_prop.get("kds_propertyProcessing"))], self.kds_classUri) if not _pre_save_data.get(_prop_uri): _pre_save_data[_prop_uri] =\ {"new":NotInFormClass(), "old":_old_data.get(_prop_uri), "doNotSave":False, "processors":_class_prop_processors, "defaultVal":_class_prop.get("kds_defaultVal")} else: _temp_list = make_list(_pre_save_data[_prop_uri]) _pre_save_data[_prop_uri] =\ _temp_list.append(\ {"new":NotInFormClass(), "old":_old_data.get(_prop_uri), "doNotSave":False, "processors":_class_prop_processors, "defaultVal":_class_prop.get("kds_defaultVal")}) # cycle through the consolidated list of _pre_save_data to # test the security, run the processors and calculate any values for _prop_uri, prop in _pre_save_data.items(): # ******* doNotSave property is set during form field creation # in get_wtform_field method. It tags fields that are there for # validation purposes i.e. password confirm fields ****** if isinstance(prop, list): # a list with in the property occurs when there are # multiple fields tied to the property. i.e. # password creation or change / imagefield that # takes a URL or file for _prop_instance in prop: if _prop_instance.get("doNotSave", False): _pre_save_data[_prop_uri].remove(_prop_instance) if len(make_list(_pre_save_data[_prop_uri])) == 1: _pre_save_data[_prop_uri] = _pre_save_data[_prop_uri][0] # doNotSave = prop.get("doNotSave", False) for _prop_uri, _prop in _pre_save_data.items(): # send each property to be proccessed if _prop: obj = self._process_prop({"propUri":_prop_uri, "prop": _prop, "processedData": _processed_data, "preSaveData": _pre_save_data}) _processed_data = obj["processedData"] _pre_save_data = obj["preSaveData"] if debug: print("PreSaveData----------------") if debug: print(json.dumps(dumpable_obj(_pre_save_data), indent=4)) _save_data = {"data":self.__format_data_for_save(_processed_data, _pre_save_data), "subjectUri":subject_uri} return _save_data
def _generate_save_query(self, save_data_obj, subject_uri=None): if not DEBUG: debug = False else: debug = False if debug: print("START RdfClass._generate_save_query -------------\n") _save_data = save_data_obj.get("data") # find the subject_uri positional argument or look in the save_data_obj # or return <> as a new node if not subject_uri: subject_uri = iri(uri(save_data_obj.get('subjectUri', "<>"))) _save_type = self.kds_storageType if subject_uri == "<>" and _save_type.lower() == "blanknode": _save_type = "blanknode" else: _save_type = "object" new_status = False if self.kds_saveLocation == "triplestore" and subject_uri == "<>": subject_uri = iri(self.new_uri()) new_status = True _bn_insert_clause = [] _insert_clause = "" _delete_clause = "" _where_clause = "" _prop_set = set() i = 1 if debug: print("save data in generateSaveQuery\n", \ pp.pprint(_save_data)) # test to see if there is data to save if len(_save_data) > 0: for prop in _save_data: _prop_set.add(uri(prop[0])) _prop_iri = iri(uri(prop[0])) if not isinstance(prop[1], DeleteProperty): _obj_val = uri(prop[1]) _insert_clause += "{}\n".format(\ make_triple(subject_uri, _prop_iri, _obj_val)) _bn_insert_clause.append("\t{} {}".format( _prop_iri, _obj_val)) if subject_uri != '<>' and not new_status: for prop in _prop_set: _prop_iri = iri(uri(prop)) _delete_clause += "{}\n".format(\ make_triple(subject_uri, _prop_iri, "?"+str(i))) _where_clause += "OPTIONAL {{ {} }} .\n".format(\ make_triple(subject_uri, _prop_iri, "?"+str(i))) i += 1 else: _obj_val = iri(uri(self.kds_classUri)) _insert_clause += make_triple(subject_uri, "a", _obj_val) + \ "\n" _bn_insert_clause.append("\t a {}".format(_obj_val)) if _save_type == "blanknode": _save_query = "[\n{}\n]".format(";\n".join(_bn_insert_clause)) else: if subject_uri != '<>' and not new_status: save_query_template = Template("""{{ prefix }} DELETE \n{ {{ _delete_clause }} } INSERT \n{ {{ _insert_clause }} } WHERE \n{ {{ _where_clause }} }""") _save_query = save_query_template.render( prefix=rdfw().get_prefix(), _delete_clause=_delete_clause, _insert_clause=_insert_clause, _where_clause=_where_clause) else: _save_query = "{}\n\n{}".format( rdfw().get_prefix("turtle"), _insert_clause) if debug: print(_save_query) if debug: print("END RdfClass._generate_save_query -------------\n") return { "query": _save_query, "subjectUri": subject_uri, "new_status": new_status } else: if debug: print("END RdfClass._generate_save_query -------------\n") return {"subjectUri": subject_uri}
def _process_class_data(self, rdf_obj): '''Reads through the processors in the defination and processes the data for saving''' if not DEBUG: debug = False else: debug = False if debug: print("START rdfclass.RdfClass._process_class_data ------\n") _pre_save_data = {} _save_data = {} _processed_data = {} obj = {} _required_props = self.list_required() _calculated_props = self._get_calculated_properties() _old_data = self._select_class_query_data(rdf_obj.query_data) # cycle through the form class data and add old, new, doNotSave and # processors for each property _class_obj_props = rdf_obj.class_grouping.get(self.kds_classUri, []) subject_uri = "<>" for prop in _class_obj_props: if hasattr(prop, "subject_uri"): if prop.subject_uri is not None: subject_uri = prop.subject_uri break subject_uri = _old_data.get("!!!!subjectUri", "<>") for prop in _class_obj_props: if debug: print("prop dict ----------------------\n", pp.pprint(prop.__dict__), "\n---------------------\n") _prop_uri = prop.kds_propUri if debug: if _prop_uri == "schema_image": x = 1 # gather all of the processors for the property _class_prop = self.kds_properties.get(_prop_uri, {}) _class_prop_processors = make_list( _class_prop.get("kds_propertyProcessing")) _form_prop_processors = make_list(prop.kds_processors) # clean the list of processors by sending a list based on # precedence i.e. the form processors should override the rdf_class # processors processors = clean_processors( [_form_prop_processors, _class_prop_processors], self.kds_classUri) # remove the property from the list of required properties # required properties not in the form will need to be addressed _required_prop = False if _prop_uri in _required_props: _required_props.remove(_prop_uri) _required_prop = True # remove the property from the list of calculated properties # calculated properties not in the form will need to be addressed if _prop_uri in _calculated_props: _calculated_props.remove(_prop_uri) # add the information to the _pre_save_data object if not _pre_save_data.get(_prop_uri): _pre_save_data[_prop_uri] =\ {"new":prop.data, "old":prop.old_data, "classUri": prop.kds_classUri, "required": _required_prop, "editable": prop.editable, "doNotSave": prop.doNotSave, "processors": processors} else: _temp_list = make_list(_pre_save_data[_prop_uri]) _temp_list.append(\ {"new":prop.data, "old":prop.old_data, "classUri": prop.kds_classUri, "required": _required_prop, "editable": prop.editable, "doNotSave": prop.doNotSave, "processors": processors}) _pre_save_data[_prop_uri] = _temp_list # now deal with missing required properties. cycle through the # remaing properties and add them to the _pre_save_data object for _prop_uri in _required_props: _class_prop = self.kds_properties.get(_prop_uri, {}) _class_prop_processors = clean_processors([make_list(\ _class_prop.get("kds_propertyProcessing"))], self.kds_classUri) if _prop_uri == "schema_alternativeName": x = 1 # remove the prop from the remaining calculated props if _prop_uri in _calculated_props: _calculated_props.remove(_prop_uri) if not _pre_save_data.get(_prop_uri): _pre_save_data[_prop_uri] =\ {"new":NotInFormClass(), "old":_old_data.get(_prop_uri), "doNotSave":False, "classUri": self.kds_classUri, "required": True, "editable": True, "processors":_class_prop_processors, "defaultVal":_class_prop.get("kds_defaultVal")} if debug: print("psave: ", _pre_save_data[_prop_uri]) else: _temp_list = make_list(_pre_save_data[_prop_uri]) _pre_save_data[_prop_uri] = _temp_list.append(\ {"new":NotInFormClass(), "old":_old_data.get(_prop_uri), "doNotSave": False, "classUri": self.kds_classUri, "editable": True, "processors":_class_prop_processors, "defaultVal":_class_prop.get("kds_defaultVal")}) # now deal with missing calculated properties. cycle through the # remaing properties and add them to the _pre_save_data object if debug: print("calc props: ", _calculated_props) for _prop_uri in _calculated_props: if debug: print("########### _calculated_props: ") _class_prop = self.kds_properties.get(_prop_uri, {}) _class_prop_processors = clean_processors([make_list(\ _class_prop.get("kds_propertyProcessing"))], self.kds_classUri) if not _pre_save_data.get(_prop_uri): _pre_save_data[_prop_uri] =\ {"new":NotInFormClass(), "old":_old_data.get(_prop_uri), "doNotSave":False, "processors":_class_prop_processors, "defaultVal":_class_prop.get("kds_defaultVal")} else: _temp_list = make_list(_pre_save_data[_prop_uri]) _pre_save_data[_prop_uri] =\ _temp_list.append(\ {"new":NotInFormClass(), "old":_old_data.get(_prop_uri), "doNotSave":False, "processors":_class_prop_processors, "defaultVal":_class_prop.get("kds_defaultVal")}) # cycle through the consolidated list of _pre_save_data to # test the security, run the processors and calculate any values for _prop_uri, prop in _pre_save_data.items(): # ******* doNotSave property is set during form field creation # in get_wtform_field method. It tags fields that are there for # validation purposes i.e. password confirm fields ****** if isinstance(prop, list): # a list with in the property occurs when there are # multiple fields tied to the property. i.e. # password creation or change / imagefield that # takes a URL or file for _prop_instance in prop: if _prop_instance.get("doNotSave", False): _pre_save_data[_prop_uri].remove(_prop_instance) if len(make_list(_pre_save_data[_prop_uri])) == 1: _pre_save_data[_prop_uri] = _pre_save_data[_prop_uri][0] # doNotSave = prop.get("doNotSave", False) for _prop_uri, _prop in _pre_save_data.items(): # send each property to be proccessed if _prop: obj = self._process_prop({ "propUri": _prop_uri, "prop": _prop, "processedData": _processed_data, "preSaveData": _pre_save_data }) _processed_data = obj["processedData"] _pre_save_data = obj["preSaveData"] if debug: print("PreSaveData----------------") if debug: print(json.dumps(dumpable_obj(_pre_save_data), indent=4)) _save_data = { "data": self.__format_data_for_save(_processed_data, _pre_save_data), "subjectUri": subject_uri } return _save_data