def update_field_predicate_infield(self, field_num, object_field_num, predicate_field_num): """ Updates a field annotation to make entities in a field_num (subject) have custom relationships defined by the values in a predicate_field_num to entities in the object_field_num """ try: # in case we try to pass somthing that's not an integer anno_subjs = ImportFieldAnnotation.objects\ .filter(source_id=self.source_id, field_num=field_num, object_field_num=object_field_num, predicate_field_num=predicate_field_num)\ .delete() except: pass anno_subjs = ImportFieldAnnotation.objects\ .filter(source_id=self.source_id, field_num=field_num, object_field_num=object_field_num, predicate='', predicate_field_num=predicate_field_num)\ .delete() ifa = ImportFieldAnnotation() ifa.source_id = self.source_id ifa.project_uuid = self.project_uuid ifa.field_num = field_num ifa.predicate = '' ifa.predicate_field_num = predicate_field_num ifa.object_field_num = object_field_num ifa.object_uuid = '' ifa.save()
def update_desciption(self, field_num, object_field_num): """ Updates a field annotation so that a list of field_num are assigned to describe an object_field_num entity """ self.get_field_num_list(field_num) # delete cases where the object_field_num is contained by another field anno_objs = ImportFieldAnnotation.objects\ .filter(source_id=self.source_id, predicate=ImportFieldAnnotation.PRED_DESCRIBES, field_num__in=self.field_num_list)\ .delete() entity_ok = self.check_field_type(object_field_num, ImportProfile.DEFAULT_SUBJECT_TYPE_FIELDS) for field_num in self.field_num_list: des_ok = self.check_field_type(field_num, ['description', 'variable']) if des_ok and entity_ok: # only make the annotation if the subject is a value, object is a variable ifa = ImportFieldAnnotation() ifa.source_id = self.source_id ifa.project_uuid = self.project_uuid ifa.field_num = field_num ifa.predicate = ImportFieldAnnotation.PRED_DESCRIBES ifa.predicate_field_num = 0 ifa.object_field_num = object_field_num ifa.object_uuid = '' ifa.save()
def update_field_containedin_entity(self, field_num, object_uuid): """ Updates a field annotation to make it contained a subject entity (object_uuid) after first deleting other containment annotations """ # delete cases where the another field contains the current field anno_objs = ImportFieldAnnotation.objects\ .filter(source_id=self.source_id, predicate=Assertion.PREDICATES_CONTAINS, object_field_num=field_num)\ .delete() # delete cases where the current field is contained in an entity anno_subjs = ImportFieldAnnotation.objects\ .filter(source_id=self.source_id, predicate=ImportFieldAnnotation.PRED_CONTAINED_IN, field_num=field_num)\ .delete() ifa = ImportFieldAnnotation() ifa.source_id = self.source_id ifa.project_uuid = self.project_uuid ifa.field_num = field_num ifa.predicate = ImportFieldAnnotation.PRED_CONTAINED_IN ifa.predicate_field_num = 0 ifa.object_field_num = 0 ifa.object_uuid = object_uuid ifa.save()
def update_field_document_text_entity(self, field_num, object_field_num): """ Updates a field annotation to make document entities in a field_num (subject) have document text in an object_ield_num """ # delete cases where the (subject) field_num is a media part of another field anno_objs = ImportFieldAnnotation.objects\ .filter(source_id=self.source_id, predicate=ImportFieldAnnotation.PRED_DOC_Text, field_num=field_num)\ .delete() ifa = ImportFieldAnnotation() ifa.source_id = self.source_id ifa.project_uuid = self.project_uuid ifa.field_num = field_num ifa.predicate = ImportFieldAnnotation.PRED_DOC_Text ifa.predicate_field_num = 0 ifa.object_field_num = object_field_num ifa.object_uuid = '' ifa.save()
def update_field_media_part_of_entity(self, field_num, object_field_num): """ Updates a field annotation to make entities in a field_num (subject) a media part of entities in an object_ield_num """ # delete cases where the (subject) field_num is a media part of another field anno_objs = ImportFieldAnnotation.objects\ .filter(source_id=self.source_id, predicate=ImportFieldAnnotation.PRED_MEDIA_PART_OF, field_num=field_num)\ .delete() ifa = ImportFieldAnnotation() ifa.source_id = self.source_id ifa.project_uuid = self.project_uuid ifa.field_num = field_num ifa.predicate = ImportFieldAnnotation.PRED_MEDIA_PART_OF ifa.predicate_field_num = 0 ifa.object_field_num = object_field_num ifa.object_uuid = '' ifa.save()
def update_obs_num(self, field_num, object_field_num): """ Updates a field annotation to make entities in a field_num (subject) contain entities in an object_ield_num """ # delete cases where the field_num is an OBS NUM of another field anno_objs = ImportFieldAnnotation.objects\ .filter(source_id=self.source_id, predicate=ImportFieldAnnotation.PRED_OBS_NUM, field_num=field_num)\ .delete() ifa = ImportFieldAnnotation() ifa.source_id = self.source_id ifa.project_uuid = self.project_uuid ifa.field_num = field_num ifa.predicate = ImportFieldAnnotation.PRED_OBS_NUM ifa.predicate_field_num = 0 ifa.object_field_num = object_field_num ifa.object_uuid = '' ifa.save()
def update_field_links(self, field_num, object_field_num): """ Updates a field annotation to make entities in a field_num (subject) contain entities in an object_ield_num """ # delete cases where the object_field_num is contained by another field anno_objs = ImportFieldAnnotation.objects\ .filter(source_id=self.source_id, predicate=Assertion.PREDICATES_LINK, object_field_num=object_field_num)\ .delete() ifa = ImportFieldAnnotation() ifa.source_id = self.source_id ifa.project_uuid = self.project_uuid ifa.field_num = field_num ifa.predicate = Assertion.PREDICATES_LINK ifa.predicate_field_num = 0 ifa.object_field_num = object_field_num ifa.object_uuid = '' ifa.save()
def update_field_contains(self, field_num, object_field_num, pred_contains=None): """ Updates a field annotation to make entities in a field_num (subject) contain entities in an object_ield_num """ if pred_contains is None: pred_contains = Assertion.PREDICATES_CONTAINS # delete cases where the object_field_num is contained by another field del_preds = [Assertion.PREDICATES_CONTAINS, ImportFieldAnnotation.PRED_DRAFT_CONTAINS] anno_objs = ImportFieldAnnotation.objects\ .filter(source_id=self.source_id, predicate__in=del_preds, object_field_num=object_field_num)\ .delete() ifa = ImportFieldAnnotation() ifa.source_id = self.source_id ifa.project_uuid = self.project_uuid ifa.field_num = field_num ifa.predicate = pred_contains ifa.predicate_field_num = 0 ifa.object_field_num = object_field_num ifa.object_uuid = '' ifa.save()
def update_variable_value(self, field_num, object_field_num): """ Updates a field annotation to make entities in a field_num (subject) contain entities in an object_ield_num """ # delete cases where the field_num is a value_of another field anno_objs = ImportFieldAnnotation.objects\ .filter(source_id=self.source_id, predicate=ImportFieldAnnotation.PRED_VALUE_OF, field_num=field_num)\ .delete() ifa = ImportFieldAnnotation() ifa.source_id = self.source_id ifa.project_uuid = self.project_uuid ifa.field_num = field_num ifa.predicate = ImportFieldAnnotation.PRED_VALUE_OF ifa.predicate_field_num = 0 ifa.object_field_num = object_field_num ifa.object_uuid = '' ifa.save()
def update_desciption(self, field_num, object_field_num): """ Updates a field annotation so that a list of field_num are assigned to describe an object_field_num entity """ self.get_field_num_list(field_num) # delete cases where the object_field_num is contained by another field anno_objs = ImportFieldAnnotation.objects\ .filter(source_id=self.source_id, predicate=ImportFieldAnnotation.PRED_DESCRIBES, field_num__in=self.field_num_list)\ .delete() entity_ok = self.check_field_type( object_field_num, ImportProfile.DEFAULT_SUBJECT_TYPE_FIELDS) for field_num in self.field_num_list: des_ok = self.check_field_type(field_num, ['description', 'variable']) if des_ok and entity_ok: # only make the annotation if the subject is a value, object is a variable ifa = ImportFieldAnnotation() ifa.source_id = self.source_id ifa.project_uuid = self.project_uuid ifa.field_num = field_num ifa.predicate = ImportFieldAnnotation.PRED_DESCRIBES ifa.predicate_field_num = 0 ifa.object_field_num = object_field_num ifa.object_uuid = '' ifa.save()
def update_description(self, field_num, object_field_num): """ Updates a field annotation so that a list of field_num are assigned to describe an object_field_num entity """ self.get_field_num_list(field_num) # delete cases where the field_num is already used del_preds = [ ImportFieldAnnotation.PRED_DESCRIBES, ImportFieldAnnotation.PRED_GEO_LOCATION, ImportFieldAnnotation.PRED_DATE_EVENT, ImportFieldAnnotation.PRED_METADATA, ImportFieldAnnotation.PRED_COMPLEX_DES ] anno_objs = ImportFieldAnnotation.objects\ .filter(source_id=self.source_id, predicate__in=del_preds, field_num__in=self.field_num_list)\ .delete() entity_ok = self.check_field_type( object_field_num, ImportProfile.DEFAULT_SUBJECT_TYPE_FIELDS) # now check to see if this is OK for making a geolocation geo_date_object_ok = self.check_field_type(object_field_num, ['subjects']) for field_num in self.field_num_list: des_ok = self.check_field_type(field_num, ['description', 'variable']) geo_ok = self.check_field_type(field_num, ['lat', 'lon', 'geojson']) date_ok = self.check_field_type(field_num, ['early', 'late']) meta_ok = self.check_field_type(field_num, ['metadata']) complex_des_ok = self.check_field_type(field_num, ['complex-description']) if des_ok and entity_ok: # only make the annotation if the subject is a value, object is a variable ifa = ImportFieldAnnotation() ifa.source_id = self.source_id ifa.project_uuid = self.project_uuid ifa.field_num = field_num ifa.predicate = ImportFieldAnnotation.PRED_DESCRIBES ifa.predicate_field_num = 0 ifa.object_field_num = object_field_num ifa.object_uuid = '' ifa.save() elif geo_ok and geo_date_object_ok: # we have a geospatial annotations ifa = ImportFieldAnnotation() ifa.source_id = self.source_id ifa.project_uuid = self.project_uuid ifa.field_num = field_num ifa.predicate = ImportFieldAnnotation.PRED_GEO_LOCATION ifa.predicate_field_num = 0 ifa.object_field_num = object_field_num ifa.object_uuid = '' ifa.save() elif date_ok and geo_date_object_ok: # we have a date annotations ifa = ImportFieldAnnotation() ifa.source_id = self.source_id ifa.project_uuid = self.project_uuid ifa.field_num = field_num ifa.predicate = ImportFieldAnnotation.PRED_DATE_EVENT ifa.predicate_field_num = 0 ifa.object_field_num = object_field_num ifa.object_uuid = '' ifa.save() elif complex_des_ok: # we have complex description annotations ifa = ImportFieldAnnotation() ifa.source_id = self.source_id ifa.project_uuid = self.project_uuid ifa.field_num = field_num ifa.predicate = ImportFieldAnnotation.PRED_COMPLEX_DES ifa.predicate_field_num = 0 ifa.object_field_num = object_field_num ifa.object_uuid = '' ifa.save() elif meta_ok: # we have metadata annotations ifa = ImportFieldAnnotation() ifa.source_id = self.source_id ifa.project_uuid = self.project_uuid ifa.field_num = field_num ifa.predicate = ImportFieldAnnotation.PRED_METADATA ifa.predicate_field_num = 0 ifa.object_field_num = object_field_num ifa.object_uuid = '' ifa.save()
def update_field_custom_predicate(self, field_num, object_field_num, predicate_id, predicate_type): """ Updates a field annotation to make entities in a field_num (subject) have a custom relationship (predicate_id) to an object_field_num """ try: # in case we try to pass somthing that's not an integer anno_subjs = ImportFieldAnnotation.objects\ .filter(source_id=self.source_id, field_num=field_num, object_field_num=object_field_num, predicate_field_num=predicate_id)\ .delete() except: pass anno_subjs = ImportFieldAnnotation.objects\ .filter(source_id=self.source_id, field_num=field_num, object_field_num=object_field_num, predicate=predicate_id)\ .delete() ifa = ImportFieldAnnotation() ifa.source_id = self.source_id ifa.project_uuid = self.project_uuid ifa.field_num = field_num if 'field' in predicate_type: # predicate linking relation in an importer field # means entities in this field describe different linking relations ifa.predicate = '' ifa.predicate_field_num = predicate_id else: # predicate linking relation is the predicate_id ifa.predicate = predicate_id ifa.predicate_field_num = 0 ifa.object_field_num = object_field_num ifa.object_uuid = '' ifa.save()
def update_description(self, field_num, object_field_num): """ Updates a field annotation so that a list of field_num are assigned to describe an object_field_num entity """ self.get_field_num_list(field_num) # delete cases where the field_num is already used del_preds = [ImportFieldAnnotation.PRED_DESCRIBES, ImportFieldAnnotation.PRED_GEO_LOCATION, ImportFieldAnnotation.PRED_DATE_EVENT, ImportFieldAnnotation.PRED_METADATA, ImportFieldAnnotation.PRED_COMPLEX_DES] anno_objs = ImportFieldAnnotation.objects\ .filter(source_id=self.source_id, predicate__in=del_preds, field_num__in=self.field_num_list)\ .delete() entity_ok = self.check_field_type(object_field_num, ImportProfile.DEFAULT_SUBJECT_TYPE_FIELDS) # now check to see if this is OK for making a geolocation geo_date_object_ok = self.check_field_type(object_field_num, ['subjects']) for field_num in self.field_num_list: des_ok = self.check_field_type(field_num, ['description', 'variable']) geo_ok = self.check_field_type(field_num, ['lat', 'lon', 'geojson']) date_ok = self.check_field_type(field_num, ['early', 'late']) meta_ok = self.check_field_type(field_num, ['metadata']) complex_des_ok = self.check_field_type(field_num, ['complex-description']) if des_ok and entity_ok: # only make the annotation if the subject is a value, object is a variable ifa = ImportFieldAnnotation() ifa.source_id = self.source_id ifa.project_uuid = self.project_uuid ifa.field_num = field_num ifa.predicate = ImportFieldAnnotation.PRED_DESCRIBES ifa.predicate_field_num = 0 ifa.object_field_num = object_field_num ifa.object_uuid = '' ifa.save() elif geo_ok and geo_date_object_ok: # we have a geospatial annotations ifa = ImportFieldAnnotation() ifa.source_id = self.source_id ifa.project_uuid = self.project_uuid ifa.field_num = field_num ifa.predicate = ImportFieldAnnotation.PRED_GEO_LOCATION ifa.predicate_field_num = 0 ifa.object_field_num = object_field_num ifa.object_uuid = '' ifa.save() elif date_ok and geo_date_object_ok: # we have a date annotations ifa = ImportFieldAnnotation() ifa.source_id = self.source_id ifa.project_uuid = self.project_uuid ifa.field_num = field_num ifa.predicate = ImportFieldAnnotation.PRED_DATE_EVENT ifa.predicate_field_num = 0 ifa.object_field_num = object_field_num ifa.object_uuid = '' ifa.save() elif complex_des_ok: # we have complex description annotations ifa = ImportFieldAnnotation() ifa.source_id = self.source_id ifa.project_uuid = self.project_uuid ifa.field_num = field_num ifa.predicate = ImportFieldAnnotation.PRED_COMPLEX_DES ifa.predicate_field_num = 0 ifa.object_field_num = object_field_num ifa.object_uuid = '' ifa.save() elif meta_ok: # we have metadata annotations ifa = ImportFieldAnnotation() ifa.source_id = self.source_id ifa.project_uuid = self.project_uuid ifa.field_num = field_num ifa.predicate = ImportFieldAnnotation.PRED_METADATA ifa.predicate_field_num = 0 ifa.object_field_num = object_field_num ifa.object_uuid = '' ifa.save()
def update_field_contains(self, field_num, object_field_num, pred_contains=None): """ Updates a field annotation to make entities in a field_num (subject) contain entities in an object_ield_num """ if pred_contains is None: pred_contains = Assertion.PREDICATES_CONTAINS # delete cases where the object_field_num is contained by another field del_preds = [ Assertion.PREDICATES_CONTAINS, ImportFieldAnnotation.PRED_DRAFT_CONTAINS ] anno_objs = ImportFieldAnnotation.objects\ .filter(source_id=self.source_id, predicate__in=del_preds, object_field_num=object_field_num)\ .delete() ifa = ImportFieldAnnotation() ifa.source_id = self.source_id ifa.project_uuid = self.project_uuid ifa.field_num = field_num ifa.predicate = pred_contains ifa.predicate_field_num = 0 ifa.object_field_num = object_field_num ifa.object_uuid = '' ifa.save()
def load_attribute_df_configs(project_uuid, source_id, source_type, df, attribute_col_configs=DF_ATTRIBUTE_CONFIGS): """Updates ImportFields with configurations""" # NOTE: This has the assumption that a column has a "primary key", # of the main entity that gets description. Descriptions and other # relationships between columns by default use the "primary key" # column as subject of a relationship. defalut_field_args = { 'field_type': 'ignore', 'field_data_type': '', } kfs = KoboFields() cols = df.columns.tolist() pk_field_num = None field_rels = [] for field_num, col in enumerate(cols, 1): if col in kfs.fields: # This is a kobo metadata field, to be field_rels.append({ 'predicate': ImportFieldAnnotation.PRED_METADATA, 'subject_field_num': field_num, }) # Skip fields configured in KoboFields. continue field_args = None field_rel = None for config in attribute_col_configs: # Default to ignore if (source_type in config['sources'] and (col == config['source-column'] or (col.startswith(config['source-column']) and config['match_type'] == 'startswith'))): field_args = config['field_args'].copy() if config.get('subject_pk'): pk_field_num = field_num if config.get('field_rel'): field_rel = config['field_rel'] if field_rel.get( 'predicate' ) == ImportFieldAnnotation.PRED_MEDIA_PART_OF: # A media file type column is the subject, the primary key field is obj. field_rel['subject_field_num'] = field_num elif field_rel.get( 'predicate' ) == ImportFieldAnnotation.PRED_GEO_LOCATION: # A geospatial type column is the subject, the primary key field is obj. field_rel['subject_field_num'] = field_num else: field_rel['object_field_num'] = field_num elif field_args.get('field_type') == 'description': field_rel = { 'predicate': ImportFieldAnnotation.PRED_DESCRIBES, 'subject_field_num': field_num, } # Don't break, incase a more specific config # is waiting. # Now update the field. if field_args is None: # We didn't find any specific config, so we will ignore # the column. field_args = defalut_field_args.copy() else: print('Found {} config for {}'.format(source_type, col)) # Update the column with configutations ImportField.objects.filter( project_uuid=project_uuid, source_id=source_id, ref_orig_name=col, field_num=field_num, ).update(**field_args) if field_rel is not None: field_rels.append(field_rel) # Now add configured relationship annotations between fields if pk_field_num is None or not len(field_rels): return None for field_rel in field_rels: # Use the specified subject field num, or default to the # source table's pk_field_num. subject_field_num = field_rel.get('subject_field_num', pk_field_num) object_field_num = field_rel.get('object_field_num', pk_field_num) # Just to be sure, delete prior links between these fields for # this source. ImportFieldAnnotation.objects.filter( source_id=source_id, project_uuid=project_uuid, field_num=subject_field_num, object_field_num=object_field_num, ).delete() # Now create the linkage imp_fa = ImportFieldAnnotation() imp_fa.source_id = source_id imp_fa.project_uuid = project_uuid # Use the specified subject field num, or default to the # source table's pk_field_num. imp_fa.field_num = subject_field_num imp_fa.predicate = field_rel['predicate'] imp_fa.predicate_field_num = field_rel.get('predicate_field_num', 0) imp_fa.object_field_num = object_field_num imp_fa.save()