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

Allow overwriting part names #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 10 additions & 4 deletions inventree_part_import/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ 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("--overwrite-part-name", help=(
"Override name of the part related to the search results. "
"If multiple search terms are given, all resulting manufacturer/supplier parts "
"will point to the same part."
))
@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 @@ -74,6 +79,7 @@ def inventree_part_import(
dry=False,
config_dir=False,
verbose=False,
overwrite_part_name=None,
show_config_dir=False,
configure=None,
update=None,
Expand Down Expand Up @@ -203,9 +209,9 @@ def inventree_part_import(
try:
for index, part in enumerate(parts):
last_import_result = (
importer.import_part(part.name, part, supplier, only_supplier)
importer.import_part(part.name, part, supplier, only_supplier, overwrite_part_name)
if isinstance(part, Part) else
importer.import_part(part, None, supplier, only_supplier)
importer.import_part(part, None, supplier, only_supplier, overwrite_part_name)
)
print()
match last_import_result:
Expand All @@ -227,9 +233,9 @@ def inventree_part_import(
importer.interactive = True
for part in parts2:
import_result = (
importer.import_part(part.name, part, supplier, only_supplier)
importer.import_part(part.name, part, supplier, only_supplier, overwrite_part_name)
if isinstance(part, Part) else
importer.import_part(part, None, supplier, only_supplier)
importer.import_part(part, None, supplier, only_supplier, overwrite_part_name)
)
match import_result:
case ImportResult.ERROR | ImportResult.FAILURE:
Expand Down
13 changes: 8 additions & 5 deletions inventree_part_import/part_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ def import_part(
search_term,
existing_part: Part = None,
supplier_id=None,
only_supplier=False
only_supplier=False,
overwrite_part_name=None
):
info(f"searching for {search_term} ...", end="\n")
import_result = ImportResult.SUCCESS
Expand All @@ -68,6 +69,8 @@ def import_part(

if len(results) == 1:
api_part = results[0]
if overwrite_part_name is not None:
api_part.name = overwrite_part_name
elif self.interactive:
prompt(f"found multiple parts at {supplier.name}, select which one to import")
results = results[:get_config()["max_results"]]
Expand Down Expand Up @@ -167,7 +170,7 @@ def import_supplier_part(self, supplier: Company, api_part: ApiPart, part: Part
if update_part:
if not api_part.finalize():
return ImportResult.FAILURE
update_object_data(part, api_part.get_part_data(), f"part {api_part.MPN}")
update_object_data(part, api_part.get_part_data(), f"part {api_part.name}")

if not part.image and api_part.image_url:
upload_image(part, api_part.image_url)
Expand Down Expand Up @@ -217,8 +220,8 @@ def create_manufacturer_part(
part: Part = None,
) -> tuple[ManufacturerPart, Part]:
part_data = api_part.get_part_data()
if part or (part := get_part(self.api, api_part.MPN)):
update_object_data(part, part_data, f"part {api_part.MPN}")
if part or (part := get_part(self.api, api_part.name)):
update_object_data(part, part_data, f"part {api_part.name}")
else:
for subcategory in reversed(api_part.category_path):
if category := self.category_map.get(subcategory.lower()):
Expand All @@ -236,7 +239,7 @@ def create_manufacturer_part(
category.add_alias(api_part.category_path[-1])
self.category_map[api_part.category_path[-1].lower()] = category

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

manufacturer = create_manufacturer(self.api, api_part.manufacturer)
Expand Down
3 changes: 2 additions & 1 deletion inventree_part_import/suppliers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

@dataclass
class ApiPart:
name: str
description: str
image_url: str
datasheet_url: str
Expand Down Expand Up @@ -35,7 +36,7 @@ def finalize_hook(self):

def get_part_data(self):
return {
"name": self.MPN,
"name": self.name,
"description": self.description,
"link": self.manufacturer_link[:200],
"active": True,
Expand Down
3 changes: 2 additions & 1 deletion inventree_part_import/suppliers/supplier_digikey.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def get_api_part(self, digikey_part):
}

return ApiPart(
name=digikey_part.manufacturer_part_number,
description=digikey_part.product_description,
image_url=digikey_part.primary_photo,
datasheet_url=digikey_part.primary_datasheet,
Expand All @@ -109,5 +110,5 @@ def get_api_part(self, digikey_part):
category_path=category_path,
parameters=parameters,
price_breaks=price_breaks,
currency=self.currency,
currency=self.currency
)
1 change: 1 addition & 0 deletions inventree_part_import/suppliers/supplier_lcsc.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def get_api_part(self, lcsc_part):
currency = self.currency

return ApiPart(
name=lcsc_part.get("productModel", ""),
description=REMOVE_HTML_TAGS.sub("", description),
image_url=image_url,
datasheet_url=lcsc_part.get("pdfUrl"),
Expand Down
1 change: 1 addition & 0 deletions inventree_part_import/suppliers/supplier_mouser.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def get_api_part(self, mouser_part):
quantity_available = 0

api_part = ApiPart(
name=mouser_part.get("ManufacturerPartNumber", ""),
description=REMOVE_HTML_TAGS.sub("", mouser_part.get("Description", "")),
image_url=mouser_part.get("ImagePath"),
datasheet_url=mouser_part.get("DataSheetUrl"),
Expand Down
1 change: 1 addition & 0 deletions inventree_part_import/suppliers/supplier_reichelt.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def get_api_part(self, soup, sku, link):
currency = meta["content"]

return ApiPart(
name=mpn,
description=description,
image_url=image_url,
datasheet_url=datasheet_url,
Expand Down
1 change: 1 addition & 0 deletions inventree_part_import/suppliers/supplier_tme.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def get_api_part(self, tme_part, tme_stock):
}

api_part = ApiPart(
name=tme_part.get("OriginalSymbol", ""),
description=tme_part.get("Description", ""),
image_url=fix_tme_url(tme_part.get("Photo")),
datasheet_url=None,
Expand Down