Skip to content

Commit

Permalink
Merge pull request #98 from sparkmicro/fix_parameters
Browse files Browse the repository at this point in the history
Update InvenTree API to fix parameters fetching
  • Loading branch information
eeintech committed Aug 15, 2022
2 parents 7dfae9f + f24ac05 commit 84be9dd
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 59 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test_deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ jobs:
INVENTREE_DB_ENGINE: django.db.backends.sqlite3
INVENTREE_DB_NAME: ${{ github.workspace }}/InvenTree/inventree_default_db.sqlite3
INVENTREE_MEDIA_ROOT: ${{ github.workspace }}/InvenTree
INVENTREE_STATIC_ROOT: ${{ github.workspace }}/InvenTree/static
INVENTREE_ENV: 0
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TOKEN_DIGIKEY: ${{ secrets.TOKEN_DIGIKEY }}
Expand All @@ -66,6 +67,7 @@ jobs:
- name: InvenTree setup
run: |
git clone https://github.com/inventree/InvenTree/
mkdir InvenTree/static
cp tests/files/inventree_default_db.sqlite3 InvenTree/
cd InvenTree/ && invoke install && invoke migrate && cd -
- name: Ki-nTree setup
Expand Down
2 changes: 1 addition & 1 deletion kintree/common/part_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def compare(new_part_parameters: dict, db_part_parameters: dict, include_filters
if value != db_part_parameters[parameter]:
return False
except KeyError:
cprint('[INFO]\tWarning: Failed to compare part with database', silent=settings.SILENT)
cprint('[INFO]\tWarning: Failed to compare part with database', silent=settings.HIDE_DEBUG)
return False

return True
Expand Down
45 changes: 26 additions & 19 deletions kintree/database/inventree_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def get_category_parameters(category_id: int) -> list:
category = PartCategory(inventree_api, category_id)

try:
category_templates = category.get_category_parameter_templates(fetch_parent=True)
category_templates = category.getCategoryParameterTemplates(fetch_parent=True)
except AttributeError:
category_templates = None

Expand All @@ -82,7 +82,7 @@ def get_category_parameters(category_id: int) -> list:
if not default_value:
default_value = '-'

parameter_templates.append([template.parameter_template['name'], default_value])
parameter_templates.append([template.getTemplate().name, default_value])

return parameter_templates

Expand Down Expand Up @@ -162,16 +162,19 @@ def fetch_template_name(template_id):
part_info['description'] == part.description and \
part_info['revision'] == part.revision

# Check if new manufacturer part
if not compare:
manufacturer = list(part_info['manufacturer'].keys())[0]
mpn = list(part_info['manufacturer'].values())[0][0]
compare = not is_new_manufacturer_part(manufacturer, mpn)

if compare:
cprint(f'\n[TREE]\tFound part match in database (pk = {part.pk})', silent=settings.HIDE_DEBUG)
cprint(f'[TREE]\tWarning: Found part match in database (pk = {part.pk})', silent=settings.SILENT)
return part.pk

# Check if manufacturer part exists in database
manufacturer = list(part_info['manufacturer'].keys())[0]
mpn = list(part_info['manufacturer'].values())[0][0]
part_pk = is_new_manufacturer_part(manufacturer, mpn, create=False)

if part_pk:
cprint(f'[TREE]\tWarning: Found part with same manufacturer part in database (pk = {part_pk})', silent=settings.SILENT)
return part_pk

cprint('\n[TREE]\tNo match found in database', silent=settings.HIDE_DEBUG)
return 0

Expand Down Expand Up @@ -320,10 +323,13 @@ def get_company_id(company_name: str) -> int:
return 0


def is_new_manufacturer_part(manufacturer_name: str, manufacturer_mpn: str) -> bool:
def is_new_manufacturer_part(manufacturer_name: str, manufacturer_mpn: str, create=True) -> int:
''' Check if InvenTree manufacturer part exists to avoid duplicates '''
global inventree_api

if not manufacturer_name:
return 0

# Fetch all companies
cprint('[TREE]\tFetching manufacturers', silent=settings.HIDE_DEBUG)
company_list = Company.list(inventree_api, is_manufacturer=True, is_customer=False)
Expand All @@ -338,26 +344,27 @@ def is_new_manufacturer_part(manufacturer_name: str, manufacturer_mpn: str) -> b
part_list = None

if part_list is None:
# Create
cprint(f'[TREE]\tCreating new manufacturer "{manufacturer_name}"', silent=settings.SILENT)
create_company(
company_name=manufacturer_name,
manufacturer=True,
)
if create:
# Create manufacturer
cprint(f'[TREE]\tCreating new manufacturer "{manufacturer_name}"', silent=settings.SILENT)
create_company(
company_name=manufacturer_name,
manufacturer=True,
)
# Get all parts
part_list = []

for item in part_list:
try:
if manufacturer_mpn in item.MPN:
cprint(f'[TREE]\t{item.MPN} ?= {manufacturer_mpn} => True', silent=settings.HIDE_DEBUG)
return False
return item.part
else:
cprint(f'[TREE]\t{item.MPN} ?= {manufacturer_mpn} => False', silent=settings.HIDE_DEBUG)
except TypeError:
cprint(f'[TREE]\t{item.MPN} ?= {manufacturer_mpn} => *** SKIPPED ***', silent=settings.HIDE_DEBUG)

return True
return 0


def is_new_supplier_part(supplier_name: str, supplier_sku: str) -> bool:
Expand Down Expand Up @@ -491,7 +498,7 @@ def create_parameter_template(name: str, units: str) -> int:

parameter_template = ParameterTemplate.create(inventree_api, {
'name': name,
'units': units,
'units': units if units else '',
})

if parameter_template:
Expand Down
8 changes: 4 additions & 4 deletions kintree/database/inventree_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,10 +509,10 @@ def inventree_create(part_info: dict, categories: list, kicad=False, symbol=None

if manufacturer_mpn:
cprint('\n[MAIN]\tCreating manufacturer part', silent=settings.SILENT)
is_new_manufacturer_part = inventree_api.is_new_manufacturer_part(manufacturer_name=manufacturer_name,
manufacturer_mpn=manufacturer_mpn)
manufacturer_part = inventree_api.is_new_manufacturer_part(manufacturer_name=manufacturer_name,
manufacturer_mpn=manufacturer_mpn)

if not is_new_manufacturer_part:
if manufacturer_part:
cprint('[INFO]\tManufacturer part already exists, skipping.', silent=settings.SILENT)
else:
# Create a new manufacturer part
Expand Down Expand Up @@ -544,7 +544,7 @@ def inventree_create(part_info: dict, categories: list, kicad=False, symbol=None
manufacturer_name=manufacturer_name,
manufacturer_mpn=manufacturer_mpn,
supplier_name=supplier_name,
supplier_sku=inventree_part['supplier'][supplier_name],
supplier_sku=supplier_sku,
description=inventree_part['description'],
link=inventree_part['supplier_link'])

Expand Down

0 comments on commit 84be9dd

Please sign in to comment.