コード例 #1
0
 def _process_prop(self, obj):
     # obj = propUri, prop, processedData, _pre_save_data
     # !!!!!!! the merge_prop function will need to be relooked for
     # instances where we have multiple property entries i.e. a fieldList
     if not DEBUG:
         debug = False
     else:
         debug = False
     if debug: print("START RdfClass._process_prop -------------------\n")
     if len(make_list(obj['prop'])) > 1:
         obj = self.__merge_prop(obj)
     processors = obj['prop'].get("processors", [])
     _prop_uri = obj['propUri']
     # process properties that are not in the form
     if isinstance(obj['prop'].get("new"), NotInFormClass) and \
             not is_not_null(obj['prop'].get("old")):
         # process required properties
         if obj['prop'].get("required"):
             # run all processors: the processor determines how to
             # handle if there is old data
             if len(processors) > 0:
                 for processor in processors.values():
                     obj = run_processor(processor, obj)
             # if the processors did not calculate a value for the
             # property attempt to calculte from the default
             # property settings
             if not obj['prop'].get('calcValue', False):
                 obj_value = calculate_default_value(obj['prop'])
                 obj['processedData'][obj['propUri']] = obj_value
         #else:
         # need to decide if you want to calculate properties
         # that are not required and not in the form
     # if the property is editable process the data
     elif obj['prop'].get("editable"):
         # if the old and new data are different
         #print(obj['prop'].get("new"), " != ", obj['prop'].get("old"))
         if clean_iri(obj['prop'].get("new")) != \
                                 clean_iri(obj['prop'].get("old")):
             #print("true")
             # if the new data is null and the property is not
             # required mark property for deletion
             if not is_not_null(obj['prop'].get("new")) and not \
                                             obj['prop'].get("required"):
                 obj['processedData'][_prop_uri] = DeleteProperty()
             # if the property has new data
             elif is_not_null(obj['prop'].get("new")):
                 if len(processors) > 0:
                     for processor in processors.values():
                         obj = run_processor(processor, obj)
                     if not obj['prop'].get('calcValue', False):
                         obj['processedData'][_prop_uri] = \
                                                    obj['prop'].get("new")
                 else:
                     obj['processedData'][_prop_uri] = obj['prop'].get(
                         "new")
     if debug: print("END RdfClass._process_prop -------------------\n")
     return obj
コード例 #2
0
 def _process_prop(self, obj):
     # obj = propUri, prop, processedData, _pre_save_data
     # !!!!!!! the merge_prop function will need to be relooked for 
     # instances where we have multiple property entries i.e. a fieldList
     if not DEBUG:
         debug = False
     else:
         debug = False
     if debug: print("START RdfClass._process_prop -------------------\n")
     if len(make_list(obj['prop'])) > 1:
         obj = self.__merge_prop(obj)
     processors = obj['prop'].get("processors", [])
     _prop_uri = obj['propUri']
     # process properties that are not in the form
     if isinstance(obj['prop'].get("new"), NotInFormClass) and \
             not is_not_null(obj['prop'].get("old")):
         # process required properties
         if obj['prop'].get("required"):
             # run all processors: the processor determines how to
             # handle if there is old data
             if len(processors) > 0:
                 for processor in processors.values():
                     obj = run_processor(processor, obj)
             # if the processors did not calculate a value for the
             # property attempt to calculte from the default
             # property settings
             if not obj['prop'].get('calcValue', False):
                 obj_value = calculate_default_value(obj['prop'])
                 obj['processedData'][obj['propUri']] = obj_value
         #else:
             # need to decide if you want to calculate properties
             # that are not required and not in the form
     # if the property is editable process the data
     elif obj['prop'].get("editable"):
         # if the old and new data are different
         #print(obj['prop'].get("new"), " != ", obj['prop'].get("old"))
         if clean_iri(obj['prop'].get("new")) != \
                                 clean_iri(obj['prop'].get("old")):
             #print("true")
             # if the new data is null and the property is not
             # required mark property for deletion
             if not is_not_null(obj['prop'].get("new")) and not \
                                             obj['prop'].get("required"):
                 obj['processedData'][_prop_uri] = DeleteProperty()
             # if the property has new data
             elif is_not_null(obj['prop'].get("new")):
                 if len(processors) > 0:
                     for processor in processors.values():
                         obj = run_processor(processor, obj)
                     if not obj['prop'].get('calcValue', False):
                         obj['processedData'][_prop_uri] = \
                                                    obj['prop'].get("new")
                 else:
                     obj['processedData'][_prop_uri] = obj['prop'].get("new")
     if debug: print("END RdfClass._process_prop -------------------\n")
     return obj
コード例 #3
0
 def __call__(self, field, **kwargs):
     if hasattr(field, 'kds_buttonAction'):
         button_action = field.kds_buttonAction
     else:
         button_action = ''
     if hasattr(field, 'kds_buttonText'):
         button_text = field.kds_buttonText
     else:
         button_text = {'false': 'Click', 'true': 'Resend'}
     if hasattr(field, 'kds_buttonLink'):
         button_link = field.kds_buttonLink
     else:
         button_link = ''
     if hasattr(field, 'kds_buttonFalseCss'):
         button_true_css = field.kds_buttonFalseCss
     else:
         button_true_css = 'btn-success'
     if hasattr(field, 'kds_buttonTrueCss'):
         button_false_css = field.kds_buttonTrueCss
     else:
         button_false_css = 'btn-danger'
     button_action = kwargs.get('button_action', button_action)
     button_text = kwargs.get('button_text', button_text)
     button_link = kwargs.get('button_link', button_link)
     css = kwargs.pop('class', '') or kwargs.pop('class_', '')
     if is_not_null(field.data):
         css = "%s %s" % (css, button_true_css)
     else:
         css = "%s %s" % (css, button_false_css)
     if button_action[:-2] == "()":
         button_action = button_action[:-2]
     return_args = []
     return_args.append("<a ")
     if is_not_null(field.data):
         button_text = button_text['true']
     else:
         button_text = button_text['false']
     if is_not_null(button_action):
         return_args.append("href='javascript:;' ")
         return_args.append('onclick="%s(\'%s\',this,\'%s\',\'%s\')" ' %
                 (button_action, field.data, button_false_css, \
                  button_true_css))
     else:
         return_args.append("href='%s' " % button_link)
     return_args.append("class='%s' " % css)
     return_args.append("kds_propUri='%s' " % field.kds_propUri)
     return_args.append("kds_classUri='%s' " % field.kds_classUri)
     return_args.append("kds_errorLogPropUri='%s' " % \
                        field.kds_errorLogPropUri)
     return_args.append("data=\"%s\" " % field.data)
     return_args.append("id='%s' " % field.name)
     return_args.append(">%s</a>" % button_text)
     return "".join(return_args)
コード例 #4
0
 def __call__(self, field, **kwargs):
     if hasattr(field,'kds_buttonAction'):
         button_action = field.kds_buttonAction
     else:
         button_action = ''
     if hasattr(field,'kds_buttonText'):
         button_text = field.kds_buttonText
     else:
         button_text = 	{'false': 'Click', 'true': 'Resend'}
     if hasattr(field,'kds_buttonLink'):
         button_link = field.kds_buttonLink
     else:
         button_link = ''
     if hasattr(field,'kds_buttonFalseCss'):
         button_true_css = field.kds_buttonFalseCss
     else:
         button_true_css = 'btn-success'
     if hasattr(field,'kds_buttonTrueCss'):
         button_false_css = field.kds_buttonTrueCss
     else:
         button_false_css = 'btn-danger'
     button_action = kwargs.get('button_action', button_action)
     button_text = kwargs.get('button_text', button_text)
     button_link = kwargs.get('button_link', button_link)
     css = kwargs.pop('class', '') or kwargs.pop('class_', '')
     if is_not_null(field.data):
         css = "%s %s" % (css, button_true_css)
     else:
         css = "%s %s" % (css, button_false_css)
     if button_action[:-2] == "()":
         button_action = button_action[:-2]
     return_args = []
     return_args.append("<a ")
     if is_not_null(field.data):
         button_text = button_text['true']
     else:
         button_text = button_text['false']
     if is_not_null(button_action):
         return_args.append("href='javascript:;' ")
         return_args.append('onclick="%s(\'%s\',this,\'%s\',\'%s\')" ' % 
                 (button_action, field.data, button_false_css, \
                  button_true_css))
     else:
         return_args.append("href='%s' " % button_link)
     return_args.append("class='%s' " % css)
     return_args.append("kds_propUri='%s' " % field.kds_propUri)
     return_args.append("kds_classUri='%s' " % field.kds_classUri)
     return_args.append("kds_errorLogPropUri='%s' " % \
                        field.kds_errorLogPropUri)
     return_args.append("data=\"%s\" " % field.data)
     return_args.append("id='%s' " % field.name)
     return_args.append(">%s</a>" % button_text)
     return "".join(return_args)
コード例 #5
0
 def _get_calculated_properties(self):
     '''lists the properties that will be calculated if no value is
        supplied'''
     _calc_list = set()
     # get the list of processors that will calculate a value for a property
     _value_processors = rdfw().value_processors
     for _prop in self.kds_properties:
         # Any properties that have a default value will be generated at
         # time of save
         if is_not_null(self.kds_properties[_prop].get('kds_defaultVal')):
             _calc_list.add(self.kds_properties[_prop].get('kds_propUri'))
         # get the processors that will run on the property
         _processors = make_list(self.kds_properties[_prop].get(\
                 'kds_propertyProcessing', []))
         # find the processors that will generate a value
         for _processor in _processors:
             #print("processor: ", processor)
             if _processor.get("rdf_type") in _value_processors:
                 _calc_list.add(_prop)
     #any dependant properties will be generated at time of save
     _dependent_list = self.list_dependant()
     # properties that are dependant on another class will assume to be 
     # calculated
     for _prop in _dependent_list:
         _calc_list.add(_prop.get("kds_propUri"))
     return remove_null(_calc_list)
コード例 #6
0
 def _validate_dependant_props(self, rdf_obj, old_data):
     '''Validates that all supplied dependant properties have a uri as an
         object'''
     # dep = self.list_dependant()
     # _return_error = []
     _data_props = set()
     for _prop in rdf_obj:
         #remove empty data properties from consideration
         if is_not_null(_prop.data):
             _data_props.add(_prop.kds_propUri)
     '''for p in dep:
         _data_value = data.get(p)
         if (is_not_null(_data_value)):
             propDetails = self.kds_properties[p]
             r = propDetails.get('range')
             literalOk = false
             for i in r:
                 if i.get('storageType')=='literal':
                     literalOk = True
             if not is_valid_object(_data_value) and not literalOk:
                 _return_error.append({
                     "errorType":"missingDependantObject",
                     "errorData":{
                         "class":self.kds_classUri,
                         "properties":propDetails.get('propUri')}})
     if len(_return_error) > 0:
         return _return_error
     else:'''
     return ["valid"]
コード例 #7
0
 def _get_calculated_properties(self):
     '''lists the properties that will be calculated if no value is
        supplied'''
     _calc_list = set()
     # get the list of processors that will calculate a value for a property
     _value_processors = rdfw().value_processors
     for _prop in self.kds_properties:
         # Any properties that have a default value will be generated at
         # time of save
         if is_not_null(self.kds_properties[_prop].get('kds_defaultVal')):
             _calc_list.add(self.kds_properties[_prop].get('kds_propUri'))
         # get the processors that will run on the property
         _processors = make_list(self.kds_properties[_prop].get(\
                 'kds_propertyProcessing', []))
         # find the processors that will generate a value
         for _processor in _processors:
             #print("processor: ", processor)
             if _processor.get("rdf_type") in _value_processors:
                 _calc_list.add(_prop)
     #any dependant properties will be generated at time of save
     _dependent_list = self.list_dependant()
     # properties that are dependant on another class will assume to be
     # calculated
     for _prop in _dependent_list:
         _calc_list.add(_prop.get("kds_propUri"))
     return remove_null(_calc_list)
コード例 #8
0
 def _validate_dependant_props(self, rdf_obj, old_data):
     '''Validates that all supplied dependant properties have a uri as an
         object'''
     # dep = self.list_dependant()
     # _return_error = []
     _data_props = set()
     for _prop in rdf_obj:
         #remove empty data properties from consideration
         if is_not_null(_prop.data):
             _data_props.add(_prop.kds_propUri)
     '''for p in dep:
         _data_value = data.get(p)
         if (is_not_null(_data_value)):
             propDetails = self.kds_properties[p]
             r = propDetails.get('range')
             literalOk = false
             for i in r:
                 if i.get('storageType')=='literal':
                     literalOk = True
             if not is_valid_object(_data_value) and not literalOk:
                 _return_error.append({
                     "errorType":"missingDependantObject",
                     "errorData":{
                         "class":self.kds_classUri,
                         "properties":propDetails.get('propUri')}})
     if len(_return_error) > 0:
         return _return_error
     else:'''
     return ["valid"]
コード例 #9
0
 def _validate_required_properties(self, rdf_obj, old_data):
     '''Validates whether all required properties have been supplied and
         contain data '''
     debug = False
     _return_error = []
     #create sets for evaluating requiredFields
     _required = self.list_required()
     if debug: print("Required Props: ", _required)
     _data_props = set()
     _deleted_props = set()
     for prop in rdf_obj:
         #remove empty data properties from consideration
         if debug: print(prop, "\n")
         if is_not_null(prop.data) or prop.data != 'None':
             _data_props.add(prop.kds_propUri)
         else:
             _deleted_props.add(prop.kds_propUri)
     # find the properties that already exist in the saved class data
     _old_class_data = self._select_class_query_data(old_data)
     for _prop in _old_class_data:
         # remove empty data properties from consideration
         if is_not_null(_old_class_data[_prop]) or _old_class_data[_prop]\
                 != 'None':
             _data_props.add(_prop)
     # remove the _deleted_props from consideration and add calculated props
     _valid_props = (_data_props - _deleted_props).union( \
             self._get_calculated_properties())
     #Test to see if all the required properties are supplied
     missing_required_properties = _required - _valid_props
     if len(missing_required_properties) > 0:
         _return_error.append({
             "errorType": "missing_required_properties",
             "errorData": {
                 "class": self.kds_classUri,
                 "properties": make_list(missing_required_properties)
             }
         })
     if len(_return_error) > 0:
         _return_val = _return_error
     else:
         _return_val = ["valid"]
     return _return_val
コード例 #10
0
    def set_obj_data(self, **kwargs):
        ''' sets the data for the current form paramters

        **keyword arguments
        subject_uri: the URI for the subject
        class_uri: the rdf class of the subject
        '''
        _class_uri = kwargs.get("class_uri", self.data_class_uri)
        _lookup_class_uri = _class_uri
        subject_uri = kwargs.get("subject_uri", self.data_subject_uri)
        if not is_not_null(subject_uri):
            self.query_data = {}
            return None
        _subform_data = {}
        _parent_field = None
        # test to see if a sub_obj is part of the form.
        '''if self.has_subobj:
            for _field in self.rdf_field_list:
                if _field.type == 'FieldList':
                    for _entry in _field.entries:
                        if _entry.type == 'FormField':
                            _sub_rdf_obj = _entry.form
                            _parent_field = _field
            # if the sub_obj get its data
            if _sub_rdf_obj:
                _subform_data = _sub_rdf_obj.query_data'''
        # send the form to the query generator and get the query data back
        if kwargs.get('query_data') is None:
            return None
            '''self.query_data = convert_obj_to_rdf_namespace(\
                    convert_spo_to_dict(get_data(self, **kwargs)))'''
        else:
            self.query_data = kwargs.get('query_data')
        _data_value = None
        # cycle through the query data and add the data to the fields
        for _item in make_list(self.query_data):
            for _prop in self.rdf_field_list:
                _prop_uri = _prop.kds_propUri
                _class_uri = iri(uri(_prop.kds_classUri))
                for _subject in _item:
                    if _class_uri in _item[_subject].get("rdf_type"):
                        _prop.query_data = _item[_subject].get(_prop_uri)
                        _prop.subject_uri = _subject
                        for _processor in _prop.kds_processors:
                            run_processor(_processor, self, _prop, "load")
                    if _prop.processed_data is not None:
                        #print(_prop_uri, " __ ", _prop.query_data, "--pro--", _prop.processed_data)
                        _prop.old_data = _prop.processed_data
                        _prop.processed_data = None
                    else:
                        _prop.old_data = _prop.query_data
                        #print(_prop_uri, " __ ", _prop.query_data, "--old--", _prop.old_data)
                    if _prop.data is None and _prop.old_data is not None:
                        _prop.data = _prop.old_data
コード例 #11
0
def salt_processor(processor, obj, prop, mode="save", **kwargs):
    '''Generates a random string for salting'''
    if mode == "load":
        return obj.get("dataValue")
    length = 32
    obj['prop']['calcValue'] = True
    # if called from the password processor the kwargs will have a
    # salt_property and we can automatically generate a new one
    if kwargs.get('salt_property'):
        obj['processedData'][kwargs['salt_property']] = \
                        b64encode(os.urandom(length)).decode('utf-8')
        return obj
    # if the salt already exists in the processed data return the obj
    # the processor may have been called by the password processor
    if is_not_null(obj['processedData'].get(obj['propUri'])):
        return obj

    # find the password property
    _class_uri = obj['prop'].get("classUri")
    _class_properties = getattr(get_framework(), _class_uri).kds_properties
    password_property = None
    for _class_prop in _class_properties.values():
        if _class_prop.get("kds_propertyProcessing",{}).get("rdf_type") \
                == "kds_PasswordProcessor":
            password_property = obj['preSaveData'].get(\
                                            _class_prop.get("kds_propUri"))
    
    # check if there is a new password in the preSaveData
    #                         or
    # if the salt property is required and the old salt is empty
    if password_property is not None:
        if is_not_null(password_property.get('new')) or \
                                    (obj['prop'].get('required') and \
                                            not is_not_null(obj['prop']['old'])):
            obj['processedData'][obj['propUri']] = \
                        b64encode(os.urandom(length)).decode('utf-8')
    elif not is_not_null(obj['prop']['old']):
        obj['processedData'][obj['propUri']] = \
                        b64encode(os.urandom(length)).decode('utf-8')
    obj['prop']['calcValue'] = True
    return obj
コード例 #12
0
 def _validate_required_properties(self, rdf_obj, old_data):
     '''Validates whether all required properties have been supplied and
         contain data '''
     debug = False
     _return_error = []
     #create sets for evaluating requiredFields
     _required = self.list_required()
     if debug: print("Required Props: ", _required)
     _data_props = set()
     _deleted_props = set()
     for prop in rdf_obj:
         #remove empty data properties from consideration
         if debug: print(prop,"\n")
         if is_not_null(prop.data) or prop.data != 'None':
             _data_props.add(prop.kds_propUri)
         else:
             _deleted_props.add(prop.kds_propUri)
     # find the properties that already exist in the saved class data
     _old_class_data = self._select_class_query_data(old_data)
     for _prop in _old_class_data:
         # remove empty data properties from consideration
         if is_not_null(_old_class_data[_prop]) or _old_class_data[_prop]\
                 != 'None':
             _data_props.add(_prop)
     # remove the _deleted_props from consideration and add calculated props
     _valid_props = (_data_props - _deleted_props).union( \
             self._get_calculated_properties())
     #Test to see if all the required properties are supplied
     missing_required_properties = _required - _valid_props
     if len(missing_required_properties) > 0:
         _return_error.append({
             "errorType":"missing_required_properties",
             "errorData":{
                 "class":self.kds_classUri,
                 "properties":make_list(missing_required_properties)}})
     if len(_return_error) > 0:
         _return_val = _return_error
     else:
         _return_val = ["valid"]
     return _return_val
コード例 #13
0
def calculation_processor(processor, obj, prop, mode="save", return_type="prop"):
    ''' Application should proccess the property according to the rules listed
        in the kds:calulation property.'''
    if DEBUG:
        debug = True
    else:
        debug = False
    if debug: print("START calculation_processor ------------------------\n")
    if mode == "save":
        calculation = processor.get('kds_calculation')
        if calculation:
            if calculation.startswith("slugify"):
                _prop_uri = calculation[calculation.find("(")+1:\
                                                        calculation.find(")")]
                _prop_uri = pyuri(_prop_uri)
                _value_to_slug = obj['processedData'].get(_prop_uri, \
                                        obj['preSaveData'].get(_prop_uri, {}\
                                            ).get('new', None))
                if is_not_null(_value_to_slug):
                    obj['processedData'][obj['propUri']] = \
                            slugify(_value_to_slug)
                    obj['prop']['calcValue'] = True
            else:
                pass
    elif mode == "load":
        if debug: print("prop.kds_propUri: ", prop.kds_propUri)
        _calc_type = processor.get('kds_calculationType')
        if debug: print("prop.kds_propUri: ", prop.kds_propUri, " calc_type: ",
                    _calc_type)
        if _calc_type == "kdr_Concat":
            return_val = calculator_concat(processor, obj, prop, mode, return_type)
        elif _calc_type == "kdr_ObjectGenerator":
            return_val = calculator_object_generator(processor, obj, prop, mode, return_type)
        elif _calc_type == "kdr_DateConvertor":
            return_val = calculator_date_convertor(processor, obj, prop, mode, return_type)
        elif _calc_type == "kdr_UriTruncation":
            return_val = calculator_uir_truncation(processor, obj, prop, mode, return_type)
        if return_type == "value":
            return return_val
    if debug: print("END calculation_processor --------------------------\n\n")
    return obj
コード例 #14
0
def csv_to_multi_prop_processor(processor, obj, prop=None, mode="save"):
    ''' Application takes a CSV string and adds each value as a separate triple
        to the class instance.'''
    if mode == "save":
        _value_string = obj['prop']['new']
        if is_not_null(_value_string):
            vals = list(make_set(make_list(_value_string.split(', '))))
            obj['processedData'][obj['propUri']] = vals
        obj['prop']['calcValue'] = True
        return obj
    elif mode == "load":
        prop_val = calculate_value("<<%s|%s>>" % \
                               (prop.kds_propUri, prop.kds_classUri),
                               obj,
                               prop)
        if prop_val is not None:
            prop.processed_data = ", ".join(make_list(prop_val))
            return ", ".join(make_list(prop_val))
        else:
            return ""
    return obj
コード例 #15
0
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
コード例 #16
0
    def validate_primary_key(self, rdf_obj, old_data):
        '''query to see if PrimaryKey is Valid'''
        if not DEBUG:
            debug = False
        else:
            debug = False
        if debug: print("START RdfClass.validate_primary_key --------------\n")
        if debug: print("old_data:\n",json.dumps(old_data,indent=4)) 
        if old_data is None:
            old_data = {}
        _prop_name_list = []
        if hasattr(self, "kds_primaryKey"):
            pkey = self.kds_primaryKey
            if isinstance(pkey, dict):
                pkey = pkey.get("kds_keyCombo",[])
            pkey = make_list(pkey)
        else:
            pkey = []
        if debug: print(self.kds_classUri, " PrimaryKeys: ", pkey, "\n")
        if len(pkey) < 1:
            if debug: print("END RdfClass.validate_primary_key -NO pKey----\n")
            return ["valid"]
        else:
            _calculated_props = self._get_calculated_properties()
            _old_class_data = self._select_class_query_data(old_data)
            _new_class_data = {}
            _query_args = [make_triple("?uri", "a", \
                    iri(uri(self.kds_classUri)))]
            _multi_key_query_args = [make_triple("?uri", 
                                                 "a",
                                                 iri(uri(self.kds_classUri)))]
            _key_changed = False
            _prop_uri_list = []
            _key_props = []
            # get primary key data from the form data
            for prop in rdf_obj:
                if prop.kds_propUri in pkey:
                    _new_class_data[prop.kds_propUri] = prop.data
                    _prop_name_list.append(prop.kds_formLabelName)
                    _key_props.append(prop)
        
            for key in pkey:
                _object_val = None
                #get the _data_value to test against
                _data_value = _new_class_data.get(key, _old_class_data.get(key))
                if is_not_null(_data_value):
                    _range_obj = make_list(self.kds_properties[key].get(\
                            "rdfs_range", [{}]))[0]
                    _data_type = _range_obj.get('storageType')
                    _range = _range_obj.get('rangeClass')
                    if debug: print("_data_type: ", _data_type)
                    if _data_type == 'literal':
                        _object_val = RdfDataType(_range).sparql(_data_value)
                    else:
                        _object_val = iri(uri(_data_value))
                else:
                    # if data is missing from the key fields exit method and 
                    # return valid. *** The object value does not exist and 
                    #                   will be generated when another class 
                    #                   is saved
                    if debug: print(\
                        "END RdfClass.validate_primary_key - NO data-------\n")
                    return ["valid"]
                # if the old_data is not equel to the newData re-evaluate
                # the primaryKey
                
                # if the old value is not equal to the new value need to test 
                # the key
                # if the new value is to be calculated, i.e. a dependant class
                # generating a value then we don't need to test the key.
                # however if there is a supplied value and it is listed as a 
                # calculated property we need to test. 
                if (_old_class_data.get(key) != _new_class_data.get(key)) and \
                        ((key not in _calculated_props) or \
                        _new_class_data.get(key) is not None):
                    _key_changed = True
                    if _object_val:
                        _query_args.append(make_triple("?uri", iri(uri(key)), \
                                _object_val))
                        _multi_key_query_args.append(make_triple("?uri", \
                                iri(uri(key)), _object_val))
                else:
                    if _object_val:
                        _multi_key_query_args.append(make_triple("?uri", \
                                iri(uri(key)), _object_val))
                    else:
                        _key_changed = False
            # if the primary key changed in the form we need to
            # query to see if there is a violation with the new value

            if _key_changed:
                if len(pkey) > 1:
                    args = _multi_key_query_args
                else:
                    args = _query_args
                sparql = '''
                         {}\nSELECT DISTINCT (COUNT(?uri)>0 AS ?keyViolation)
                         {{\n{}\n}}\nGROUP BY ?uri'''.format(\
                                rdfw().get_prefix(),
                                "\n".join(args)) 
                if debug: print("----------- PrimaryKey query:\n", sparql)
                _key_test_results =\
                        requests.post(\
                                self.triplestore_url,
                                data={"query": sparql, "format": "json"})
                if debug: print("_key_test_results: ", _key_test_results.json())
                _key_test = _key_test_results.json().get('results').get( \
                        'bindings', [])
                if debug: print(_key_test)
                if len(_key_test) > 0:
                    _key_test = _key_test[0].get('keyViolation', {}).get( \
                            'value', False)
                else:
                    _key_test = False

                
                if not _key_test:
                    if debug: print(\
                        "END RdfClass.validate_primary_key - Key Passed --\n")
                    return ["valid"]
                else:
                    error_msg = "This {} aleady exists.".format(
                                        " / ".join(_prop_name_list))
                    for prop in _key_props:
                        if hasattr(prop, "errors"):
                            if isinstance(prop.errors, list):
                                prop.errors.append(error_msg)
                            else:
                                prop.errors = [error_msg]
                        else:
                            setattr(prop, "errors", [error_msg])
                    return [{"errorType":"primaryKeyViolation",
                             "formErrorMessage": error_msg,
                             "errorData":{"class": self.kds_classUri,
                                          "propUri": pkey}}]
            if debug: print(\
                "START RdfClass.validate_primary_key - Skipped Everything--\n")
            return ["valid"]
コード例 #17
0
    def set_obj_data(self, **kwargs):
        ''' sets the data for the current form paramters

        **keyword arguments
        subject_uri: the URI for the subject
        class_uri: the rdf class of the subject
        '''
        if not DEBUG:
            debug = False
        else:
            debug = True
        if debug: print("START Form.set_obj_data rdfforms.py --------------\n")
        _class_uri = kwargs.get("class_uri", self.data_class_uri)
        _lookup_class_uri = _class_uri
        subject_uri = kwargs.get("subject_uri", self.data_subject_uri)
        if not is_not_null(subject_uri) and not kwargs.get('query_data'):
            self.query_data = {}
            return None
        _subform_data = {}
        _parent_field = None
        # test to see if a sub_obj is part of the form.
        '''if self.has_subobj:
            for _field in self.rdf_field_list:
                if _field.type == 'FieldList':
                    for _entry in _field.entries:
                        if _entry.type == 'FormField':
                            _sub_rdf_obj = _entry.form
                            _parent_field = _field
            # if the sub_obj get its data
            if _sub_rdf_obj:
                _subform_data = _sub_rdf_obj.query_data'''
        # send the form to the query generator and get the query data back
        if kwargs.get('query_data') is None:
            return None
            '''self.query_data = convert_obj_to_rdf_namespace(\
                    convert_spo_to_dict(get_data(self, **kwargs)))'''
        else:
            self.query_data = kwargs.get('query_data')
        _data_value = None
        # cycle through the query data and add the data to the fields
        for _item in make_list(self.query_data):
            for _prop in self.rdf_field_list:
                _prop_uri = _prop.kds_propUri
                _class_uri = iri(uri(_prop.kds_classUri))
                for _subject in _item:
                    if _class_uri in _item[_subject].get("rdf_type"):
                        _prop.query_data = _item[_subject].get(_prop_uri)
                        _prop.subject_uri = _subject
                        for _processor in _prop.kds_processors:
                            run_processor(_processor, self, _prop, "load")
                    if _prop.processed_data is not None:
                        #print(_prop_uri, " __ ", _prop.query_data, "--pro--", _prop.processed_data)
                        _prop.old_data = _prop.processed_data
                        _prop.processed_data = None
                    else:
                        _prop.old_data = _prop.query_data
                        #print(_prop_uri, " __ ", _prop.query_data, "--old--", _prop.old_data)
                    if _prop.data is None and _prop.old_data is not None:
                        _prop.data = _prop.old_data
                #pp.pprint(_prop.__dict__)
        if debug: print("END Form.set_obj_data rdfforms.py --------------\n")
コード例 #18
0
def password_processor(processor, obj, prop, mode="save"):
    """Function handles application password actions

    Returns:
        modified passed in obj
    """
    if DEBUG:
        debug = True
    else:
        debug = False
    if debug: print("START password_processor --------------------------\n")
    salt_url = "kdr_SaltProcessor"
    if mode == "save":
        # find the salt property
        
        _class_uri = obj['prop'].get("classUri")
        _class_properties = getattr(get_framework(), _class_uri).kds_properties
        salt_property = None
        # find the property Uri that stores the salt value
        for _class_prop in _class_properties.values():
            _processors = clean_processors([make_list(\
                    _class_prop.get("kds_propertyProcessing",{}))])
            for _processor in _processors.values():
                if _processor.get("rdf_type") == salt_url:
                    salt_property = _class_prop.get("kds_propUri")
                    salt_processor_dict = _processor
        # if in save mode create a hashed password
        if mode == "save":
            # if the there is not a new password in the data return the obj
            if is_not_null(obj['prop']['new']) or obj['prop']['new'] != 'None':
                # if a salt has not been created call the salt processor
                if not obj['processedData'].get(salt_property):
                    obj = salt_processor(salt_processor_dict,
                                         obj, 
                                         mode,
                                         salt_property=salt_property)
                # create the hash
                salt = obj['processedData'].get(salt_property)
                _hash_value = sha256_crypt.encrypt(obj['prop']['new']+salt)
                # assign the hashed password to the processedData
                obj['processedData'][obj['propUri']] = _hash_value
                obj['prop']['calcValue'] = True
                if debug: print("END password_processor  mode = save-------\n")
            return obj
    elif mode == "verify":
        # verify the supplied password matches the saved password
        if not len(obj.query_data) > 0:
            setattr(prop, "password_verified", False)
            return obj    
        _class_uri = prop.kds_classUri
        _class_properties = getattr(get_framework(), _class_uri).kds_properties
        salt_property = None
        # find the property Uri that stores the salt value
        for _class_prop in _class_properties.values():
            _processors = clean_processors([make_list(\
                    _class_prop.get("kds_propertyProcessing",{}))])
            for _processor in _processors.values():
                if _processor.get("rdf_type") == salt_url:
                    salt_property = _class_prop.get("kds_propUri")
                    salt_processor_dict = _processor
        # find the salt value in the query_data
        salt_value = None
        for subject, props in obj.query_data.items():
            if clean_iri(props.get("rdf_type")) == _class_uri:
                salt_value = props.get(salt_property)
                hashed_password = props.get(prop.kds_propUri)
                break
        if debug: print(salt_value, " - ", hashed_password, " - ", prop.data)
        setattr(prop, "password_verified", \
            sha256_crypt.verify(prop.data + salt_value, hashed_password)) 
        if debug: print("END password_processor  mode = verify -------\n")
        return obj
    if mode == "load":
        if debug: print("END password_processor  mode = load -------\n")
        return obj
    return obj
コード例 #19
0
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
コード例 #20
0
    def validate_primary_key(self, rdf_obj, old_data):
        '''query to see if PrimaryKey is Valid'''
        if not DEBUG:
            debug = False
        else:
            debug = False
        if debug: print("START RdfClass.validate_primary_key --------------\n")
        if debug: print("old_data:\n", json.dumps(old_data, indent=4))
        if old_data is None:
            old_data = {}
        _prop_name_list = []
        if hasattr(self, "kds_primaryKey"):
            pkey = self.kds_primaryKey
            if isinstance(pkey, dict):
                pkey = pkey.get("kds_keyCombo", [])
            pkey = make_list(pkey)
        else:
            pkey = []
        if debug: print(self.kds_classUri, " PrimaryKeys: ", pkey, "\n")
        if len(pkey) < 1:
            if debug: print("END RdfClass.validate_primary_key -NO pKey----\n")
            return ["valid"]
        else:
            _calculated_props = self._get_calculated_properties()
            _old_class_data = self._select_class_query_data(old_data)
            _new_class_data = {}
            _query_args = [make_triple("?uri", "a", \
                    iri(uri(self.kds_classUri)))]
            _multi_key_query_args = [
                make_triple("?uri", "a", iri(uri(self.kds_classUri)))
            ]
            _key_changed = False
            _prop_uri_list = []
            _key_props = []
            # get primary key data from the form data
            for prop in rdf_obj:
                if prop.kds_propUri in pkey:
                    _new_class_data[prop.kds_propUri] = prop.data
                    _prop_name_list.append(prop.kds_formLabelName)
                    _key_props.append(prop)

            for key in pkey:
                _object_val = None
                #get the _data_value to test against
                _data_value = _new_class_data.get(key,
                                                  _old_class_data.get(key))
                if is_not_null(_data_value):
                    _range_obj = make_list(self.kds_properties[key].get(\
                            "rdfs_range", [{}]))[0]
                    _data_type = _range_obj.get('storageType')
                    _range = _range_obj.get('rangeClass')
                    if debug: print("_data_type: ", _data_type)
                    if _data_type == 'literal':
                        _object_val = RdfDataType(_range).sparql(_data_value)
                    else:
                        _object_val = iri(uri(_data_value))
                else:
                    # if data is missing from the key fields exit method and
                    # return valid. *** The object value does not exist and
                    #                   will be generated when another class
                    #                   is saved
                    if debug:                        print(\
                  "END RdfClass.validate_primary_key - NO data-------\n")
                    return ["valid"]
                # if the old_data is not equel to the newData re-evaluate
                # the primaryKey

                # if the old value is not equal to the new value need to test
                # the key
                # if the new value is to be calculated, i.e. a dependant class
                # generating a value then we don't need to test the key.
                # however if there is a supplied value and it is listed as a
                # calculated property we need to test.
                if (_old_class_data.get(key) != _new_class_data.get(key)) and \
                        ((key not in _calculated_props) or \
                        _new_class_data.get(key) is not None):
                    _key_changed = True
                    if _object_val:
                        _query_args.append(make_triple("?uri", iri(uri(key)), \
                                _object_val))
                        _multi_key_query_args.append(make_triple("?uri", \
                                iri(uri(key)), _object_val))
                else:
                    if _object_val:
                        _multi_key_query_args.append(make_triple("?uri", \
                                iri(uri(key)), _object_val))
                    else:
                        _key_changed = False
            # if the primary key changed in the form we need to
            # query to see if there is a violation with the new value

            if _key_changed:
                if len(pkey) > 1:
                    args = _multi_key_query_args
                else:
                    args = _query_args
                sparql = '''
                         {}\nSELECT DISTINCT (COUNT(?uri)>0 AS ?keyViolation)
                         {{\n{}\n}}\nGROUP BY ?uri'''.format(\
                                rdfw().get_prefix(),
                                "\n".join(args))
                if debug: print("----------- PrimaryKey query:\n", sparql)
                _key_test_results =\
                        requests.post(\
                                self.triplestore_url,
                                data={"query": sparql, "format": "json"})
                if debug:
                    print("_key_test_results: ", _key_test_results.json())
                _key_test = _key_test_results.json().get('results').get( \
                        'bindings', [])
                if debug: print(_key_test)
                if len(_key_test) > 0:
                    _key_test = _key_test[0].get('keyViolation', {}).get( \
                            'value', False)
                else:
                    _key_test = False

                if not _key_test:
                    if debug:                        print(\
                  "END RdfClass.validate_primary_key - Key Passed --\n")
                    return ["valid"]
                else:
                    error_msg = "This {} aleady exists.".format(
                        " / ".join(_prop_name_list))
                    for prop in _key_props:
                        if hasattr(prop, "errors"):
                            if isinstance(prop.errors, list):
                                prop.errors.append(error_msg)
                            else:
                                prop.errors = [error_msg]
                        else:
                            setattr(prop, "errors", [error_msg])
                    return [{
                        "errorType": "primaryKeyViolation",
                        "formErrorMessage": error_msg,
                        "errorData": {
                            "class": self.kds_classUri,
                            "propUri": pkey
                        }
                    }]
            if debug:                print(\
          "START RdfClass.validate_primary_key - Skipped Everything--\n")
            return ["valid"]