Fix order of operations

This commit is contained in:
Oliver Walters 2020-04-13 23:19:23 +10:00
parent fb8c0e5180
commit 3b08b962c1

View File

@ -93,23 +93,19 @@ def associate_manufacturers(apps, schema_editor):
for company in Company.objects.all(): for company in Company.objects.all():
companies[company.name] = company companies[company.name] = company
# List of parts which will need saving
parts = []
def link_part(part, name): def link_part(part, name):
""" Attempt to link Part to an existing Company """ """ Attempt to link Part to an existing Company """
# Matches a company name directly # Matches a company name directly
if name in companies.keys(): if name in companies.keys():
print(" -> '{n}' maps to existing manufacturer".format(n=name)) print(" - Part[{pk}]: '{n}' maps to existing manufacturer".format(pk=part.pk, n=name))
part.manufacturer = companies[name] part.manufacturer = companies[name]
part.save() part.save()
return True return True
# Have we already mapped this # Have we already mapped this
if name in links.keys(): if name in links.keys():
print(" -> Mapped '{n}' -> '{c}'".format(n=name, c=links[name].name)) print(" - Part[{pk}]: Mapped '{n}' - '{c}'".format(pk=part.pk, n=name, c=links[name].name))
part.manufacturer = links[name] part.manufacturer = links[name]
part.save() part.save()
return True return True
@ -124,22 +120,21 @@ def associate_manufacturers(apps, schema_editor):
company.is_manufacturer = True company.is_manufacturer = True
# Save the company BEFORE we associate the part, otherwise the PK does not exist
company.save()
# Map both names to the same company # Map both names to the same company
links[input_name] = company links[input_name] = company
links[company_name] = company links[company_name] = company
companies[company_name] = company companies[company_name] = company
# Save the company BEFORE we associate the part, otherwise the PK does not exist print(" - Part[{pk}]: Created new manufacturer: '{name}'".format(pk=part.pk, name=company_name))
company.save()
# Save the manufacturer reference link # Save the manufacturer reference link
part.manufacturer = company part.manufacturer = company
part.save() part.save()
print(" -> Created new manufacturer: '{name}'".format(name=company_name))
def find_matches(text, threshold=65): def find_matches(text, threshold=65):
""" """
Attempt to match a 'name' to an existing Company. Attempt to match a 'name' to an existing Company.
@ -167,6 +162,7 @@ def associate_manufacturers(apps, schema_editor):
# Skip empty names # Skip empty names
if not name or len(name) == 0: if not name or len(name) == 0:
print(" - Part[{pk}]: No manufacturer_name provided, skipping".format(pk=part.pk))
return return
# Can be linked to an existing manufacturer # Can be linked to an existing manufacturer
@ -180,7 +176,7 @@ def associate_manufacturers(apps, schema_editor):
# Present a list of options # Present a list of options
print("----------------------------------") print("----------------------------------")
print("Checking part {idx} of {total}".format(idx=idx+1, total=total)) print("Checking part [{pk}] ({idx} of {total})".format(pk=part.pk, idx=idx+1, total=total))
print("Manufacturer name: '{n}'".format(n=name)) print("Manufacturer name: '{n}'".format(n=name))
print("----------------------------------") print("----------------------------------")
print("Select an option from the list below:") print("Select an option from the list below:")
@ -194,8 +190,7 @@ def associate_manufacturers(apps, schema_editor):
print("") print("")
print("OR - Type a new custom manufacturer name") print("OR - Type a new custom manufacturer name")
while True:
while (1):
response = str(input("> ")).strip() response = str(input("> ")).strip()
# Attempt to parse user response as an integer # Attempt to parse user response as an integer
@ -208,7 +203,7 @@ def associate_manufacturers(apps, schema_editor):
create_manufacturer(part, name, name) create_manufacturer(part, name, name)
return return
# Options 1) -> n) select an existing manufacturer # Options 1) - n) select an existing manufacturer
else: else:
n = n - 1 n = n - 1
@ -229,7 +224,7 @@ def associate_manufacturers(apps, schema_editor):
links[name] = company links[name] = company
links[company_name] = company links[company_name] = company
print(" -> Linked '{n}' to manufacturer '{m}'".format(n=name, m=company_name)) print(" - Part[{pk}]: Linked '{n}' to manufacturer '{m}'".format(pk=part.pk, n=name, m=company_name))
return return
else: else:
@ -281,11 +276,10 @@ def associate_manufacturers(apps, schema_editor):
for idx, part in enumerate(SupplierPart.objects.all()): for idx, part in enumerate(SupplierPart.objects.all()):
if part.manufacturer is not None: if part.manufacturer is not None:
print(" -> Part '{p}' already has a manufacturer associated (skipping)".format(p=part)) print(" - Part '{p}' already has a manufacturer associated (skipping)".format(p=part))
continue continue
map_part_to_manufacturer(part, idx, part_count) map_part_to_manufacturer(part, idx, part_count)
parts.append(part)
print("Done!") print("Done!")