Exemplo n.º 1
0
    def _select_class_query_data(self, old_data):
        ''' Find the data in query data that pertains to this class instance
            returns dictionary of data with the subject_uri stored as
            !!!!subject'''

        #print("__________ class queryData:\n", \
        #                        json.dumps(dumpable_obj(old_data), indent=4))
        _old_class_data = {}
        if old_data:
            # find the current class data from the query
            if isinstance(old_data, list):
                for entry in old_data:
                    for _subject_uri, value in entry.items():
                        _class_types = make_list(value.get("rdf_type", []))
                        for _rdf_type in _class_types:
                            if _rdf_type == self.kds_classUri or \
                                    _rdf_type == "<%s>" % self.kds_classUri:
                                _old_class_data = value
                                _old_class_data[
                                    "!!!!subjectUri"] = _subject_uri
                                break
            else:
                for _subject_uri in old_data:
                    _class_types = make_list(old_data[_subject_uri].get( \
                        "rdf_type", []))
                    for _rdf_type in _class_types:
                        if _rdf_type == self.kds_classUri or \
                                    _rdf_type == "<%s>" % self.kds_classUri:
                            _old_class_data = old_data[_subject_uri]
                            _old_class_data["!!!!subjectUri"] = _subject_uri
                        break

        return _old_class_data
Exemplo n.º 2
0
    def _select_class_query_data(self, old_data):
        ''' Find the data in query data that pertains to this class instance
            returns dictionary of data with the subject_uri stored as
            !!!!subject'''

        #print("__________ class queryData:\n", \
        #                        json.dumps(dumpable_obj(old_data), indent=4))
        _old_class_data = {}
        if old_data:
            # find the current class data from the query
            if isinstance(old_data, list):
                for entry in old_data:
                    for _subject_uri, value in entry.items():
                        _class_types = make_list(value.get("rdf_type", []))
                        for _rdf_type in _class_types: 
                            if _rdf_type == self.kds_classUri or \
                                    _rdf_type == "<%s>" % self.kds_classUri:
                                _old_class_data = value
                                _old_class_data["!!!!subjectUri"] = _subject_uri
                                break
            else:    
                for _subject_uri in old_data:
                    _class_types = make_list(old_data[_subject_uri].get( \
                        "rdf_type", []))
                    for _rdf_type in _class_types:
                        if _rdf_type == self.kds_classUri or \
                                    _rdf_type == "<%s>" % self.kds_classUri:
                            _old_class_data = old_data[_subject_uri]
                            _old_class_data["!!!!subjectUri"] = _subject_uri
                        break

        return _old_class_data
Exemplo n.º 3
0
 def list_dependant(self):
     '''Returns a set of properties that are dependent upon the
     creation of another object'''
     _dependent_list = set()
     for _prop in self.kds_properties:
         _range_list = make_list(
             self.kds_properties[_prop].get('rdfs_range'))
         for _row in _range_list:
             if _row.get('storageType') == "object" or \
                     _row.get('storageType') == "blanknode":
                 _dependent_list.add(_prop)
     _return_obj = []
     for _dep in _dependent_list:
         _range_list = make_list(
             self.kds_properties[_dep].get('rdfs_range'))
         for _row in _range_list:
             if _row.get('storageType') == "object" or \
                _row.get('storageType') == "blanknode":
                 _return_obj.append({
                     "kds_propUri":
                     self.kds_properties[_dep].get("kds_propUri"),
                     "kds_classUri":
                     _row.get("rangeClass")
                 })
     return _return_obj
Exemplo n.º 4
0
    def get_vocab(self, vocab_name, **kwargs):
        """ Returns data stream of an rdf vocabulary

        args:
            vocab_name: the name or uri of the vocab to return
        """
        vocab_dict = self.__get_vocab_dict__(vocab_name, **kwargs)

        filepaths = list(
            set([
                os.path.join(self.cache_dir, vocab_dict['filename']),
                os.path.join(self.vocab_dir, vocab_dict['filename'])
            ]))
        for path in filepaths:
            if os.path.exists(path):
                with open(path, 'rb') as f_obj:
                    vocab_dict.update({
                        "name": vocab_name,
                        "data": f_obj.read(),
                        "modified": os.path.getmtime(path)
                    })
                return vocab_dict
        download_locs = make_list(vocab_dict.get('download', []))
        for loc in download_locs:

            loc_web = urllib.request.urlopen(loc)
            # loc_file_date = date_parse(loc_web.info()['Last-Modified'])
            urllib.request.urlretrieve(loc, filepaths[0])
            with open(filepaths[0], 'rb') as f_obj:
                vocab_dict.update({
                    "name": vocab_name,
                    "data": f_obj.read(),
                    "modified": os.path.getmtime(filepaths[0])
                })
                return vocab_dict
Exemplo n.º 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)
Exemplo n.º 6
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)
Exemplo n.º 7
0
    def get_vocab(self, vocab_name, **kwargs):
        """ Returns data stream of an rdf vocabulary

        args:
            vocab_name: the name or uri of the vocab to return
        """
        vocab_dict = self.__get_vocab_dict__(vocab_name, **kwargs)

        filepaths = list(set([os.path.join(self.cache_dir,
                                           vocab_dict['filename']),
                              os.path.join(self.vocab_dir,
                                           vocab_dict['filename'])]))
        for path in filepaths:
            if os.path.exists(path):
                with open(path, 'rb') as f_obj:
                    vocab_dict.update({"name": vocab_name,
                                       "data": f_obj.read(),
                                       "modified": os.path.getmtime(path)})
                return vocab_dict
        download_locs = make_list(vocab_dict.get('download',[]))
        for loc in download_locs:

            loc_web = urllib.request.urlopen(loc)
            # loc_file_date = date_parse(loc_web.info()['Last-Modified'])
            urllib.request.urlretrieve(loc, filepaths[0])
            with open(filepaths[0], 'rb') as f_obj:
                vocab_dict.update({"name": vocab_name,
                                   "data": f_obj.read(),
                                   "modified": os.path.getmtime(filepaths[0])})
                return vocab_dict
def calculator_object_generator(processor, obj, prop, mode, return_type="prop"):   
    ''' returns and object of calculated values '''
    if DEBUG:
        debug = True
    else:
        debug = False
        
    object_list = make_list(processor.get("kds_calculationObject"))
    return_obj = {}
    counter = 0
    for _object in object_list:
        key = calculate_value(_object.get("kds_objectKey"), obj, prop)
        if _object.get("kds_calculationType") == "kdr_ValueHasher":
            value = calculator_value_hasher(_object.get("kds_objectValue"),
                                            obj,
                                            prop)
        else:
            value = calculate_value(_object.get("kds_objectValue"), obj, prop)
        return_obj[key] = value
        counter += 1
        if debug: print(counter, " ", key, "-", value, " | ", _object)
    if return_type == "prop":
        prop.processed_data = return_obj
    else:
        return return_obj
Exemplo n.º 9
0
    def format(self, **kwargs):

        uri_format = kwargs.get('uri_format', "pyuri")
        output = kwargs.get('output', "dict")
        pretty = kwargs.get('pretty', False)
        remove = make_list(kwargs.get('remove', None))
        compress = kwargs.get('compress', False)
        sort = kwargs.get('sort', False)
        base_uri = kwargs.get("base_uri", None)
        add_ids = kwargs.get("add_ids", False)
        base_only = kwargs.get("base_only", False)

        if compress:
            new_obj = copy.copy(self)
            for key, value in self.items():
                for skey, svalue in value.items():
                    if isinstance(svalue, list) and len(svalue) == 1:
                        try:
                            new_obj[key][skey] = svalue[0]
                        except KeyError:
                            new_obj[key] = {skey: svalue[0]}
            format_obj = new_obj
        else:
            format_obj = self
        if remove:
            remove = make_list(remove)

        conv_data = {
            key: value.conv_json(uri_format, add_ids)
            for key, value in format_obj.items()
            if value.subject.type not in remove
        }
        if base_only:
            try:
                conv_data = conv_data[self.base_uri]
            except KeyError:
                return "Base_uri undefined or not in dataset"

        if output.lower() == 'json':
            indent = None
            if pretty:
                indent = 4
            return json.dumps(conv_data, indent=indent, sort_keys=sort)
        elif output.lower() == 'dict':
            return conv_data
Exemplo n.º 10
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
Exemplo n.º 11
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
Exemplo n.º 12
0
    def format(self, **kwargs):

        uri_format = kwargs.get('uri_format', "pyuri")
        output = kwargs.get('output', "dict")
        pretty = kwargs.get('pretty', False)
        remove = make_list(kwargs.get('remove', None))
        compress = kwargs.get('compress', False)
        sort = kwargs.get('sort', False)
        base_uri = kwargs.get("base_uri",None)
        add_ids = kwargs.get("add_ids", False)
        base_only = kwargs.get("base_only", False)

        if compress:
            new_obj = copy.copy(self)
            for key, value in self.items():
                for skey, svalue in value.items():
                    if isinstance(svalue, list) and len(svalue) == 1:
                        try:
                            new_obj[key][skey] = svalue[0]
                        except KeyError:
                            new_obj[key] = {skey: svalue[0]}
            format_obj = new_obj
        else:
            format_obj = self
        if remove:
            remove = make_list(remove)

        conv_data = {key: value.conv_json(uri_format, add_ids)
                     for key, value in format_obj.items()
                     if value.subject.type not in remove}
        if base_only:
            try:
                conv_data = conv_data[self.base_uri]
            except KeyError:
                return "Base_uri undefined or not in dataset"

        if output.lower() == 'json':
            indent = None
            if pretty:
                indent = 4
            return json.dumps(conv_data, indent=indent, sort_keys=sort)
        elif output.lower() == 'dict':
            return conv_data
Exemplo n.º 13
0
def get_field_security_access(field, user_info, item_permissions=None):
    '''This function will return level security access allowed for the field'''
    if item_permissions is None:
        item_permissions = []
    #Check application wide access
    _app_security = user_info.get('kds_applicationSecurity', set())
    #Check class access
    _class_access_list = make_list(field.get('kds_classInfo', {"kds_classSecurity":[]}\
                ).get("kds_classSecurity", []))
    _class_access = set()
    if len(_class_access_list) > 0:
        for i in _class_access_list:
            if i['acl_agent'] in user_info['kds_userGroups']:
                _class_access.add(i.get('acl_mode'))

    #check property security
    _property_access_list = make_list(field.get('kds_propertySecurity', []))
    _property_access = set()
    if len(_property_access_list) > 0:
        for i in _property_access_list:
            if i['acl_agent'] in user_info['kds_userGroups']:
                _class_access.add(i.get('acl_mode'))

    #check item permissions
    _item_access_list = make_list(field.get('itemSecurity', []))
    _item_access = set()
    if len(_item_access_list) > 0:
        for i in _item_access_list:
            if i['agent'] in user_info['kds_userGroups']:
                _item_access.add(i.get('mode'))

    _main_access = _item_access.intersection(_property_access)
    if "SuperUser" in _app_security:
        return set('Read', 'Write')
    elif len(_main_access) > 0:
        return _main_access
    elif len(_class_access) > 0:
        return _class_access
    elif len(_app_security) > 0:
        return _app_security
    else:
        return set()
Exemplo n.º 14
0
def get_field_security_access(field, user_info, item_permissions=None):
    '''This function will return level security access allowed for the field'''
    if item_permissions is None:
        item_permissions = []
    #Check application wide access
    _app_security = user_info.get('kds_applicationSecurity', set())
    #Check class access
    _class_access_list = make_list(field.get('kds_classInfo', {"kds_classSecurity":[]}\
                ).get("kds_classSecurity", []))
    _class_access = set()
    if len(_class_access_list) > 0:
        for i in _class_access_list:
            if i['acl_agent'] in user_info['kds_userGroups']:
                _class_access.add(i.get('acl_mode'))

    #check property security
    _property_access_list = make_list(field.get('kds_propertySecurity', []))
    _property_access = set()
    if len(_property_access_list) > 0:
        for i in _property_access_list:
            if i['acl_agent'] in user_info['kds_userGroups']:
                _class_access.add(i.get('acl_mode'))

    #check item permissions
    _item_access_list = make_list(field.get('itemSecurity', []))
    _item_access = set()
    if len(_item_access_list) > 0:
        for i in _item_access_list:
            if i['agent'] in user_info['kds_userGroups']:
                _item_access.add(i.get('mode'))

    _main_access = _item_access.intersection(_property_access)
    if "SuperUser" in _app_security:
        return set('Read', 'Write')
    elif len(_main_access) > 0:
        return _main_access
    elif len(_class_access) > 0:
        return _class_access
    elif len(_app_security) > 0:
        return _app_security
    else:
        return set()
Exemplo n.º 15
0
 def list_dependant(self):
     '''Returns a set of properties that are dependent upon the
     creation of another object'''
     _dependent_list = set()
     for _prop in self.kds_properties:
         _range_list = make_list(self.kds_properties[_prop].get('rdfs_range'))
         for _row in _range_list:
             if _row.get('storageType') == "object" or \
                     _row.get('storageType') == "blanknode":
                 _dependent_list.add(_prop)
     _return_obj = []
     for _dep in _dependent_list:
         _range_list = make_list(self.kds_properties[_dep].get('rdfs_range'))
         for _row in _range_list:
             if _row.get('storageType') == "object" or \
                _row.get('storageType') == "blanknode":
                 _return_obj.append(
                     {"kds_propUri": self.kds_properties[_dep].get("kds_propUri"),
                      "kds_classUri": _row.get("rangeClass")})
     return _return_obj
Exemplo n.º 16
0
 def _find_type(self, class_uri, prop_uri):
     '''find the data type based on class_uri and prop_uri'''
     _rdf_class = getattr(rdfw(), class_uri)
     _range = make_list(_rdf_class.kds_properties.get(prop_uri).get(\
             "rdfs_range"))[0]
     _range.get("storageType")
     if _range.get("storageType") == "literal":
         _range = _range.get("rangeClass")
     else:
         _range = _range.get("storageType")
     return _range
Exemplo n.º 17
0
 def _find_type(self, class_uri, prop_uri):
     '''find the data type based on class_uri and prop_uri'''
     _rdf_class = getattr(rdfw(), class_uri)
     _range = make_list(_rdf_class.kds_properties.get(prop_uri).get(\
             "rdfs_range"))[0]
     _range.get("storageType")
     if _range.get("storageType") == "literal":
         _range = _range.get("rangeClass")
     else:
         _range = _range.get("storageType")
     return _range
Exemplo n.º 18
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
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
 def _salt_class_search(class_uri):
     _class_properties = getattr(get_framework(), class_uri).kds_properties
     _salt_property = None
     _salt_url = "kdr_SaltProcessor"
     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 = {"kds_classUri": class_uri, \
                             "kds_propUri": _class_prop.get("kds_propUri")}
                 salt_processor_dict = _processor    
     return _salt_property    
Exemplo n.º 21
0
 def add_props(self, prop_list):
     ''' adds a new property/field to the form in all the correct
         locations '''
     prop_list = make_list(prop_list)
     self.form_changed = True
     for _prop in prop_list:
         _current_class = _prop.kds_classUri
         setattr(self, _prop.name, _prop)
         self.rdf_field_list.append(_prop)
         if isinstance(self.class_grouping[_current_class], list):
             self.class_grouping[_current_class].append(_prop)
         else:
             self.class_grouping[_current_class] = [_prop]
     self._set_class_links()
Exemplo n.º 22
0
 def add_props(self, prop_list):
     ''' adds a new property/field to the form in all the correct
         locations '''
     prop_list = make_list(prop_list)
     self.form_changed = True
     for _prop in prop_list:
         _current_class = _prop.kds_classUri
         setattr(self, _prop.name, _prop)
         self.rdf_field_list.append(_prop)
         if isinstance(self.class_grouping[_current_class], list):
             self.class_grouping[_current_class].append(_prop)
         else:
             self.class_grouping[_current_class] = [_prop]    
     self._set_class_links()
def calculate_value(value, obj, prop):
    if DEBUG:
        debug = True
    else:
        debug = False
    if value.startswith("<<"):
        _lookup_value = value.replace("<<","").replace(">>","")
        if debug: print(value)
        if "|" in _lookup_value:
            value_array = _lookup_value.split("|")
            _lookup_value = pyuri(value_array[0])
            _class_uri = iri(pyuri(value_array[1]))
        else:
            _lookup_value = pyuri(_lookup_value)
            _class_uri = iri(prop.kds_classUri)
        _query_data = obj.query_data
        for _subject, _data in _query_data.items():
            if _class_uri in make_list(_data.get("rdf_type")):
                #if _class_uri == "<obi_Assertion>": x=y
                return cbool(_data.get(pyuri(_lookup_value)), False)
    elif value.startswith("!--"):
        return_val = value
        if value == "!--api_url":
            return_val = obj.api_url
        if value == "!--base_url":
            return_val = obj.base_url
        if value == "!--base_api_url":
            return_val = obj.base_api_url
        if value == "!--subjectUri":
            _query_data = obj.query_data
            _class_uri = iri(prop.kds_classUri)
            for _subject, _data in _query_data.items():
                if _class_uri in make_list(_data.get("rdf_type")):
                    return_val = _subject  
        return return_val
    else:
        return cbool(value, False)
def prop_to_array_processor(processor, obj, prop, mode):
    ''' This will take a property data and convert it to an array '''
    if DEBUG:
        debug = True
    else:
        debug = True
    if debug: print("START prop_to_array_processor - rdfprocessors.py-----\n")
    if debug: print(prop.kds_propUri)
    return_val = obj
    # get the data value
    value = calculate_value("<<%s|%s>>" % (prop.kds_propUri, prop.kds_classUri),
                            obj, prop)
    if mode == "save":
        return_val = obj
    elif mode == "load":
        if value is not None:
            prop.processed_data = make_list(value)
            return_val = make_list(value)
        else:
            prop.processed_data = []
            return_val = []
    if debug: print("return_val: ", return_val)
    if debug: print("\nEND prop_to_array_processor - rdfprocessors.py-----\n")
    return return_val
Exemplo n.º 25
0
def get_api_instructions_json(instructions, instance):
    ''' This function will read through the RDF defined info and proccess the
        json to retrun the correct instructions for the specified form
        instance.'''

    _rdf_app = rdfw().app
    #print("inst------", instructions)
# get form instance info
    _api_instance_info = {}
    _api_instance_type_list = make_list(instructions.get('kds_apiInstance', []))
    for _api_instance in _api_instance_type_list:
        if _api_instance.get('kds_apiInstanceType') == instance:
            _api_instance_info = _api_instance
    _new_instr = {}
    #print("------", _form_instance_info)
#Determine the api paramaters
    _new_instr['kds_apiTitle'] = _api_instance_info.get('kds_apiTitle', \
            instructions.get("kds_apiTitle", ""))
    _new_instr['kds_apiDescription'] = _api_instance_info.get('kds_apiDescription', \
            instructions.get("kds_apiDescription", ""))
    _new_instr['kds_apiMethod'] = _api_instance_info.get('kds_apiMethod', \
            instructions.get("kds_apiMethod", ""))
    _new_instr['kds_lookupClassUri'] = _api_instance_info.get('kds_lookupClassUri', \
            instructions.get("kds_lookupClassUri", ""))
    _new_instr['kds_lookupPropertyUri'] =\
            _api_instance_info.get('kds_lookupPropertyUri',\
                    instructions.get("kds_lookupPropertyUri", ""))
    _new_instr['kds_submitSuccessRedirect'] = \
            _api_instance_info.get('kds_submitSuccessRedirect',
                                    instructions.get(\
                                            "kds_submitSuccessRedirect", ""))
    _new_instr['kds_submitFailRedirect'] = \
            _api_instance_info.get('kds_submitFailRedirect',
                                    instructions.get("kds_submitFailRedirect", ""))
    _new_instr['kds_saveAction'] = \
            _api_instance_info.get('kds_saveAction',
                                    instructions.get("kds_saveAction", ""))
    _new_instr['kds_returnType'] = \
            _api_instance_info.get('kds_returnType',
                                    instructions.get("kds_returnType", ""))
    _new_instr['kds_mimeType'] = \
            _api_instance_info.get('kds_mimeType',
                                    instructions.get("kds_mimeType", ""))  
    _new_instr['kds_subjectUriTransform'] = \
            _api_instance_info.get('kds_subjectUriTransform',
                                    instructions.get("kds_subjectUriTransform", ""))                                                                                               
    return _new_instr
def calculator_concat(processor, obj, prop, mode="save", return_type="prop"):
    ''' Does a concatition based on the the provided args and kwargs '''
    if DEBUG:
        debug = True
    else:
        debug = True
    if debug: print("START calculator_concat ---------------------\n")
    if debug: print(prop.kds_propUri)
    
    _seperator = processor.get("kds_calculationSeparator",",")
    _calc_string = processor.get("kds_calculation")
    _concat_list = make_list(_calc_string.split(_seperator))
    prop_val = calculate_value("<<%s|%s>>" % \
                               (prop.kds_propUri, prop.kds_classUri),
                               obj,
                               prop)
    if prop_val is None:
        prop.processed_data = None
        return None
        
    if debug: print(get_attr(prop, "kds_apiFieldName", \
                get_attr(prop, "kds_formFieldName")),": ", prop_val) 
    for i, _item in enumerate(_concat_list):
        item_val = ""
        if "||" in _item:
            _sub_calc = _item.split("||")
            new_processor = {}
            for val in _sub_calc:
                val_parts = val.split("|")
                new_processor[pyuri(val_parts[0])] = pyuri(val_parts[1])
            if len(new_processor) > 0:
                item_val = calculation_processor(new_processor, 
                                                 obj, 
                                                 prop, 
                                                 mode,
                                                 return_type="value")
        else:
            item_val = calculate_value(_item, obj, prop)            
        _concat_list[i] = item_val
    
    if return_type == "prop":
        prop.processed_data = "".join(remove_null(_concat_list))
        #if prop.kds_apiFieldName == "image":
        #S    prop.processed_data = "adfadsf"
    else:
        return "".join(remove_null(_concat_list))
Exemplo n.º 27
0
def rdf_lookup_api(class_uri, prop_uri):
    if not DEBUG:
        debug = False
    else:
        debug = True
    if debug: print("START rdf_lookup_api ----------------------------\n")
    return abort(400)
    referer = request.environ.get("HTTP_REFERER")
    form_function_path = url_for("open_badge.rdf_class_forms",
                                 form_name="form_name",
                                 form_instance="form_instance")
    base_form_path = \
            form_function_path.replace("form_name/form_instance.html", "")
    form_path = referer[referer.find(base_form_path)+len(base_form_path):\
            ].replace('.html','')
    form_exists = rdfw().form_exists(form_path)
    if not form_exists:
        return abort(400)
    form_class = rdf_framework_form_factory(form_path)() 
    if debug: print("form_path: ",form_path)
    if debug: print("Source Form: ", referer)
    #if debug: print("form dict:\n", pp.pformat(form_class.__dict__))
    related_fields = []
    for fld in form_class.rdf_field_list:
        print(fld.__dict__,"\n")
        if fld.kds_classUri == class_uri:
            related_fields.append(fld)
        for _range in make_list(fld.rdfs_range):
            if _range.get("rangeClass") == class_uri:
                related_fields.append(fld)
        if fld.type == "FieldList":
            if fld.entries[0].type == "FormField":
                for _fld in fld.entries[0].rdf_field_list:
                    if class_uri == _fld.kds_classUri:
                        related_fields.append(_fld)
    for fld in related_fields:
        print("field: ",fld.name)
    subject_uri = request.args.get("id")
    data = request.args.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)
Exemplo n.º 28
0
def rdf_lookup_api(class_uri, prop_uri):
    if not DEBUG:
        debug = False
    else:
        debug = True
    if debug: print("START rdf_lookup_api ----------------------------\n")
    return abort(400)
    referer = request.environ.get("HTTP_REFERER")
    form_function_path = url_for("app.rdf_class_forms",
                                 form_name="form_name",
                                 form_instance="form_instance")
    base_form_path = \
            form_function_path.replace("form_name/form_instance.html", "")
    form_path = referer[referer.find(base_form_path)+len(base_form_path):\
            ].replace('.html','')
    form_exists = rdfw().form_exists(form_path)
    if not form_exists:
        return abort(400)
    form_class = rdf_framework_form_factory(form_path)()
    if debug: print("form_path: ", form_path)
    if debug: print("Source Form: ", referer)
    #if debug: print("form dict:\n", pp.pformat(form_class.__dict__))
    related_fields = []
    for fld in form_class.rdf_field_list:
        print(fld.__dict__, "\n")
        if fld.kds_classUri == class_uri:
            related_fields.append(fld)
        for _range in make_list(fld.rdfs_range):
            if _range.get("rangeClass") == class_uri:
                related_fields.append(fld)
        if fld.type == "FieldList":
            if fld.entries[0].type == "FormField":
                for _fld in fld.entries[0].rdf_field_list:
                    if class_uri == _fld.kds_classUri:
                        related_fields.append(_fld)
    for fld in related_fields:
        print("field: ", fld.name)
    subject_uri = request.args.get("id")
    data = request.args.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)
Exemplo n.º 29
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
def get_wtform_validators(field):
    ''' reads the list of validators for the field and returns the wtforms
        validator list'''
    _field_validators = []
    required = field.get('kds_required', False)
    if required:
        _field_validators.append(InputRequired())
    else:
        _field_validators.append(Optional())
    _validator_list = make_list(field.get('kds_validators', []))
    for _validator in _validator_list:
        _validator_type = _validator['rdf_type']
        if _validator_type == 'kdr_PasswordValidator':
            _field_validators.append(
                EqualTo(
                    field.get("kds_formFieldName", '') +'_confirm',
                    message='Passwords must match'))
        if _validator_type == 'kdr_EmailValidator':
            _field_validators.append(Email(message=\
                    'Enter a valid email address'))
        if _validator_type == 'kdr_UrlValidator':
            _field_validators.append(URL(message=\
                    'Enter a valid URL/web address'))
        if _validator_type == 'kdr_UniqueValueValidator':
            _field_validators.append(UniqueValue())
        if _validator_type == 'kdr_StringLengthValidator':
            _string_params = _validator.get('kds_parameters')
            _param_list = _string_params.split(',')
            _param_obj = {}
            for _param in _param_list:
                _new_param = _param.split('=')
                _param_obj[_new_param[0]] = _new_param[1]
            _field_min = int(_param_obj.get('min', 0))
            _field_max = int(_param_obj.get('max', 1028))
            _field_validators.append(Length(
                min=_field_min,
                max=_field_max,
                message="{} size must be between {} and {} characters".format(
                    field.get("formFieldName"),
                    _field_min,
                    _field_max)))
    return _field_validators
Exemplo n.º 31
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
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
Exemplo n.º 33
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"]
Exemplo n.º 34
0
def get_form_instructions_json(instructions, instance):
    ''' This function will read through the RDF defined info and proccess the
        json to retrun the correct instructions for the specified form
        instance.'''

    _rdf_app = rdfw().app
    #print("inst------", instructions)
    # get form instance info
    _form_instance_info = {}
    _form_instance_type_list = make_list(
        instructions.get('kds_formInstance', []))
    for _form_instance in _form_instance_type_list:
        if _form_instance.get('kds_formInstanceType') == instance:
            _form_instance_info = _form_instance
    _new_instr = {}
    #print("------", _form_instance_info)
    #Determine the form paramaters
    _new_instr['kds_formTitle'] = _form_instance_info.get('kds_formTitle', \
            instructions.get("kds_formTitle", ""))
    _new_instr['kds_formDescription'] = _form_instance_info.get('kds_formDescription', \
            instructions.get("kds_formDescription", ""))
    _new_instr['kds_form_Method'] = _form_instance_info.get('kds_form_Method', \
            instructions.get("kds_form_Method", ""))
    _new_instr['kds_form_enctype'] = _form_instance_info.get('kds_form_enctype', \
            instructions.get("kds_form_enctype", ""))
    _new_instr['kds_propertyAddOnCss'] = _form_instance_info.get('kds_propertyAddOnCss', \
            instructions.get("kds_propertyAddOnCss", ""))
    _new_instr['kds_lookupClassUri'] = _form_instance_info.get('kds_lookupClassUri', \
            instructions.get("kds_lookupClassUri", ""))
    _new_instr['kds_lookupPropertyUri'] =\
            _form_instance_info.get('kds_lookupPropertyUri',\
                    instructions.get("kds_lookupPropertyUri"))
    _new_instr['kds_submitSuccessRedirect'] = \
            _form_instance_info.get('kds_submitSuccessRedirect',
                                    instructions.get(\
                                            "kds_submitSuccessRedirect", ""))
    _new_instr['kds_submitFailRedirect'] = \
            _form_instance_info.get('kds_submitFailRedirect',
                                    instructions.get("kds_submitFailRedirect", ""))
    _new_instr['kds_saveAction'] = \
            _form_instance_info.get('kds_saveAction',
                                    instructions.get("kds_saveAction", ""))
    _new_instr["kds_lookupPropertyClass"] = \
            _form_instance_info.get('"kds_lookupPropertyClass"',
                                    instructions.get("kds_lookupPropertyClass"))
    _new_instr["kds_loginRequired"] = \
            _form_instance_info.get('"kds_loginRequired"',
                                    instructions.get("kds_loginRequired"))
    # Determine css classes
    #form row css
    css = _form_instance_info.get('kds_rowOverideCss', instructions.get(\
            'kds_rowOverideCss', None))
    if css is None:
        css = _rdf_app.get('kds_formDefault', {}).get('kds_rowCss', '')
        css = css.strip() + " " + _form_instance_info.get('kds_rowAddOnCss', \
                instructions.get('kds_rowAddOnCss', ''))
        css = css.strip()
        css.strip()
    _new_instr['kds_rowCss'] = css

    #form general css
    css = _form_instance_info.get('kds_formOverideCss', instructions.get(\
            'kds_formOverideCss', None))
    if css is None:
        css = _rdf_app.get('kds_formDefault', {}).get('kds_formCss', '')
        css = css.strip() + " " + _form_instance_info.get('kds_formAddOnCss', \
                instructions.get('kds_formAddOnCss', ''))
        css = css.strip()
        css.strip()
    _new_instr['kds_formCss'] = css

    return _new_instr
Exemplo n.º 35
0
 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
Exemplo n.º 36
0
    def make(self):
        """ reads through the definitions and generates an python class for each
        definition """
        log.setLevel(self.log_level)
        created = []
        self.set_class_dict()
        start = datetime.datetime.now()
        log.info(" # of classes to create: %s" % len(self.class_dict))
        log.debug(" creating classes that are not subclassed")

        for name, cls_defs in self.class_dict.items():
            # if name in ['bf_Organization', 'bf_Agent']:
            #     pdb.set_trace()
            if not self.class_dict[name].get('rdfs_subClassOf'):
                created.append(name)
                setattr(
                    MODULE.rdfclass,
                    name,
                    types.new_class(
                        name,
                        (RdfClassBase, ),
                        {  #'metaclass': RdfClassMeta,
                            'cls_defs': cls_defs
                        }))
        log.debug(" created %s classes in: %s", len(created),
                  (datetime.datetime.now() - start))
        for name in created:
            del self.class_dict[name]
        left = len(self.class_dict)
        classes = []
        while left > 0:
            new = []
            for name, cls_defs in self.class_dict.items():
                # if name in ['bf_Organization', 'bf_Agent']:
                # pdb.set_trace()
                parents = self.class_dict[name].get('rdfs_subClassOf')
                if not parents:
                    bases += (RdfClassBase, )
                else:
                    for parent in make_list(parents):
                        bases = tuple()
                        if parent in created or parent in classes:
                            if parent in classes:
                                bases += (RdfClassBase, )
                            else:
                                base = getattr(MODULE.rdfclass, parent)
                                bases += (base, ) + base.__bases__
                if len(bases) > 0:
                    created.append(name)
                    setattr(
                        MODULE.rdfclass,
                        name,
                        types.new_class(
                            name,
                            bases,
                            {  #'metaclass': RdfClassMeta,
                                'cls_defs': cls_defs
                            }))
            for name in created:
                try:
                    del self.class_dict[name]
                except KeyError:
                    pass
            if left == len(self.class_dict):
                # c_list = [self.class_dict[name].get('rdfs_subClassOf') \
                #           for name in self.class_dict]
                missing_parents = []
                for name in self.class_dict:
                    missing_parents += \
                            self.class_dict[name].get('rdfs_subClassOf', [])
                missing_parents = set(missing_parents)
                still_valid = set([
                    name for name in self.class_dict
                    if name not in missing_parents
                ])
                classes = list(missing_parents.difference(\
                            set(self.class_dict.keys())))
                # classess = []
                # for cl in c_list:
                #     for item in cl:
                #         classes.append(item)

                for name in self.class_dict:
                    if name in classes:
                        classes.remove(name)
                    for p_name in self.class_dict[name].get(
                            'rdfs_subClassOf', []).copy():
                        if p_name in classes:
                            self.class_dict[name]['rdfs_subClassOf'].remove(\
                                    p_name)
                # pdb.set_trace()
            left = len(self.class_dict)
        # self.tie_properties(created)
        log.info(" created all classes in %s",
                 (datetime.datetime.now() - start))
Exemplo n.º 37
0
def get_api_field_json(field,
                       instructions,
                       instance,
                       user_info,
                       item_permissions=None):
    '''This function will read through the RDF defined info and proccess the
	json to return the correct values for the instance, security and details'''
    if DEBUG:
        debug = True
    else:
        debug = False
    if item_permissions is None:
        item_permissions = []
    _rdf_app = rdfw().app
    instance = instance.replace(".html", "")
    # get class property info
    try:
        _class_prop = getattr(rdfw(), field.get(\
                'kds_classUri')).kds_properties.get(field.get('kds_propUri'),{})
    except:
        _class_prop = {}
    # merge the class prop attributes with the api prop

    #field = {**_class_prop, **field}
    temp_field = _class_prop.copy()
    temp_field.update(field)
    field = temp_field

    # Determine Security Access
    _new_field = {}

    _access_level = get_field_security_access(field, user_info,
                                              item_permissions)
    if "acl_Read" not in _access_level:
        return None
    _new_field['accessLevel'] = _access_level

    # get api instance info
    _api_instance_info = {}
    _api_field_instance_type_list = make_list(field.get('kds_apiInstance', field.get(\
            'kds_apiDefault', {}).get('kds_apiInstance', [])))
    if debug: print("instance type list: ", _api_field_instance_type_list)
    if debug: print("instance: ", instance)
    for _field_instance in _api_field_instance_type_list:
        if _field_instance.get('kds_apiInstanceType') == instance:
            _api_instance_info = _field_instance
    if debug: print("instance info\n", _api_instance_info)
    # Determine the field paramaters
    _new_field['kds_apiFieldName'] = _api_instance_info.get('kds_apiFieldName', field.get(\
            "kds_apiFieldName", field.get('kds_apiDefault', {}).get(\
            'kds_apiFieldName', "")))
    _new_field['kds_fieldType'] = _api_instance_info.get('kds_fieldType', field.get(\
            'kds_fieldType', field.get('kds_apiDefault', {}).get('kds_fieldType', "")))
    if not isinstance(_new_field['kds_fieldType'], dict):
        _new_field['kds_fieldType'] = {"rdf_type": _new_field['kds_fieldType']}

    _new_field['kds_apiLabelName'] = _api_instance_info.get('kds_apiLabelName', \
            field.get("kds_apiLabelName", field.get('kds_apiDefault', {}).get(\
            'kds_apiLabelName', "")))
    _new_field['kds_apiFieldHelp'] = _api_instance_info.get('kds_apiFieldHelp', \
            field.get("apiFieldHelp", field.get('apiDefault', {}).get(\
            'kds_apiFieldHelp', "")))
    _new_field['kds_apiFieldOrder'] = _api_instance_info.get('kds_apiFieldOrder', \
            field.get("kds_apiFieldOrder", field.get('kds_apiDefault', {}).get(\
            'kds_apiFieldOrder', "")))
    _new_field['kds_apiLayoutRow'] = _api_instance_info.get('kds_apiLayoutRow', \
            field.get("kds_apiLayoutRow", field.get('kds_apiDefault', {}).get(\
            'kds_apiLayoutRow', "")))

    _new_field['rdfs_range'] = field.get('rdfs_range')
    _new_field['kds_defaultVal'] = _api_instance_info.get('kds_defaultVal',\
            field.get('kds_defaultVal'))
    _new_field['kds_propUri'] = field.get('kds_propUri')
    _new_field['kds_classUri'] = field.get('kds_classUri')
    _new_field['kds_returnValue'] = field.get('kds_returnValue')
    # get applicationActionList
    _new_field['kds_actionList'] = make_set(_api_instance_info.get(\
            'kds_applicationAction', set()))
    _new_field['kds_actionList'].union(
        make_set(field.get('kds_applicationAction', set())))
    _new_field['kds_actionList'] = list(_new_field['kds_actionList'])
    if debug:
        print("action List:_______________", _new_field['kds_actionList'])
    if "kdr_RemoveFromApi" in\
            _new_field['kds_actionList']:
        return None
    # get valiator list
    if field.get('kds_overrideValidation'):
        _new_field['kds_validators'] = field.get('kds_overrideValidation')
    else:
        _new_field['kds_validators'] = make_list(\
                _api_instance_info.get('kds_apiValidation', []))
        _new_field['kds_validators'] += make_list(\
                field.get('kds_apiValidation', []))
        _new_field['kds_validators'] += make_list(\
                field.get('kds_propertyValidation', []))
    # get processing list
    _new_field['kds_processors'] = make_list(
        _api_instance_info.get('kds_apiProcessing', []))
    _new_field['kds_processors'] += make_list(
        field.get('kds_apiProcessing', []))
    _new_field['kds_processors'] += make_list(
        field.get('kds_propertyProcessing', []))

    if debug:
        if field['kds_propUri'] == "schema_image":
            x = 1
    # get required state
    _required = False
    _field_req_var = cbool(field.get('kds_requiredField'))
    if (field.get('kds_propUri') in make_list(field.get('kds_classInfo', {}).get(\
            'kds_primaryKey', []))) or _field_req_var:
        _required = True
    if field.get('kds_classUri') in make_list(
            field.get('kds_requiredByDomain', {})):
        _required = True
    if _field_req_var == False:
        _required = False
    _new_field['kds_required'] = _required

    # Determine EditState
    if ("acl_Write" in _access_level) and ("kdr_NotEditable" \
            not in _new_field['kds_actionList']):
        _new_field['editable'] = True
    else:
        _new_field['editable'] = False

    return _new_field
Exemplo n.º 38
0
    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 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
Exemplo n.º 40
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")
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
Exemplo n.º 42
0
def get_form_instructions_json(instructions, instance):
    ''' This function will read through the RDF defined info and proccess the
        json to retrun the correct instructions for the specified form
        instance.'''

    _rdf_app = rdfw().app
    #print("inst------", instructions)
# get form instance info
    _form_instance_info = {}
    _form_instance_type_list = make_list(instructions.get('kds_formInstance', []))
    for _form_instance in _form_instance_type_list:
        if _form_instance.get('kds_formInstanceType') == instance:
            _form_instance_info = _form_instance
    _new_instr = {}
    #print("------", _form_instance_info)
#Determine the form paramaters
    _new_instr['kds_formTitle'] = _form_instance_info.get('kds_formTitle', \
            instructions.get("kds_formTitle", ""))
    _new_instr['kds_formDescription'] = _form_instance_info.get('kds_formDescription', \
            instructions.get("kds_formDescription", ""))
    _new_instr['kds_form_Method'] = _form_instance_info.get('kds_form_Method', \
            instructions.get("kds_form_Method", ""))
    _new_instr['kds_form_enctype'] = _form_instance_info.get('kds_form_enctype', \
            instructions.get("kds_form_enctype", ""))
    _new_instr['kds_propertyAddOnCss'] = _form_instance_info.get('kds_propertyAddOnCss', \
            instructions.get("kds_propertyAddOnCss", ""))
    _new_instr['kds_lookupClassUri'] = _form_instance_info.get('kds_lookupClassUri', \
            instructions.get("kds_lookupClassUri", ""))
    _new_instr['kds_lookupPropertyUri'] =\
            _form_instance_info.get('kds_lookupPropertyUri',\
                    instructions.get("kds_lookupPropertyUri"))
    _new_instr['kds_submitSuccessRedirect'] = \
            _form_instance_info.get('kds_submitSuccessRedirect',
                                    instructions.get(\
                                            "kds_submitSuccessRedirect", ""))
    _new_instr['kds_submitFailRedirect'] = \
            _form_instance_info.get('kds_submitFailRedirect',
                                    instructions.get("kds_submitFailRedirect", ""))
    _new_instr['kds_saveAction'] = \
            _form_instance_info.get('kds_saveAction',
                                    instructions.get("kds_saveAction", ""))
    _new_instr["kds_lookupPropertyClass"] = \
            _form_instance_info.get('"kds_lookupPropertyClass"',
                                    instructions.get("kds_lookupPropertyClass"))                                
    _new_instr["kds_loginRequired"] = \
            _form_instance_info.get('"kds_loginRequired"',
                                    instructions.get("kds_loginRequired"))
# Determine css classes
    #form row css
    css = _form_instance_info.get('kds_rowOverideCss', instructions.get(\
            'kds_rowOverideCss', None))
    if css is None:
        css = _rdf_app.get('kds_formDefault', {}).get('kds_rowCss', '')
        css = css.strip() + " " + _form_instance_info.get('kds_rowAddOnCss', \
                instructions.get('kds_rowAddOnCss', ''))
        css = css.strip()
        css.strip()
    _new_instr['kds_rowCss'] = css

    #form general css
    css = _form_instance_info.get('kds_formOverideCss', instructions.get(\
            'kds_formOverideCss', None))
    if css is None:
        css = _rdf_app.get('kds_formDefault', {}).get('kds_formCss', '')
        css = css.strip() + " " + _form_instance_info.get('kds_formAddOnCss', \
                instructions.get('kds_formAddOnCss', ''))
        css = css.strip()
        css.strip()
    _new_instr['kds_formCss'] = css

    return _new_instr
Exemplo n.º 43
0
 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
Exemplo n.º 44
0
def get_field_json(field, instructions, instance, user_info, item_permissions=None):
    '''This function will read through the RDF defined info and proccess the
	json to return the correct values for the instance, security and details'''
    debug = False
    if item_permissions is None:
        item_permissions = []
    _rdf_app = rdfw().app
    instance = instance.replace(".html", "")
    # Determine Security Access
    _new_field = {}
    _access_level = get_field_security_access(field, user_info, item_permissions)
    if "acl_Read" not in _access_level:
        return None
    _new_field['accessLevel'] = _access_level

    # get form instance info
    _form_instance_info = {}
    _form_field_instance_type_list = make_list(field.get('kds_formInstance', field.get(\
            'kds_formDefault', {}).get('kds_formInstance', [])))
    #print("instance type list: ",_form_field_instance_type_list)
    #print("instance: ", instance)
    for _field_instance in _form_field_instance_type_list:
        if _field_instance.get('kds_formInstanceType') == instance:
            _form_instance_info = _field_instance
    #print("instance info\n",_form_instance_info)
    # Determine the field paramaters
    _new_field['kds_formFieldName'] = _form_instance_info.get('kds_formFieldName', field.get(\
            "kds_formFieldName", field.get('kds_formDefault', {}).get(\
            'kds_formFieldName', "")))
    _new_field['kds_fieldType'] = _form_instance_info.get('kds_fieldType', field.get(\
            'kds_fieldType', field.get('kds_formDefault', {}).get('kds_fieldType', "")))
    if not isinstance(_new_field['kds_fieldType'], dict):
        _new_field['kds_fieldType'] = {"rdf_type":_new_field['kds_fieldType']}

    _new_field['kds_formLabelName'] = _form_instance_info.get('kds_formLabelName', \
            field.get("kds_formLabelName", field.get('kds_formDefault', {}).get(\
            'kds_formLabelName', "")))
    _new_field['kds_formFieldHelp'] = _form_instance_info.get('kds_formFieldHelp', \
            field.get("formFieldHelp", field.get('formDefault', {}).get(\
            'kds_formFieldHelp', "")))
    _new_field['kds_formFieldOrder'] = _form_instance_info.get('kds_formFieldOrder', \
            field.get("kds_formFieldOrder", field.get('kds_formDefault', {}).get(\
            'kds_formFieldOrder', "")))
    _new_field['kds_formLayoutRow'] = _form_instance_info.get('kds_formLayoutRow', \
            field.get("kds_formLayoutRow", field.get('kds_formDefault', {}).get(\
            'kds_formLayoutRow', "")))
    _new_field['kds_propUri'] = field.get('kds_propUri')
    _new_field['kds_classUri'] = field.get('kds_classUri')
    _new_field['rdfs_range'] = field.get('rdfs_range')
    _new_field['kds_defaultVal'] = _form_instance_info.get('kds_defaultVal',\
            field.get('kds_defaultVal'))

    # get applicationActionList
    _new_field['kds_actionList'] = make_set(_form_instance_info.get(\
            'kds_applicationAction', set()))
    _new_field['kds_actionList'].union(make_set(field.get('kds_applicationAction', set())))
    _new_field['kds_actionList'] = list(_new_field['kds_actionList'])
    #print("action List:_______________", _new_field['kds_actionList'])
    if "kdr_RemoveFromForm" in\
            _new_field['kds_actionList']:
        return None
    # get valiator list
    if field.get('kds_overrideValidation'):
        _new_field['kds_validators'] = field.get('kds_overrideValidation')
    else:
        _new_field['kds_validators'] = make_list(\
                _form_instance_info.get('kds_formValidation', []))
        _new_field['kds_validators'] += make_list(\
                field.get('kds_formValidation', []))
        _new_field['kds_validators'] += make_list(\
                field.get('kds_propertyValidation', []))
    # get processing list
    _new_field['kds_processors'] = make_list(_form_instance_info.get('kds_formProcessing', []))
    _new_field['kds_processors'] += make_list(field.get('kds_formProcessing', []))
    _new_field['kds_processors'] += make_list(field.get('kds_propertyProcessing', []))
    if debug:
        if field['kds_propUri'] == "schema_image":
            x=1
    # get required state
    _required = False
    _field_req_var = cbool(field.get('kds_requiredField'))
    if (field.get('kds_propUri') in make_list(field.get('kds_classInfo', {}).get(\
            'kds_primaryKey', []))) or _field_req_var:
        _required = True
    if field.get('kds_classUri') in make_list(field.get('kds_requiredByDomain', {})):
        _required = True
    if _field_req_var == False:
        _required = False
    _new_field['kds_required'] = _required

    # Determine EditState
    if ("acl_Write" in _access_level) and ("kdr_NotEditable" \
            not in _new_field['kds_actionList']):
        _new_field['editable'] = True
    else:
        _new_field['editable'] = False

    # Determine css classes
    css = _form_instance_info.get('kds_overrideCss', field.get('kds_overrideCss', \
            instructions.get('kds_overrideCss', None)))
    if css is None:
        css = _rdf_app.get('kds_formDefault', {}).get('kds_fieldCss', '')
        css = css.strip() + " " + instructions.get('kds_propertyAddOnCss', '')
        css = css.strip() + " " + _form_instance_info.get('kds_addOnCss', field.get(\
                'kds_addOnCss', field.get('kds_formDefault', {}).get('kds_addOnCss', '')))
        css = css.strip()
    _new_field['kds_css'] = css
    #print("field_json:\n", json.dumps(_new_field, indent=4))

    return _new_field
Exemplo n.º 45
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"]
Exemplo n.º 46
0
def get_api_field_json(field, instructions, instance, user_info, item_permissions=None):
    '''This function will read through the RDF defined info and proccess the
	json to return the correct values for the instance, security and details'''
    if DEBUG:
        debug = True
    else:
        debug = False
    if item_permissions is None:
        item_permissions = []
    _rdf_app = rdfw().app
    instance = instance.replace(".html", "")
    # get class property info
    try:
        _class_prop = getattr(rdfw(), field.get(\
                'kds_classUri')).kds_properties.get(field.get('kds_propUri'),{})
    except:
        _class_prop = {}
    # merge the class prop attributes with the api prop

    #field = {**_class_prop, **field} 
    temp_field = _class_prop.copy()
    temp_field.update(field)
    field = temp_field

    # Determine Security Access
    _new_field = {}
    
    _access_level = get_field_security_access(field, user_info, item_permissions)
    if "acl_Read" not in _access_level:
        return None
    _new_field['accessLevel'] = _access_level
    
    # get api instance info
    _api_instance_info = {}
    _api_field_instance_type_list = make_list(field.get('kds_apiInstance', field.get(\
            'kds_apiDefault', {}).get('kds_apiInstance', [])))
    if debug: print("instance type list: ",_api_field_instance_type_list)
    if debug: print("instance: ", instance)
    for _field_instance in _api_field_instance_type_list:
        if _field_instance.get('kds_apiInstanceType') == instance:
            _api_instance_info = _field_instance
    if debug: print("instance info\n",_api_instance_info)
    # Determine the field paramaters
    _new_field['kds_apiFieldName'] = _api_instance_info.get('kds_apiFieldName', field.get(\
            "kds_apiFieldName", field.get('kds_apiDefault', {}).get(\
            'kds_apiFieldName', "")))
    _new_field['kds_fieldType'] = _api_instance_info.get('kds_fieldType', field.get(\
            'kds_fieldType', field.get('kds_apiDefault', {}).get('kds_fieldType', "")))
    if not isinstance(_new_field['kds_fieldType'], dict):
        _new_field['kds_fieldType'] = {"rdf_type":_new_field['kds_fieldType']}

    _new_field['kds_apiLabelName'] = _api_instance_info.get('kds_apiLabelName', \
            field.get("kds_apiLabelName", field.get('kds_apiDefault', {}).get(\
            'kds_apiLabelName', "")))
    _new_field['kds_apiFieldHelp'] = _api_instance_info.get('kds_apiFieldHelp', \
            field.get("apiFieldHelp", field.get('apiDefault', {}).get(\
            'kds_apiFieldHelp', "")))
    _new_field['kds_apiFieldOrder'] = _api_instance_info.get('kds_apiFieldOrder', \
            field.get("kds_apiFieldOrder", field.get('kds_apiDefault', {}).get(\
            'kds_apiFieldOrder', "")))
    _new_field['kds_apiLayoutRow'] = _api_instance_info.get('kds_apiLayoutRow', \
            field.get("kds_apiLayoutRow", field.get('kds_apiDefault', {}).get(\
            'kds_apiLayoutRow', "")))
    
    _new_field['rdfs_range'] = field.get('rdfs_range')
    _new_field['kds_defaultVal'] = _api_instance_info.get('kds_defaultVal',\
            field.get('kds_defaultVal'))
    _new_field['kds_propUri'] = field.get('kds_propUri')
    _new_field['kds_classUri'] = field.get('kds_classUri')
    _new_field['kds_returnValue'] = field.get('kds_returnValue')
    # get applicationActionList
    _new_field['kds_actionList'] = make_set(_api_instance_info.get(\
            'kds_applicationAction', set()))
    _new_field['kds_actionList'].union(make_set(field.get('kds_applicationAction', set())))
    _new_field['kds_actionList'] = list(_new_field['kds_actionList'])
    if debug: print("action List:_______________", _new_field['kds_actionList'])
    if "kdr_RemoveFromApi" in\
            _new_field['kds_actionList']:
        return None
    # get valiator list
    if field.get('kds_overrideValidation'):
        _new_field['kds_validators'] = field.get('kds_overrideValidation')
    else:
        _new_field['kds_validators'] = make_list(\
                _api_instance_info.get('kds_apiValidation', []))
        _new_field['kds_validators'] += make_list(\
                field.get('kds_apiValidation', []))
        _new_field['kds_validators'] += make_list(\
                field.get('kds_propertyValidation', []))
    # get processing list
    _new_field['kds_processors'] = make_list(_api_instance_info.get('kds_apiProcessing', []))
    _new_field['kds_processors'] += make_list(field.get('kds_apiProcessing', []))
    _new_field['kds_processors'] += make_list(field.get('kds_propertyProcessing', []))
    
    if debug:
        if field['kds_propUri'] == "schema_image":
            x=1
    # get required state
    _required = False
    _field_req_var = cbool(field.get('kds_requiredField'))
    if (field.get('kds_propUri') in make_list(field.get('kds_classInfo', {}).get(\
            'kds_primaryKey', []))) or _field_req_var:
        _required = True
    if field.get('kds_classUri') in make_list(field.get('kds_requiredByDomain', {})):
        _required = True
    if _field_req_var == False:
        _required = False
    _new_field['kds_required'] = _required

    # Determine EditState
    if ("acl_Write" in _access_level) and ("kdr_NotEditable" \
            not in _new_field['kds_actionList']):
        _new_field['editable'] = True
    else:
        _new_field['editable'] = False

    return _new_field
Exemplo n.º 47
0
def rdf_framework_api_factory(api_id, **kwargs):
    ''' Generates a form class based on the form definitions in the
        kds-app.ttl file

    keyword Args:
        class_uri: the classUri used for a form with loaded data
                   ***** has to be the class of the subject_uri for
                         the form data lookup
        subject_uri: the uri of the object that you want to lookup
        is_subform: True or False. States whether the form is a subform
                 of another form
    '''
    # find the api name and instance from the url
    _api_location = rdfw().api_exists(api_id)
    # exit factory if form does not exist
    if _api_location is False:
        return None
    _api_uri = _api_location['api_uri']
    _instance = _api_location['api_uri']

    rdf_api = type(_api_uri, (Api, ), {})
    setattr(rdf_api, "api_uri", _api_uri)
    _app_api = rdfw().rdf_api_dict.get(_api_uri, {})
    fields = make_list(_app_api.get('kds_properties'))
    instructions = get_api_instructions_json(\
            _app_api.get('kds_apiInstructions'), _instance)
    _lookup_class_uri = kwargs.get("classUri",\
                                   instructions.get("kds_lookupClassUri"))
    _lookup_prop_uri = kwargs.get("propUri", \
                                  instructions.get("kds_lookupPropUri"))
    _lookup_subject_uri = kwargs.get("subject_uri")
    kwargs['subject_uri'] = _lookup_subject_uri
    kwargs["propUri"] = _lookup_prop_uri
    kwargs["classUri"] = _lookup_class_uri
    # ************************** Testing Variable *************************
    user_info = {
        "kds_userGroups":["kdr_SysAdmin-SG"],
        'kds_applicationSecurity':["acl_Read", "acl_Write"]
    }
    # *********************************************************************
    _has_subobj = False
    rdf_field_list = []
    for fld in fields:
        #print(fld)
        field = get_api_field_json(fld, instructions, _instance, user_info)
        if field:
            field_item = get_api_field(field, _instance, **kwargs)
            api_field = field_item.get('fld')
            field = field_item.get('fld_json')
            if isinstance(api_field, list):
                i = 0
                for nfld in api_field:
                    #print(fld)
                    if nfld.get('kds_field'):
                        _new_field = dict.copy(field)
                        _new_field['kds_apiFieldName'] = nfld['kds_fieldName']
                        _new_field['kds_apiFieldOrder'] = \
                                float(_new_field['kds_apiFieldOrder']) + i
                        if fld.get("doNotSave"):
                            _new_field['doNotSave'] = True
                        else:
                            _new_field['doNotSave'] = False
                        augmented_field = add_field_attributes(\
                                nfld['kds_field'],_new_field)
                        rdf_field_list.append(_new_field)
                        setattr(rdf_api, nfld['kds_fieldName'], augmented_field)
                        i += .1
            else:
                #print(field['apiFieldName'], " - ", api_field)
                if api_field:
                    #print("set --- ", field)
                    field['doNotSave'] = False
                    rdf_field_list.append(field)
                    setattr(rdf_api, field['kds_apiFieldName'], api_field)

    if kwargs.get("is_subobj"):
        field = {'kds_apiFieldName':'subjectUri',
                 'kds_fieldType':{'rdf_type':'ReferenceField'},
                 'kds_apiLabelName':'Hidden dataSubjectUri',
                 'kds_classUri':_lookup_class_uri,
                 'kds_propUri':'subjectUri',
                 'kds_processors':[],
                 'editable': False,
                 'doNotSave': False}
        rdf_field_list.append(field)
        augmented_field = add_field_attributes(StringField('dataSubjectUri'),
                                               field)
        setattr(rdf_api, 'subjectUri', augmented_field)
        setattr(rdf_api, 'is_subobj', True)
    else:
        setattr(rdf_api, 'is_subobj', False)
    setattr(rdf_api, 'has_subobj', _has_subobj)
    setattr(rdf_api, 'rdf_field_list', rdf_field_list)
    setattr(rdf_api, "rdf_instructions", instructions)
    setattr(rdf_api, "instance_uri", _instance)
    setattr(rdf_api, "data_class_uri", _lookup_class_uri)
    setattr(rdf_api, "data_subject_uri", _lookup_subject_uri)
    setattr(rdf_api, "data_prop_uri", _lookup_prop_uri)
    setattr(rdf_api, "base_url", kwargs.get("base_url"))
    setattr(rdf_api, "current_url", kwargs.get("current_url"))
    setattr(rdf_api, "base_api_url", kwargs.get("base_api_url"))
    setattr(rdf_api, "api_url", kwargs.get("api_url"))
    #pp.pprint(rdf_api.__dict__)
    return rdf_api
Exemplo n.º 48
0
def get_field_json(field,
                   instructions,
                   instance,
                   user_info,
                   item_permissions=None):
    '''This function will read through the RDF defined info and proccess the
	json to return the correct values for the instance, security and details'''
    debug = False
    if item_permissions is None:
        item_permissions = []
    _rdf_app = rdfw().app
    instance = instance.replace(".html", "")
    # Determine Security Access
    _new_field = {}
    _access_level = get_field_security_access(field, user_info,
                                              item_permissions)
    if "acl_Read" not in _access_level:
        return None
    _new_field['accessLevel'] = _access_level

    # get form instance info
    _form_instance_info = {}
    _form_field_instance_type_list = make_list(field.get('kds_formInstance', field.get(\
            'kds_formDefault', {}).get('kds_formInstance', [])))
    #print("instance type list: ",_form_field_instance_type_list)
    #print("instance: ", instance)
    for _field_instance in _form_field_instance_type_list:
        if _field_instance.get('kds_formInstanceType') == instance:
            _form_instance_info = _field_instance
    #print("instance info\n",_form_instance_info)
    # Determine the field paramaters
    _new_field['kds_formFieldName'] = _form_instance_info.get('kds_formFieldName', field.get(\
            "kds_formFieldName", field.get('kds_formDefault', {}).get(\
            'kds_formFieldName', "")))
    _new_field['kds_fieldType'] = _form_instance_info.get('kds_fieldType', field.get(\
            'kds_fieldType', field.get('kds_formDefault', {}).get('kds_fieldType', "")))
    if not isinstance(_new_field['kds_fieldType'], dict):
        _new_field['kds_fieldType'] = {"rdf_type": _new_field['kds_fieldType']}

    _new_field['kds_formLabelName'] = _form_instance_info.get('kds_formLabelName', \
            field.get("kds_formLabelName", field.get('kds_formDefault', {}).get(\
            'kds_formLabelName', "")))
    _new_field['kds_formFieldHelp'] = _form_instance_info.get('kds_formFieldHelp', \
            field.get("formFieldHelp", field.get('formDefault', {}).get(\
            'kds_formFieldHelp', "")))
    _new_field['kds_formFieldOrder'] = _form_instance_info.get('kds_formFieldOrder', \
            field.get("kds_formFieldOrder", field.get('kds_formDefault', {}).get(\
            'kds_formFieldOrder', "")))
    _new_field['kds_formLayoutRow'] = _form_instance_info.get('kds_formLayoutRow', \
            field.get("kds_formLayoutRow", field.get('kds_formDefault', {}).get(\
            'kds_formLayoutRow', "")))
    _new_field['kds_propUri'] = field.get('kds_propUri')
    _new_field['kds_classUri'] = field.get('kds_classUri')
    _new_field['rdfs_range'] = field.get('rdfs_range')
    _new_field['kds_defaultVal'] = _form_instance_info.get('kds_defaultVal',\
            field.get('kds_defaultVal'))

    # get applicationActionList
    _new_field['kds_actionList'] = make_set(_form_instance_info.get(\
            'kds_applicationAction', set()))
    _new_field['kds_actionList'].union(
        make_set(field.get('kds_applicationAction', set())))
    _new_field['kds_actionList'] = list(_new_field['kds_actionList'])
    #print("action List:_______________", _new_field['kds_actionList'])
    if "kdr_RemoveFromForm" in\
            _new_field['kds_actionList']:
        return None
    # get valiator list
    if field.get('kds_overrideValidation'):
        _new_field['kds_validators'] = field.get('kds_overrideValidation')
    else:
        _new_field['kds_validators'] = make_list(\
                _form_instance_info.get('kds_formValidation', []))
        _new_field['kds_validators'] += make_list(\
                field.get('kds_formValidation', []))
        _new_field['kds_validators'] += make_list(\
                field.get('kds_propertyValidation', []))
    # get processing list
    _new_field['kds_processors'] = make_list(
        _form_instance_info.get('kds_formProcessing', []))
    _new_field['kds_processors'] += make_list(
        field.get('kds_formProcessing', []))
    _new_field['kds_processors'] += make_list(
        field.get('kds_propertyProcessing', []))
    if debug:
        if field['kds_propUri'] == "schema_image":
            x = 1
    # get required state
    _required = False
    _field_req_var = cbool(field.get('kds_requiredField'))
    if (field.get('kds_propUri') in make_list(field.get('kds_classInfo', {}).get(\
            'kds_primaryKey', []))) or _field_req_var:
        _required = True
    if field.get('kds_classUri') in make_list(
            field.get('kds_requiredByDomain', {})):
        _required = True
    if _field_req_var == False:
        _required = False
    _new_field['kds_required'] = _required

    # Determine EditState
    if ("acl_Write" in _access_level) and ("kdr_NotEditable" \
            not in _new_field['kds_actionList']):
        _new_field['editable'] = True
    else:
        _new_field['editable'] = False

    # Determine css classes
    css = _form_instance_info.get('kds_overrideCss', field.get('kds_overrideCss', \
            instructions.get('kds_overrideCss', None)))
    if css is None:
        css = _rdf_app.get('kds_formDefault', {}).get('kds_fieldCss', '')
        css = css.strip() + " " + instructions.get('kds_propertyAddOnCss', '')
        css = css.strip() + " " + _form_instance_info.get('kds_addOnCss', field.get(\
                'kds_addOnCss', field.get('kds_formDefault', {}).get('kds_addOnCss', '')))
        css = css.strip()
    _new_field['kds_css'] = css
    #print("field_json:\n", json.dumps(_new_field, indent=4))

    return _new_field
Exemplo n.º 49
0
    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