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