def create_model(self, data_start_row=1): code = self.get_mapping_and_attr() attr_order = None mapping = None exec code res = [u"", u"", u"# @python_2_unicode_compatible", u"class %s(models.Model):" % self.class_name] for key in attr_order: if key == "": continue help_text = get_valid_excel_field_name(key) first_part = u'%s%s = models.CharField(max_length=%s, help_text=_("""' % \ (self.indents, mapping[key], self.field_len_definition) declaration = first_part + help_text + u'"""), null=True, blank=True, verbose_name="""'+help_text+'""")' res.append(declaration) res.append(self.indents + u'data_import_id = models.CharField(max_length=TEXT_LENGTH_128, ' u'help_text=_("Id for this import"), null=True, blank=True)') res.append(self.indents + u'') res.append(self.indents + u'# def __str__(self):') res.append(self.indents + u'# ' + self.indents + u'return "%s" % self.name') self.model_code_lines = res return u"\n".join(res)
def create_field_title_to_attr_name_mapping(self): for col in self.header: if col == "": self.mapping_dict[u""] = self.invalid_field_name else: field_title_str = get_valid_excel_field_name(col) model_attribute_name = self.get_attribute_name(col) self.mapping_dict[field_title_str] = model_attribute_name
def create_attr_list_code_lines(self): self.attr_list_code_lines.append("attr_order = [") for col in self.header: name = get_valid_excel_field_name(unicode(col)) if not (name in self.attr_list): self.attr_list.append(name) self.attr_list_code_lines.append(self.indents + 'u"""%s""",' % name) self.attr_list_code_lines.append("]")
def _get_column_to_db_field_mapping(self, data_source): column_to_db_field_mapping = {} for column_name in data_source.get_headers(): if column_name in self.model_module.mapping: column_to_db_field_mapping[column_name] = self.model_module.mapping[column_name] else: converted_field_name = get_valid_excel_field_name(column_name) if converted_field_name in self.model_module.mapping: column_to_db_field_mapping[column_name] = \ self.model_module.mapping[converted_field_name] return column_to_db_field_mapping
def get_mapped_columns(self, line, mapping): value_list = line.split(';') if len(self.title_columns) != len(value_list): raise InconsistentLineLength('Line length is not consistent with header: %s' % line) res = {} for i in range(self.title_column_len): src_key = unicode(self.title_columns[i]) try: mapping_key = mapping[src_key] except KeyError: mapping_key = mapping[get_valid_excel_field_name(src_key)] val = value_list[i] res[mapping_key] = val return res