def get_column_mapping(column_raw, organization, attr_name='column_mapped'): """Callable provided to MCM to return a previously mapped field. :param column_raw: str, the column name of the raw data. :param organization: Organization inst. :param attr_name: str, name of attribute on ColumnMapping to pull out. whether we're looking at a mapping from the perspective of a raw_column (like we do when creating a mapping), or mapped_column, (like when we're applying that mapping). :returns: list of mapped items, float repreresentation of confidence. """ from seed.utils.mapping import _get_column_names if not isinstance(column_raw, list): column_raw = [column_raw] cols = Column.objects.filter( organization=organization, column_name__in=column_raw ) try: previous_mapping = ColumnMapping.objects.get( super_organization=organization, column_raw__in=cols, ) except ColumnMapping.DoesNotExist: return None column_names = _get_column_names(previous_mapping, attr_name=attr_name) if previous_mapping.is_direct(): column_names = column_names[0] return column_names, 100
def get_column_mappings(organization): """Returns dict of all the column mappings for an Org's given source type :param organization: inst, Organization. :returns dict, list of dict: Use this when actually performing mapping between datasources, but only call it after all of the mappings have been saved to the ``ColumnMapping`` table. """ from seed.utils.mapping import _get_column_names source_mappings = ColumnMapping.objects.filter( super_organization=organization ) concat_confs = [] mapping = {} for item in source_mappings: if not item.column_mapped.all().exists(): continue key = _get_column_names(item) value = _get_column_names(item, attr_name='column_mapped')[0] if isinstance(key, list) and len(key) > 1: concat_confs.append({ 'concat_columns': key, 'target': value, 'delimiter': ' ' }) continue # These should be lists of one element each. mapping[key[0]] = value return mapping, concat_confs