def query_select_options(field): ''' returns a list of key value pairs for a select field ''' _prefix = rdfw().get_prefix() _select_query = field.kds_fieldType.get('kds_selectQuery', None) _select_list = {} _options = [] if _select_query: # send query to triplestore _select_list = requests.post(fw_config().get('TRIPLESTORE_URL'), data={ "query": _prefix + _select_query, "format": "json" }) _raw_options = _select_list.json().get('results', {}).get('bindings', []) _bound_var = field.kds_fieldType.get('kds_selectBoundValue', ''\ ).replace("?", "") _display_var = field.kds_fieldType.get('kds_selectDisplay', ''\ ).replace("?", "") # format query result into key value pairs for row in _raw_options: _options.append({ "id": iri(row.get(_bound_var, {}).get('value', '')), "value": row.get(_display_var, {}).get('value', '') }) return _options
def query_select_options(field): ''' returns a list of key value pairs for a select field ''' _prefix = rdfw().get_prefix() _select_query = field.kds_fieldType.get('kds_selectQuery', None) _select_list = {} _options = [] if _select_query: # send query to triplestore _select_list = requests.post( fw_config().get('TRIPLESTORE_URL'), data={"query": _prefix + _select_query, "format": "json"}) _raw_options = _select_list.json().get('results', {}).get('bindings', []) _bound_var = field.kds_fieldType.get('kds_selectBoundValue', ''\ ).replace("?", "") _display_var = field.kds_fieldType.get('kds_selectDisplay', ''\ ).replace("?", "") # format query result into key value pairs for row in _raw_options: _options.append( { "id":iri(row.get(_bound_var, {}).get('value', '')), "value":row.get(_display_var, {}).get('value', '') }) return _options
def get_framework(**kwargs): ''' sets an instance of the the framework as a global variable. This this method is then called to access that specific instance ''' global RDF_GLOBAL fw_config(config=kwargs.get("config")) _reset = kwargs.get("reset") if _reset: from .framework import RdfFramework RDF_GLOBAL = RdfFramework() try: RDF_GLOBAL except NameError: RDF_GLOBAL = None if RDF_GLOBAL is None: from .framework import RdfFramework RDF_GLOBAL = RdfFramework() return RDF_GLOBAL
def run_sparql_query(sparql, **kwargs): ''' run the passed in sparql query and returns the results ''' _prefix = rdfw().get_prefix() if sparql is not None: _results = requests.post(fw_config().get('TRIPLESTORE_URL'), data={"prefix": _prefix, "query": sparql, "format": "json"}) return _results.json().get('results', {}).get('bindings', []) else: return None
def save_file_to_repository(data, repo_item_address): ''' saves a file from a form to a repository''' object_value = "" if repo_item_address: print("~~~~~~~~ write code here") else: repository_result = requests.post( fw_config().get("REPOSITORY_URL"), data=data.read(), headers={"Content-type": "'image/png'"}) object_value = repository_result.text return iri(object_value)
def save_file_to_repository(data, repo_item_address): ''' saves a file from a form to a repository''' object_value = "" if repo_item_address: print("~~~~~~~~ write code here") else: repository_result = requests.post( fw_config().get("REPOSITORY_URL"), data=data.read(), headers={"Content-type":"'image/png'"}) object_value = repository_result.text return iri(object_value)
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 run_sparql_query(sparql, **kwargs): ''' run the passed in sparql query and returns the results ''' _prefix = rdfw().get_prefix() if sparql is not None: _results = requests.post(fw_config().get('TRIPLESTORE_URL'), data={ "prefix": _prefix, "query": sparql, "format": "json" }) return _results.json().get('results', {}).get('bindings', []) else: return None
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 __call__(self, form, field): # get the test query debug = True _sparql = self._make_unique_value_qry(form, field) if debug: print(_sparql) # run the test query _unique_test_results = requests.post(\ fw_config().get('TRIPLESTORE_URL'), data={"query": _sparql, "format": "json"}) _unique_test = _unique_test_results.json().get('results').get( \ 'bindings', []) # evaluate the results; True result in the query denotes that the # value already exists if len(_unique_test) > 0: _unique_test = _unique_test[0].get(\ 'uniqueValueViolation', {}).get('value', False) else: _unique_test = False if _unique_test: raise ValidationError(self.message)