Add support for 2nd "member type" file in CORE member batch

still just feature preview mode here, this is terribly specific
This commit is contained in:
Lance Edgar 2021-11-05 20:35:38 -05:00
parent 0c817fb7d0
commit 1c9f1ef515

View file

@ -43,17 +43,6 @@ class CoreMemberBatchHandler(BatchHandler):
"""
batch_model_class = CoreMemberBatch
importing_fields = [
'first_name',
'last_name',
'street',
'city',
'state',
'zipcode',
'phone',
'email1',
]
def should_populate(self, batch):
if batch.input_file:
return True
@ -81,8 +70,11 @@ class CoreMemberBatchHandler(BatchHandler):
"""
Populate member batch from input data file.
"""
# TODO: should detect what type of input file we have, but for
# now we only support one kind..
# TODO: how should we detect what type of input file we have?
# this way is pretty lame but sort of works for testing
if 'member-status' in batch.input_file:
return self.populate_from_member_file(batch, progress=progress)
return self.populate_from_contact_file(batch, progress=progress)
def populate_from_contact_file(self, batch, progress=None):
@ -95,10 +87,21 @@ class CoreMemberBatchHandler(BatchHandler):
data = list(reader)
input_file.close()
batch.set_param('fields', self.importing_fields)
fields = [
'first_name',
'last_name',
'street',
'city',
'state',
'zipcode',
'phone',
'email1',
]
batch.set_param('fields', fields)
maxlens = {}
for field in self.importing_fields:
for field in fields:
maxlens[field] = maxlen(getattr(CoreMemberBatchRow, field))
def append(csvrow, i):
@ -115,7 +118,7 @@ class CoreMemberBatchHandler(BatchHandler):
# row.phone = self.app.format_phone_number(csvrow['phone_number'])
row.email1 = csvrow['email']
for field in self.importing_fields:
for field in fields:
if len(getattr(row, field)) > maxlens[field]:
log.warning("%s field is %s and will be truncated to %s "
"for row #%s in CSV data: %s",
@ -132,7 +135,37 @@ class CoreMemberBatchHandler(BatchHandler):
self.progress_loop(append, data, progress,
message="Adding initial rows to batch")
def populate_from_member_file(self, batch, progress=None):
"""
Populate member batch from "member" CSV input data file.
"""
input_path = batch.filepath(self.config, batch.input_file)
input_file = open(input_path, 'rt')
reader = csv.DictReader(input_file)
data = list(reader)
input_file.close()
fields = [
'first_name',
'last_name',
'member_type_id',
]
batch.set_param('fields', fields)
def append(csvrow, i):
row = self.make_row()
row.card_number = int(csvrow['signup_external_id'])
row.first_name = csvrow['signup_first_name']
row.last_name = csvrow['signup_last_name']
row.member_type_id = int(csvrow['member_type'])
self.add_row(batch, row)
self.progress_loop(append, data, progress,
message="Adding initial rows to batch")
def refresh_row(self, row):
batch = row.batch
# clear these first in case they are set
row.first_name_old = None
@ -174,7 +207,8 @@ class CoreMemberBatchHandler(BatchHandler):
row.member_type_id_old = core_customer.member_type_id
diffs = []
for field in self.importing_fields:
fields = batch.get_param('fields')
for field in fields:
if getattr(row, field) != getattr(row, '{}_old'.format(field)):
diffs.append(field)
@ -241,6 +275,10 @@ class CoreMemberBatchHandler(BatchHandler):
if 'last_name' in fields:
core_customer.last_name = row.last_name
if 'member_type_id' in fields:
for core_customer in core_member.customers:
core_customer.member_type_id = row.member_type_id
self.progress_loop(update, rows, progress,
message="Updating members in CORE-POS")