Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally set Part IPN to search term (SKU/MPN) when creating new parts #38 #39

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion inventree_part_import/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def wrapper(*args, **kwargs):
@click.option("-d", "--dry", is_flag=True, help="Run without modifying InvenTree database.")
@click.option("-c", "--config-dir", help="Override path to config directory.")
@click.option("-v", "--verbose", is_flag=True, help="Enable verbose output for debugging.")
@click.option("--ipn", is_flag=True, help="When import creates a new part, sets the IPN to the search part number.")
@click.option("--show-config-dir", is_flag=True, help="Show path to config directory and exit.")
@click.option("--configure", type=AvailableSuppliersChoices, help="Configure supplier.")
@click.option("--update", metavar="CATEGORY", help="Update all parts from InvenTree CATEGORY.")
Expand All @@ -72,6 +73,7 @@ def inventree_part_import(
only=None,
interactive="false",
dry=False,
ipn=False,
config_dir=False,
verbose=False,
show_config_dir=False,
Expand Down Expand Up @@ -147,6 +149,9 @@ def inventree_part_import(

if not verbose:
error_helper.INFO_END = "\r"

if ipn:
hint("--ipn will set new parts IPN to the search part number.")

if dry:
warning(DRY_MODE_WARNING, prefix="")
Expand Down Expand Up @@ -191,7 +196,7 @@ def inventree_part_import(
# make sure suppliers.yaml exists
get_suppliers(reload=True)
setup_supplier_companies(inventree_api)
importer = PartImporter(inventree_api, interactive=interactive == "true", verbose=verbose)
importer = PartImporter(inventree_api, interactive=interactive == "true", verbose=verbose, ipn=ipn)

if update or update_recursive:
info(f"updating {len(parts)} parts from '{category_path}'", end="\n")
Expand Down
5 changes: 3 additions & 2 deletions inventree_part_import/part_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ def __or__(self, other):
return self if self.value < other.value else other

class PartImporter:
def __init__(self, inventree_api, interactive=False, verbose=False):
def __init__(self, inventree_api, interactive=False, verbose=False, ipn=False):
self.api = inventree_api
self.interactive = interactive
self.verbose = verbose
self.dry_run = hasattr(inventree_api, "DRY_RUN")
self.ipn = ipn

# preload pre_creation_hooks
get_pre_creation_hooks()
Expand Down Expand Up @@ -247,7 +248,7 @@ def create_manufacturer_part(
self.category_map[api_part.category_path[-1].lower()] = category

info(f"creating part {api_part.MPN} in '{category.part_category.pathstring}' ...")
part = Part.create(self.api, {"category": category.part_category.pk, **part_data})
part = Part.create(self.api, {"category": category.part_category.pk, **({"IPN": api_part.SKU} if self.api else {}),**part_data})

manufacturer = create_manufacturer(self.api, api_part.manufacturer)
info(f"creating manufacturer part {api_part.MPN} ...")
Expand Down