Skip to content

Commit

Permalink
Change codes from Python 3.x.
Browse files Browse the repository at this point in the history
Code refine.
  • Loading branch information
Richard committed Aug 14, 2019
1 parent 1d1ecce commit 38b14dd
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 22 deletions.
2 changes: 1 addition & 1 deletion django_excel_to_model/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import models
from . import models
from djangoautoconf.auto_conf_admin_tools.admin_register import AdminRegister
factory = AdminRegister()

Expand Down
2 changes: 1 addition & 1 deletion django_excel_to_model/django_app_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self, app_name):
'''
self.urls_content = '''from djangoautoconf.model_utils.url_for_models import add_all_urls
from django.conf.urls import patterns
import models
from . import models
urlpatterns = patterns('',)
Expand Down
7 changes: 7 additions & 0 deletions django_excel_to_model/field_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,10 @@ def get_valid_excel_field_name(col):
# Only ascii supported here
return get_string_with_only_char_in_list(col, var_name_char_list + " \t")


def get_db_field(mapping, src_key):
try:
target_key = mapping[src_key]
except KeyError:
target_key = mapping[get_valid_excel_field_name(src_key)]
return target_key
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@
from django.contrib.contenttypes.models import ContentType
from django.core.management.base import BaseCommand

from django_excel_to_model.field_tools import get_valid_excel_field_name
from django_excel_to_model.file_readers.csv_reader import CsvFile
from django_excel_to_model.management.commands.utils.bulk_inserter import BulkInserter
from django_excel_to_model.management.commands.utils.counter import Counter
from django_excel_to_model.models import ExcelImportTask
from django_excel_to_model.reader import ExcelFile, XlsbFile

try:
from pinax.eventlog.models import log
except:
except ImportError:
log = None


class MandatoryColumnMissing(Exception):
pass


class DictTranslator(object):
# noinspection PyMethodMayBeStatic
def translate(self, item_dict):
Expand All @@ -32,6 +38,7 @@ def __init__(self, class_instance, sheet_numbered_from_1=1):
self.translator = DictTranslator()
self.sheet_numbered_from_1 = sheet_numbered_from_1
self.inserter = BulkInserter(self.class_instance)
self.mandatory_column_headers = None

def import_excel(self, full_path, header_row_numbered_from_1, first_import_row_numbered_from_1=1, count=1000):
if 'xlsb' in full_path:
Expand All @@ -41,13 +48,24 @@ def import_excel(self, full_path, header_row_numbered_from_1, first_import_row_n
else:
excel_file = CsvFile(full_path)
filename = os.path.basename(full_path)
sheet = excel_file.get_sheet(self.sheet_numbered_from_1-1)
sheet = excel_file.get_sheet(self.sheet_numbered_from_1 - 1)
sheet.init_header_raw(header_row_numbered_from_1 - 1)
# for class_instance in class_enumerator(self.model_module):
# new_item_class = class_instance
c = Counter(count)

for item_info_dict in sheet.enumerate_mapped(self.model_module.mapping,
self.validate_existence_of_mandatory_columns(sheet)

column_to_db_field_mapping = {}

for column_name in sheet.get_title_columns():
if column_name in self.model_module.mapping:
column_to_db_field_mapping[column_name] = self.model_module.mapping[column_name]
elif get_valid_excel_field_name(column_name) in self.model_module.mapping:
column_to_db_field_mapping[column_name] = \
self.model_module.mapping[get_valid_excel_field_name(column_name)]

for item_info_dict in sheet.enumerate_mapped(column_to_db_field_mapping,
start_row=first_import_row_numbered_from_1):
# print item_info_dict
self.translator.translate(item_info_dict)
Expand All @@ -61,6 +79,12 @@ def import_excel(self, full_path, header_row_numbered_from_1, first_import_row_n
self.commit_and_log(filename)
return -1

def validate_existence_of_mandatory_columns(self, sheet):
if self.mandatory_column_headers is not None:
if all(spreadsheet_column_header in sheet.get_title_columns()
for spreadsheet_column_header in self.mandatory_column_headers):
raise MandatoryColumnMissing()

def commit_and_log(self, filename):
self.inserter.commit()
if log is not None:
Expand Down
25 changes: 8 additions & 17 deletions django_excel_to_model/reader.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# -*- coding: utf-8 -*-
from datetime import datetime, tzinfo
from datetime import datetime
import pytz
import pyxlsb
import codecs
from xlrd import open_workbook, cellname, XL_CELL_DATE, xldate_as_tuple, XL_CELL_NUMBER
from xlrd import open_workbook, XL_CELL_DATE, xldate_as_tuple, XL_CELL_NUMBER
from django.utils import timezone
from field_tools import get_valid_excel_field_name
from django_excel_to_model.field_tools import get_db_field


class ExcelReaderBaseException(Exception):
Expand Down Expand Up @@ -79,10 +78,7 @@ def get_mapped_columns(self, row_index, mapping):
for r in row:
if r.v is not None and r.r== row_index:
src_key = unicode(self.title_columns[r.c])
try:
target_key = mapping[src_key]
except KeyError:
target_key = mapping[get_valid_excel_field_name(src_key)]
target_key = get_db_field(mapping, src_key)
cell = r
res[target_key] = self.parse_cell_value(cell)
return res
Expand All @@ -95,10 +91,7 @@ def enumerate_mapped(self, mapping, start_row=1):
if r.r % 1000 == 0:
print r, r.r
src_key = unicode(self.title_columns[r.c])
try:
target_key = mapping[src_key]
except KeyError:
target_key = mapping[get_valid_excel_field_name(src_key)]
target_key = get_db_field(mapping, src_key)
res[target_key] = self.parse_cell_value(r)
yield res

Expand Down Expand Up @@ -139,10 +132,7 @@ def get_mapped_columns(self, row_index, mapping):
# print self.sheet.cell(row_index, col_index).value
src_key = unicode(self.title_columns[col_index])
# print "___________________", src_key
try:
target_key = mapping[src_key]
except KeyError:
target_key = mapping[get_valid_excel_field_name(src_key)]
target_key = get_db_field(mapping, src_key)
cell = self.sheet.cell(row_index, col_index)
res[target_key] = self.parse_cell_value(cell)
return res
Expand Down Expand Up @@ -226,7 +216,8 @@ def get_header_to_column_dict(self, row_index):
return res

def init_header_raw(self, header_row_index=0):
self.title_columns = self.get_columns(header_row_index)
if self.title_columns is None:
self.title_columns = self.get_columns(header_row_index)
return self.title_columns

def get_headers(self):
Expand Down

0 comments on commit 38b14dd

Please sign in to comment.