mirror of
https://github.com/c0de-archive/Odoo-Contact-Import.git
synced 2024-12-22 17:12:41 +00:00
Fix if on find_index
This commit is contained in:
parent
8df6301f64
commit
ddd4da9d9d
90
main.py
90
main.py
@ -20,71 +20,71 @@ import re
|
|||||||
|
|
||||||
# Change these paths to reflect your needs
|
# Change these paths to reflect your needs
|
||||||
config = {
|
config = {
|
||||||
'partner_path': 'C:\\Users\\dtodd\\Desktop\\res.partner (2).csv', # This is the csv file containing two columns, id and name
|
'partner_path': 'C:\\Users\\dtodd\\Desktop\\res.partner (2).csv', # This is the csv file containing two columns, id and name
|
||||||
'import_path': 'C:\\Users\\dtodd\\Desktop\\To Import\\MN Companies-50 FTE-HQ one (contacts) Campaign.csv', # This is the csv file containing whatever needs to be imported
|
'import_path': 'C:\\Users\\dtodd\\Desktop\\To Import\\MN Companies-50 FTE-HQ one (contacts) Campaign.csv', # This is the csv file containing whatever needs to be imported
|
||||||
'temp_path': 'C:\\Users\\dtodd\\Desktop\\temp', # This is a csv file that will be deleted on program completion
|
'temp_path': 'C:\\Users\\dtodd\\Desktop\\temp', # This is a csv file that will be deleted on program completion
|
||||||
'column_to_change': 'parent_id/id' # This is the name of the column that you want to change
|
'column_to_change': 'parent_id/id' # This is the name of the column that you want to change
|
||||||
}
|
}
|
||||||
|
|
||||||
# Find the index number of a column in a row and return that
|
# Find the index number of a column in a row and return that
|
||||||
def find_index(row=None, col=None):
|
def find_index(row=None, col=None):
|
||||||
if ((row == None) | (col == None)) | ((type(row) != '' | type(col) != ''):
|
if ((row == None) | (col == None)) | ((type(row) != '') | (type(col) != '')):
|
||||||
raise ValueError('find_index requires both row and col to be set to strings')
|
raise ValueError('find_index requires both row and col to be set to strings')
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
index = None
|
index = None
|
||||||
for i,j in enumerate(row):
|
for i,j in enumerate(row):
|
||||||
if j == col:
|
if j == col:
|
||||||
index = i
|
index = i
|
||||||
break
|
break
|
||||||
return index
|
return index
|
||||||
|
|
||||||
os.rename(config['import_path'], config['import_path']+'.orig') # Store the original import_path in a backup file
|
os.rename(config['import_path'], config['import_path']+'.orig') # Store the original import_path in a backup file
|
||||||
|
|
||||||
with open(config['partner_path'], 'r') as partner_csv:
|
with open(config['partner_path'], 'r') as partner_csv:
|
||||||
reader = csv.reader(partner_csv)
|
reader = csv.reader(partner_csv)
|
||||||
|
|
||||||
first_row = next(reader)
|
first_row = next(reader)
|
||||||
if ('id' not in first_row) | ('name' not in first_row):
|
if ('id' not in first_row) | ('name' not in first_row):
|
||||||
raise IndexError('Either \'id\' or \'name\' is not defined in your partner_path csv file')
|
raise IndexError('Either \'id\' or \'name\' is not defined in your partner_path csv file')
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
id = find_index(first_row, 'id')
|
id = find_index(first_row, 'id')
|
||||||
name = find_index(first_row, 'name')
|
name = find_index(first_row, 'name')
|
||||||
|
|
||||||
if (id == None) | (name == None):
|
if (id == None) | (name == None):
|
||||||
raise ValueError('Either id or name could not be indexed with find_index')
|
raise ValueError('Either id or name could not be indexed with find_index')
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
partner_ids = {rows[id]:rows[name] for rows in reader} # Build a dict containing the company names as the keys and the __export__IDs as values
|
partner_ids = {rows[id]:rows[name] for rows in reader} # Build a dict containing the company names as the keys and the __export__IDs as values
|
||||||
reader = None
|
reader = None
|
||||||
|
|
||||||
# Open both the import_csv and temp_csv files
|
# Open both the import_csv and temp_csv files
|
||||||
with open(config['import_path'], 'r') as import_csv, open(config['temp_path'], 'w') as temp_csv:
|
with open(config['import_path'], 'r') as import_csv, open(config['temp_path'], 'w') as temp_csv:
|
||||||
reader = csv.reader(import_csv)
|
reader = csv.reader(import_csv)
|
||||||
writer = csv.writer(temp_csv)
|
writer = csv.writer(temp_csv)
|
||||||
|
|
||||||
first_row = next(reader)
|
first_row = next(reader)
|
||||||
if config['column_to_change'] not in first_row:
|
if config['column_to_change'] not in first_row:
|
||||||
raise IndexError('\'{}\' is not defined in your import_path csv file').format(config['column_to_change'])
|
raise IndexError('\'{}\' is not defined in your import_path csv file').format(config['column_to_change'])
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
col = find_index(first_row, config['column_to_change'])
|
col = find_index(first_row, config['column_to_change'])
|
||||||
|
|
||||||
if col != None:
|
if col != None:
|
||||||
for lines in reader:
|
for lines in reader:
|
||||||
# Taken from http://stackoverflow.com/a/2400577/5727514 and modified to support what I need
|
# Taken from http://stackoverflow.com/a/2400577/5727514 and modified to support what I need
|
||||||
pattern = re.compile('|'.join(partner_ids.keys()))
|
pattern = re.compile('|'.join(partner_ids.keys()))
|
||||||
lines[col] = pattern.sub(lambda x: partner_ids[x.group()], lines[col])
|
lines[col] = pattern.sub(lambda x: partner_ids[x.group()], lines[col])
|
||||||
|
|
||||||
writer.writerow(lines)
|
writer.writerow(lines)
|
||||||
else:
|
else:
|
||||||
raise ValueError('col could not be indexed with find_index')
|
raise ValueError('col could not be indexed with find_index')
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
# Close our files to be nice to the OS
|
# Close our files to be nice to the OS
|
||||||
temp_csv.close()
|
temp_csv.close()
|
||||||
import_csv.close()
|
import_csv.close()
|
||||||
partner_csv.close()
|
partner_csv.close()
|
||||||
|
|
||||||
# On program completion, move the temp file to the original file
|
# On program completion, move the temp file to the original file
|
||||||
|
Loading…
Reference in New Issue
Block a user