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

Support for part-id fallback #64

Merged
merged 1 commit into from
Dec 15, 2023
Merged
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
51 changes: 34 additions & 17 deletions inventree_kicad/KiCadLibraryPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ class KiCadLibraryPlugin(UrlsMixin, AppMixin, SettingsMixin, SettingsContentMixi
'description': _('This identifier specifies what key the import tool looks for to get the part ID'),
'default': "InvenTree"
},
'IMPORT_INVENTREE_ID_FALLBACK': {
'name': _('Also Match Against Part Name'),
'description': _(
'When activated, the import tool will use the part name as fallback if the ID does not return an existing part.'),
'validator': bool,
'default': False,
},
}

def get_settings_content(self, request):
Expand Down Expand Up @@ -249,7 +256,7 @@ def import_meta_data(self, request): # noqa

symbol = f'{lib_name}:{lib_part}'

inventree_id = None
inventree_part_id = None

# check if there are fields, some parts may not have any like fiducials.
fields = comp.find('fields')
Expand All @@ -262,34 +269,44 @@ def import_meta_data(self, request): # noqa

for field in fields:
if str(field.attrib.get('name', '')).lower().startswith(inventree_id_identifier.lower()):
inventree_id = field.text
inventree_part_id = field.text
break

# Missing inventree_id, cannot continue
if not inventree_id:
logger.debug('Missing inventree_id, skipping')
continue

try:
inventree_id = int(inventree_id)
except ValueError:
logger.debug('InvenTree ID is not an integer, skipping')
if not inventree_part_id:
logger.debug('Missing part id, skipping')
continue

# Already checked this one
if inventree_id in inventree_parts:
if inventree_part_id in inventree_parts:
continue

# add to our cache, we use this to not add the same data multiple times
inventree_parts.add(inventree_id)
inventree_parts.add(inventree_part_id)

# try load part from database
part = None
try:
part = Part.objects.get(id=inventree_id)
part = Part.objects.get(id=inventree_part_id)

except Part.DoesNotExist:
logger.debug(f'Part ID: {inventree_id} does not belong to an existing part, skipping')
continue
invalid_part = True

# try also the part name if user wants it
if self.get_setting('IMPORT_INVENTREE_ID_FALLBACK', None):
try:
part = Part.objects.get(name=inventree_part_id)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't inventry_part_id be updated here since it may be used for PartAttachment below?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or use part=part below.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point, thanks

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed!

invalid_part = False

except Part.DoesNotExist:
invalid_part = True

if invalid_part:
logger.debug(f'Part ID: {inventree_part_id} does not belong to an existing part, skipping')
continue

except Exception as exp:
logger.debug(f'Part ID: {inventree_id} caused uknown error {exp}')
logger.debug(f'Part ID: {inventree_part_id} caused uknown error {exp}')
continue

# find and/or add template and value
Expand Down Expand Up @@ -318,7 +335,7 @@ def import_meta_data(self, request): # noqa
try:
# inventree is not happy with urls which are too long, so let's make sure that this
# doesn't prevent us from importing all the following parts.
PartAttachment.objects.get_or_create(part_id=inventree_id, link=datasheet, comment='datasheet')
PartAttachment.objects.get_or_create(part_id=inventree_part_id, link=datasheet, comment='datasheet')
except Exception as exp:
logger.debug(exp)
pass
Expand Down
Loading