Exemple #1
0
    def _find_related_values(self, field_name, sess, value):
        is_related_field = False
        field_mapping = sess.importer.mapping.get(field_name)

        for related_field, relmapper in sess.importer.relation_map_cache.items(
        ):
            if related_field.name != field_name:
                continue

            is_related_field = True
            if isinstance(related_field, ManyToManyField) and value is None:
                return ([], related_field)

            if isinstance(related_field, ForeignKey):
                try:
                    value = int(
                        value)  # this is because xlrd causes 1 to be 1.0
                except Exception:
                    pass
                value = relmapper.fk_cache.get(str(value))
                break
            else:
                value = sess.importer.relation_map_cache.get(
                    field_mapping.get("field")).map_cache[value]
                break

        if field_mapping.get("is_enum_field"):
            field = field_mapping.get("field")
            for k, v in field.get_choices():
                if fold_mapping_name(
                        force_text(v)) == fold_mapping_name(value):
                    value = k
                    break
        return (value, is_related_field)
Exemple #2
0
    def _find_related_values(self, field_name, sess, value):
        is_related_field = False
        if not value:
            return (value, is_related_field)

        field_mapping = sess.importer.mapping.get(field_name)

        for related_field, relmapper in sess.importer.relation_map_cache.items():
            if related_field.name != field_name:
                continue

            is_related_field = True
            if isinstance(related_field, ForeignKey):
                try:
                    value = int(value)  # this is because xlrd causes 1 to be 1.0
                except:
                    pass
                value = relmapper.fk_cache.get(str(value))
                break
            else:
                value = sess.importer.relation_map_cache.get(field_mapping.get("field")).map_cache[value]
                break

        if field_mapping.get("is_enum_field"):
            field = field_mapping.get("field")
            for k, v in field.get_choices():
                if fold_mapping_name(force_text(v)) == fold_mapping_name(value):
                    value = k
                    break
        return (value, is_related_field)
 def _handle_related_value(self, field, mapping, orig_value, row_session, obj, value):
     has_related = False
     if mapping.get("fk"):
         value = self._handle_row_fk_value(field, orig_value, row_session, value)
         if not field.null and value is None:
             has_related = True
     elif mapping.get("m2m"):
         self._handle_row_m2m_value(field, orig_value, row_session, obj, value)
         has_related = True
     elif mapping.get("is_enum_field"):
         for k, v in field.get_choices():
             if fold_mapping_name(force_text(v)) == fold_mapping_name(orig_value):
                 value = k
                 break
     return (value, has_related)
Exemple #4
0
 def _handle_related_value(self, field, mapping, orig_value, row_session, obj, value):
     has_related = False
     if mapping.get("fk"):
         value = self._handle_row_fk_value(field, orig_value, row_session, value)
         if not field.null and value is None:
             has_related = True
     elif mapping.get("m2m"):
         self._handle_row_m2m_value(field, orig_value, row_session, obj, value)
         has_related = True
     elif mapping.get("is_enum_field"):
         for k, v in field.get_choices():
             if fold_mapping_name(force_text(v)) == fold_mapping_name(orig_value):
                 value = k
                 break
     return (value, has_related)
Exemple #5
0
    def map_data_to_fields(self, model_mapping):
        """
        Map fields

        If field is not found it will be saved into unmapped
        :return:
        """
        # reset unmatched here
        self.unmatched_fields = set()

        data_map = {}
        for field_name in sorted(self.data_keys):
            mfname = fold_mapping_name(field_name)
            mapped_value = model_mapping.get(mfname)
            if not mapped_value:
                for fld, opt in six.iteritems(model_mapping):
                    matcher = opt.get("matcher")
                    if matcher and (matcher(field_name) or matcher(mfname)):
                        mapped_value = opt
                        break

            if mapped_value:
                data_map[field_name] = mapped_value
                if mapped_value.get("keyable"):
                    self.unique_fields[field_name] = mapped_value
            elif not mapped_value and not self._meta.has_post_save_handler(
                    field_name):
                self.unmatched_fields.add(field_name)

        self.data_map = data_map
        return data_map
Exemple #6
0
    def create_mapping(self):
        mapping = {}
        aliases = self._meta.field_aliases
        for model in self.get_related_models():
            for field, mode in self._get_fields_with_modes(model):
                map_base = self._get_map_base(field, mode)

                if isinstance(field, RelatedField) and not field.null:
                    map_base["priority"] -= 10

                # Figure out names
                names = [field.name]
                if field.verbose_name:
                    names.append(field.verbose_name)

                # find aliases
                this_aliases = aliases.get(field.name)
                if this_aliases:
                    if isinstance(this_aliases, six.string_types):
                        this_aliases = [this_aliases]
                    names.extend(this_aliases)

                # Assign into mapping
                for name in names:
                    if map_base.get("translated"):
                        mapping[name] = copy_update(map_base,
                                                    lang=self.language)
                    else:
                        mapping[name] = map_base

        mapping = dict((fold_mapping_name(mname), mdata)
                       for (mname, mdata) in six.iteritems(mapping))
        self.mapping = mapping
        return mapping
Exemple #7
0
    def map_data_to_fields(self, model_mapping):
        """
        Map fields

        If field is not found it will be saved into unmapped
        :return:
        """
        # reset unmatched here
        self.unmatched_fields = set()

        data_map = {}

        for field_name in sorted(self.data_keys):
            mfname = fold_mapping_name(field_name)

            mapped_value = model_mapping.get(mfname)
            if not mapped_value:
                for fld, opt in six.iteritems(model_mapping):
                    matcher = opt.get("matcher")
                    if matcher and (matcher(field_name) or matcher(mfname)):
                        mapped_value = opt
                        break

            if mapped_value:
                data_map[field_name] = mapped_value
                if mapped_value.get("keyable"):
                    self.unique_fields[field_name] = mapped_value
            elif not mapped_value and not self._meta.has_post_save_handler(field_name):
                self.unmatched_fields.add(field_name)

        self.data_map = data_map
        return data_map
Exemple #8
0
    def create_mapping(self):
        mapping = {}
        aliases = self._meta.field_aliases
        for model in self.get_related_models():
            for field, mode in self._get_fields_with_modes(model):
                map_base = self._get_map_base(field, mode)

                if isinstance(field, RelatedField) and not field.null:
                    map_base["priority"] -= 10

                # Figure out names
                names = [field.name]
                if field.verbose_name:
                    names.append(field.verbose_name)

                # find aliases
                this_aliases = aliases.get(field.name)
                if this_aliases:
                    if isinstance(this_aliases, six.string_types):
                        this_aliases = [this_aliases]
                    names.extend(this_aliases)

                # Assign into mapping
                for name in names:
                    if map_base.get("translated"):
                        mapping[name] = copy_update(map_base, lang=self.language)
                    else:
                        mapping[name] = map_base

        mapping = dict((fold_mapping_name(mname), mdata) for (mname, mdata) in six.iteritems(mapping))
        self.mapping = mapping
        return mapping