30350n / inventree_part_import

CLI to import parts from suppliers like DigiKey, LCSC, Mouser, etc. to InvenTree
MIT License
24 stars 8 forks source link

FR: Import supplier part with unknown manufacturer part #30

Closed ondras12345 closed 3 months ago

ondras12345 commented 3 months ago

TME lists some parts that do not have a manufacturer part number. Example: https://www.tme.eu/en/details/lam100x160ex1.5/two-layers-laminates/ I reported this to TME for parts where it was obviously a mistake (e.g. when there was a datasheet attached that clearly showed the manufacturer part number), but I keep finding more and more parts that have this problem, so I think it would be useful if it was possible to import them anyway. Btw I believe they might be doing it on purpose for some parts, e.g. the copper clad I linked above. Maybe they use a mix of manufacturer parts for the same supplier part.

This is what happens when I try to import such a part:

$ inventree-part-import -v -o tme LAM100X160EX1.5
setting up InvenTree API ...
loading api configuration from 'inventree.yaml' ...
(only 2 of 5 available supplier modules are configured)
loading pre creation hooks ...
successfully loaded 1 pre creation hooks!
setting up categories ...
successfully setup categories!

searching for LAM100X160EX1.5 ...
searching at TME ...
importing TME part LAM100X160EX1.5 ...
Traceback (most recent call last):
  File "/home/ondra/.local/bin/inventree-part-import", line 8, in <module>
    sys.exit(inventree_part_import())
  File "/home/ondra/.local/pipx/venvs/inventree-part-import/lib/python3.10/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
  File "/home/ondra/.local/pipx/venvs/inventree-part-import/lib/python3.10/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
  File "/home/ondra/.local/pipx/venvs/inventree-part-import/lib/python3.10/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/ondra/.local/pipx/venvs/inventree-part-import/lib/python3.10/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
  File "/home/ondra/.local/pipx/venvs/inventree-part-import/lib/python3.10/site-packages/click/decorators.py", line 33, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/home/ondra/.local/pipx/venvs/inventree-part-import/lib/python3.10/site-packages/inventree_part_import/cli.py", line 23, in wrapper
    func(*args, **kwargs)
  File "/home/ondra/.local/pipx/venvs/inventree-part-import/lib/python3.10/site-packages/inventree_part_import/cli.py", line 208, in inventree_part_import
    importer.import_part(part, None, supplier, only_supplier)
  File "/home/ondra/.local/pipx/venvs/inventree-part-import/lib/python3.10/site-packages/inventree_part_import/part_importer.py", line 86, in import_part
    import_result |= self.import_supplier_part(supplier, api_part, existing_part)
  File "/home/ondra/.local/pipx/venvs/inventree-part-import/lib/python3.10/site-packages/inventree_part_import/part_importer.py", line 155, in import_supplier_part
    elif manufacturer_part := get_manufacturer_part(self.api, api_part.MPN):
  File "/home/ondra/.local/pipx/venvs/inventree-part-import/lib/python3.10/site-packages/inventree_part_import/inventree_helpers.py", line 36, in get_manufacturer_part
    assert len(manufacturer_parts) == 0
AssertionError
30350n commented 3 months ago

Went with what's probably the simplest fix: If there's no MPN, just use the TME SKU.

Thanks for your donation btw! 😊

ondras12345 commented 3 months ago

Thanks! That did the trick for parts that were missing MPN but had a valid manufacturer (e.g. https://www.tme.eu/en/details/to220-set/heatsinks-equipment/stonecold/).

However, that copper clad still fails to import, because it has an empty manufacturer name:

$ inventree-part-import -v -o tme LAM100X160EX1.5
setting up InvenTree API ...
loading api configuration from 'inventree.yaml' ...
(only 2 of 5 available supplier modules are configured)
loading pre creation hooks ...
successfully loaded 1 pre creation hooks!
setting up categories ...
[...]
successfully setup categories!

searching for LAM100X160EX1.5 ...
searching at TME ...
importing TME part LAM100X160EX1.5 ...
creating part LAM100X160EX1.5 in 'PCBs/Copper Laminates' ...
creating manufacturer '' ...
error: failed to import part with:
    name: ['This field may not be blank.']

FULL TRACEBACK:
Traceback (most recent call last):
  File "/home/ondra/.local/pipx/venvs/inventree-part-import/lib/python3.10/site-packages/inventree_part_import/part_importer.py", line 86, in import_part
    import_result |= self.import_supplier_part(supplier, api_part, existing_part)
  File "/home/ondra/.local/pipx/venvs/inventree-part-import/lib/python3.10/site-packages/inventree_part_import/part_importer.py", line 162, in import_supplier_part
    result = self.create_manufacturer_part(api_part, part)
  File "/home/ondra/.local/pipx/venvs/inventree-part-import/lib/python3.10/site-packages/inventree_part_import/part_importer.py", line 252, in create_manufacturer_part
    manufacturer = create_manufacturer(self.api, api_part.manufacturer)
  File "/home/ondra/.local/pipx/venvs/inventree-part-import/lib/python3.10/site-packages/inventree_part_import/inventree_helpers.py", line 98, in create_manufacturer
    return InventreeCompany.create(inventree_api, {
  File "/home/ondra/.local/pipx/venvs/inventree-part-import/lib/python3.10/site-packages/inventree/base.py", line 166, in create
    response = api.post(cls.URL, data, **kwargs)
  File "/home/ondra/.local/pipx/venvs/inventree-part-import/lib/python3.10/site-packages/inventree/api.py", line 426, in post
    response = self.request(
  File "/home/ondra/.local/pipx/venvs/inventree-part-import/lib/python3.10/site-packages/inventree_part_import/retries.py", line 68, in request
    return super().request(api_url, **kwargs)
  File "/home/ondra/.local/pipx/venvs/inventree-part-import/lib/python3.10/site-packages/inventree/api.py", line 378, in request
    raise requests.exceptions.HTTPError(detail)
requests.exceptions.HTTPError: {'detail': 'Error occurred during API request', 'url': 'http://inventree.rpi.lab.lan/api/company/', 'method': 'POST', 'status_code': 400, 'body': '{"name":["This field may not be blank."]}', 'headers': {'AUTHORIZATION': 'Token [REDACTED]'}, 'params': {'format': 'json'}, 'data': {'name': '', 'description': '', 'is_manufacturer': True, 'is_supplier': False, 'is_customer': False}}

the following parts failed to import:
LAM100X160EX1.5
30350n commented 3 months ago

Should be fixed now!

ondras12345 commented 3 months ago

Thanks, that did it