def create_description(self): """ Creates a new descriptive assertion if data is valid """ self.is_valid = self.validate_creation() if self.is_valid: new_ass = Assertion() new_ass.uuid = self.subject_uuid new_ass.subject_type = self.subject_type new_ass.project_uuid = self.project_uuid new_ass.source_id = self.source_id new_ass.obs_node = self.obs_node new_ass.obs_num = self.obs_num new_ass.sort = self.sort new_ass.visibility = 1 new_ass.predicate_uuid = self.predicate_uuid new_ass.object_type = self.object_type if self.object_uuid is not False: new_ass.object_uuid = self.object_uuid if self.data_num is not None: new_ass.data_num = self.data_num if self.data_date is not None: new_ass.data_date = self.data_date new_ass.save()
def validate_save_field_value(self, item_man, # subject manifest object field, field_value_dict, value_num): """ Valididates a field value for a field and then saves it to the database if valid """ error_key = str(field['id']) + '-' + str(value_num) valid = True id_val = None literal_val = None if isinstance(field_value_dict, dict): if 'id' in field_value_dict: id_val = field_value_dict['id'] if 'literal' in field_value_dict: literal_val = field_value_dict['literal'] else: valid = False self.errors[error_key] = 'Value submitted to ' + field['label'] + ' has the wrong format' data_type = self.get_predicate_data_type(field['predicate_uuid']) object_uuid = None object_type = data_type data_num = None data_date = None if field['predicate_uuid'] in self.global_predicates: # a global predicate, so get from dictionary set above data_type = self.global_predicates[field['predicate_uuid']]['data_type'] else: pred_man = self.get_manifest_item(field['predicate_uuid']) if pred_man is False or data_type is False: # could not find a predicate_uuid!! valid = False self.errors[error_key] = 'Problem with: ' + field['label'] + ' ' self.errors[error_key] += '(' + field['predicate_uuid'] + ') is not found.' if valid: # predicate_uuid exists str_obj = None if data_type == 'xsd:string': str_man = StringManagement() str_man.project_uuid = item_man.project_uuid str_man.source_id = item_man.source_id str_obj = str_man.get_make_string(str(literal_val)) object_uuid = str_obj.uuid object_type = data_type elif data_type == 'id': # first check is to see if the field_value exists in the manifest val_man = self.get_manifest_item(id_val) if val_man is False: valid = False self.errors[error_key] = 'Problem with: ' + field['label'] + '; cannot find: ' + str(id_val) if valid: # OK, the field_value is an ID in the manifest # so let's check to make sure it is OK to associate object_type = val_man.item_type object_uuid = val_man.uuid if val_man.class_uri == 'variable' \ and val_man.item_type != 'types': # we've got a variable that does not link to a type! self.errors[error_key] = 'Problem with: ' + field['label'] + '; ' self.errors[error_key] += val_man.label + ' (' + val_man.uuid + ') is not a type/category.' valid = False elif data_type == 'xsd:boolean': data_num = self.validate_convert_boolean(literal_val) if t_f_data is None: self.errors[error_key] = 'Problem with: ' + field['label'] + '; ' self.errors[error_key] += '"' + str(literal_val) + '" is not a recognized boolean (T/F) value.' valid = False elif data_type == 'xsd:integer': try: data_num = int(float(literal_val)) except: self.errors[error_key] = 'Problem with: ' + field['label'] + '; ' self.errors[error_key] += '"' + str(literal_val) + '" is not an integer.' valid = False elif data_type == 'xsd:double': try: data_num = float(literal_val) except: self.errors[error_key] = 'Problem with: ' + field['label'] + '; ' self.errors[error_key] += '"' + str(literal_val) + '" is not a number.' valid = False elif data_type == 'xsd:date': try: data_date = parse(literal_val) except Exception as e: self.errors[error_key] = 'Problem with: ' + field['label'] + '; ' self.errors[error_key] += '"' + str(literal_val) + '" is not a yyyy-mm-dd date' valid = False if valid: # we've validated the field_value data. Now to save the assertions! new_ass = Assertion() new_ass.uuid = item_man.uuid new_ass.subject_type = item_man.item_type new_ass.project_uuid = item_man.project_uuid new_ass.source_id = self.source_id new_ass.obs_node = '#obs-' + str(field['obs_num']) new_ass.obs_num = field['obs_num'] new_ass.sort = field['sort'] + (value_num / 1000) new_ass.visibility = 1 new_ass.predicate_uuid = field['predicate_uuid'] new_ass.object_type = object_type if object_uuid is not None: new_ass.object_uuid = object_uuid if data_num is not None: new_ass.data_num = data_num if data_date is not None: new_ass.data_date = data_date try: new_ass.save() except: valid = False self.errors[error_key] = 'Same assertion for: ' + field['label'] + ' ' self.errors[error_key] += 'already exists.' return valid
def add_description(self): """ validates the value's data type if the data type differs from the predicate data type, add it as a note """ # validate the faims record against its expected data type # the validated_record will be None if not valid if isinstance(self.subject_uuid, str) \ and isinstance(self.subject_type, str): # we have subject information ok_to_add = False data_type = self.attrib_dict['data_type'] if isinstance(self.predicate_uuid, str): predicate_uuid = self.predicate_uuid elif 'predicate_uuid' in self.attrib_dict: predicate_uuid = self.attrib_dict['predicate_uuid'] object_date = None object_uuid = None object_num = None object_type = data_type if data_type == 'id': # we're processing a controlled vocab (oc-type) object if isinstance(self.faims_record_id, str): if self.faims_record_id in self.attrib_dict['objects']: # we found the object for this item obj_dict = self.attrib_dict['objects'][ self.faims_record_id] object_uuid = obj_dict['type_uuid'] object_type = 'types' ok_to_add = True elif self.faims_record is not None: # we're processing a literal value ddt = DescriptionDataType() validated_record = ddt.validate_literal_by_data_type( data_type, self.faims_record) if validated_record is not None: # the record is valid for the data_type ok_to_add = True if data_type == 'xsd:date': object_date = validated_record elif data_type == 'xsd:string': # we have a string, so add it to the database and get its uuid str_man = StringManagement() str_man.project_uuid = self.project_uuid str_man.source_id = self.source_id str_obj = str_man.get_make_string(validated_record) object_uuid = str(str_obj.uuid) else: # we have numeric data, so add to numeric data object_num = validated_record else: # the record is not valid for the data_type, so treat it as a string # so first we need to make a predicate for it, that is for # string values of this associated with this attribute object_type = 'xsd:string' sup_dict = LastUpdatedOrderedDict() sup_dict[self.reconcile_key] = self.attrib_dict[ 'id'] # the faims attribute id pm = PredicateManagement() pm.project_uuid = self.project_uuid pm.source_id = self.source_id pm.sup_dict = sup_dict pm.sup_reconcile_key = self.reconcile_key pm.sup_reconcile_value = self.attrib_dict[ 'id'] # the faims attribute id pred_obj = pm.get_make_predicate( self.attrib_dict['label'] + ' [Note]', self.attrib_dict['predicate_type'], object_type) if pred_obj is not False: # we have a new predicate predicate_uuid = pred_obj.uuid # now make an object_uuid for the original faims record str_man = StringManagement() str_man.project_uuid = self.project_uuid str_man.source_id = self.source_id str_obj = str_man.get_make_string(self.faims_record) object_uuid = str(str_obj.uuid) ok_to_add = True else: # not valid data, don't add ok_to_add = False if ok_to_add: # now we're OK to add the descrpitive assertion new_ass = Assertion() new_ass.uuid = self.subject_uuid new_ass.subject_type = self.subject_type new_ass.project_uuid = self.project_uuid new_ass.source_id = self.source_id new_ass.obs_node = '#obs-' + str(self.obs_num) new_ass.obs_num = self.obs_num new_ass.sort = self.sort_num new_ass.visibility = 1 new_ass.predicate_uuid = predicate_uuid new_ass.object_type = object_type if object_num is not None: new_ass.data_num = object_num if object_date is not None: new_ass.data_date = object_date if object_uuid is not None: new_ass.object_uuid = object_uuid try: new_ass.save() except: # probably already imported pass
def add_description(self): """ validates the value's data type if the data type differs from the predicate data type, add it as a note """ # validate the faims record against its expected data type # the validated_record will be None if not valid if isinstance(self.subject_uuid, str) \ and isinstance(self.subject_type, str): # we have subject information ok_to_add = False data_type = self.attrib_dict['data_type'] if isinstance(self.predicate_uuid, str): predicate_uuid = self.predicate_uuid elif 'predicate_uuid' in self.attrib_dict: predicate_uuid = self.attrib_dict['predicate_uuid'] object_date = None object_uuid = None object_num = None object_type = data_type if data_type == 'id': # we're processing a controlled vocab (oc-type) object if isinstance(self.faims_record_id, str): if self.faims_record_id in self.attrib_dict['objects']: # we found the object for this item obj_dict = self.attrib_dict['objects'][self.faims_record_id] object_uuid = obj_dict['type_uuid'] object_type = 'types' ok_to_add = True elif self.faims_record is not None: # we're processing a literal value ddt = DescriptionDataType() validated_record = ddt.validate_literal_by_data_type(data_type, self.faims_record) if validated_record is not None: # the record is valid for the data_type ok_to_add = True if data_type == 'xsd:date': object_date = validated_record elif data_type == 'xsd:string': # we have a string, so add it to the database and get its uuid str_man = StringManagement() str_man.project_uuid = self.project_uuid str_man.source_id = self.source_id str_obj = str_man.get_make_string(validated_record) object_uuid = str(str_obj.uuid) else: # we have numeric data, so add to numeric data object_num = validated_record else: # the record is not valid for the data_type, so treat it as a string # so first we need to make a predicate for it, that is for # string values of this associated with this attribute object_type = 'xsd:string' sup_dict = LastUpdatedOrderedDict() sup_dict[self.reconcile_key] = self.attrib_dict['id'] # the faims attribute id pm = PredicateManagement() pm.project_uuid = self.project_uuid pm.source_id = self.source_id pm.sup_dict = sup_dict pm.sup_reconcile_key = self.reconcile_key pm.sup_reconcile_value = self.attrib_dict['id'] # the faims attribute id pred_obj = pm.get_make_predicate(self.attrib_dict['label'] + ' [Note]', self.attrib_dict['predicate_type'], object_type) if pred_obj is not False: # we have a new predicate predicate_uuid = pred_obj.uuid # now make an object_uuid for the original faims record str_man = StringManagement() str_man.project_uuid = self.project_uuid str_man.source_id = self.source_id str_obj = str_man.get_make_string(self.faims_record) object_uuid = str(str_obj.uuid) ok_to_add = True else: # not valid data, don't add ok_to_add = False if ok_to_add: # now we're OK to add the descrpitive assertion new_ass = Assertion() new_ass.uuid = self.subject_uuid new_ass.subject_type = self.subject_type new_ass.project_uuid = self.project_uuid new_ass.source_id = self.source_id new_ass.obs_node = '#obs-' + str(self.obs_num) new_ass.obs_num = self.obs_num new_ass.sort = self.sort_num new_ass.visibility = 1 new_ass.predicate_uuid = predicate_uuid new_ass.object_type = object_type if object_num is not None: new_ass.data_num = object_num if object_date is not None: new_ass.data_date = object_date if object_uuid is not None: new_ass.object_uuid = object_uuid try: new_ass.save() except: # probably already imported pass
def validate_save_field_value( self, item_man, # subject manifest object field, field_value_dict, value_num): """ Valididates a field value for a field and then saves it to the database if valid """ error_key = str(field['id']) + '-' + str(value_num) valid = True id_val = None literal_val = None if isinstance(field_value_dict, dict): if 'id' in field_value_dict: id_val = field_value_dict['id'] if 'literal' in field_value_dict: literal_val = field_value_dict['literal'] else: valid = False self.errors[error_key] = 'Value submitted to ' + field[ 'label'] + ' has the wrong format' data_type = self.get_predicate_data_type(field['predicate_uuid']) object_uuid = None object_type = data_type data_num = None data_date = None if field['predicate_uuid'] in self.global_predicates: # a global predicate, so get from dictionary set above data_type = self.global_predicates[ field['predicate_uuid']]['data_type'] else: pred_man = self.get_manifest_item(field['predicate_uuid']) if pred_man is False or data_type is False: # could not find a predicate_uuid!! valid = False self.errors[ error_key] = 'Problem with: ' + field['label'] + ' ' self.errors[error_key] += '(' + field[ 'predicate_uuid'] + ') is not found.' if valid: # predicate_uuid exists str_obj = None if data_type == 'xsd:string': str_man = StringManagement() str_man.project_uuid = item_man.project_uuid str_man.source_id = item_man.source_id str_obj = str_man.get_make_string(str(literal_val)) object_uuid = str_obj.uuid object_type = data_type elif data_type == 'id': # first check is to see if the field_value exists in the manifest val_man = self.get_manifest_item(id_val) if val_man is False: valid = False self.errors[error_key] = 'Problem with: ' + field[ 'label'] + '; cannot find: ' + str(id_val) if valid: # OK, the field_value is an ID in the manifest # so let's check to make sure it is OK to associate object_type = val_man.item_type object_uuid = val_man.uuid if val_man.class_uri == 'variable' \ and val_man.item_type != 'types': # we've got a variable that does not link to a type! self.errors[error_key] = 'Problem with: ' + field[ 'label'] + '; ' self.errors[ error_key] += val_man.label + ' (' + val_man.uuid + ') is not a type/category.' valid = False elif data_type == 'xsd:boolean': data_num = self.validate_convert_boolean(literal_val) if t_f_data is None: self.errors[ error_key] = 'Problem with: ' + field['label'] + '; ' self.errors[error_key] += '"' + str( literal_val ) + '" is not a recognized boolean (T/F) value.' valid = False elif data_type == 'xsd:integer': try: data_num = int(float(literal_val)) except: self.errors[ error_key] = 'Problem with: ' + field['label'] + '; ' self.errors[error_key] += '"' + str( literal_val) + '" is not an integer.' valid = False elif data_type == 'xsd:double': try: data_num = float(literal_val) except: self.errors[ error_key] = 'Problem with: ' + field['label'] + '; ' self.errors[error_key] += '"' + str( literal_val) + '" is not a number.' valid = False elif data_type == 'xsd:date': try: data_date = parse(literal_val) except Exception as e: self.errors[ error_key] = 'Problem with: ' + field['label'] + '; ' self.errors[error_key] += '"' + str( literal_val) + '" is not a yyyy-mm-dd date' valid = False if valid: # we've validated the field_value data. Now to save the assertions! new_ass = Assertion() new_ass.uuid = item_man.uuid new_ass.subject_type = item_man.item_type new_ass.project_uuid = item_man.project_uuid new_ass.source_id = self.source_id new_ass.obs_node = '#obs-' + str(field['obs_num']) new_ass.obs_num = field['obs_num'] new_ass.sort = field['sort'] + (value_num / 1000) new_ass.visibility = 1 new_ass.predicate_uuid = field['predicate_uuid'] new_ass.object_type = object_type if object_uuid is not None: new_ass.object_uuid = object_uuid if data_num is not None: new_ass.data_num = data_num if data_date is not None: new_ass.data_date = data_date try: new_ass.save() except: valid = False self.errors[error_key] = 'Same assertion for: ' + field[ 'label'] + ' ' self.errors[error_key] += 'already exists.' return valid