mirror of
https://github.com/inventree/demo-dataset.git
synced 2025-06-16 03:55:30 +00:00
Add some more parts
- Fasteners - ICs
This commit is contained in:
134
scripts/capacitor_digikey.py
Normal file
134
scripts/capacitor_digikey.py
Normal file
@ -0,0 +1,134 @@
|
||||
from inventree.api import InvenTreeAPI
|
||||
from inventree.part import Part, PartCategory
|
||||
from inventree.base import Parameter
|
||||
from inventree.company import Company, ManufacturerPart, SupplierPart, ManufacturerPartParameter
|
||||
|
||||
import os
|
||||
import sys
|
||||
import digikey
|
||||
from digikey.v3.productinformation import KeywordSearchRequest
|
||||
|
||||
os.environ['DIGIKEY_CLIENT_ID'] = 'DjV4w1v0ebNTiL7Nqvslw0GkNYuYdrLG'
|
||||
os.environ['DIGIKEY_CLIENT_SECRET'] = 'dK0dTRimeq3aiPH1'
|
||||
os.environ['DIGIKEY_CLIENT_SANDBOX'] = 'False'
|
||||
os.environ['DIGIKEY_STORAGE_PATH'] = 'C:\\Users\\Oliver\\Desktop\\digikey\\'
|
||||
|
||||
INVENTREE_URL = "http://localhost:8000"
|
||||
INVENTREE_USERNAME = "admin"
|
||||
INVENTREE_PASSWORD = "inventree"
|
||||
|
||||
inventree = InvenTreeAPI(INVENTREE_URL, username=INVENTREE_USERNAME, password=INVENTREE_PASSWORD)
|
||||
|
||||
resistors = Part.list(inventree, category=6)
|
||||
|
||||
def getParameter(result, name):
|
||||
|
||||
for param in result.parameters:
|
||||
if param.parameter.lower() == name.lower():
|
||||
return param
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def getValue(result, name):
|
||||
|
||||
param = getParameter(result, name)
|
||||
|
||||
if param:
|
||||
return param.value
|
||||
else:
|
||||
return None
|
||||
|
||||
manufacturers = {}
|
||||
|
||||
DIGIKEY_PK = 1
|
||||
|
||||
for res in resistors:
|
||||
|
||||
search_term = res.name.replace('_', ' ').replace('C ' , 'Capacitor ceramic X7R ')
|
||||
|
||||
print(f"Capacitor: {res.name} -> {search_term}")
|
||||
|
||||
request = KeywordSearchRequest(search_term, record_count=25)
|
||||
|
||||
result = digikey.keyword_search(body=request)
|
||||
|
||||
# Set of manufacturer part numbers
|
||||
MPN = set()
|
||||
|
||||
if len(result.products) == 0:
|
||||
print("--- NO RESULTS FOUND ---")
|
||||
|
||||
for product in result.products:
|
||||
|
||||
mpn = product.manufacturer_part_number
|
||||
|
||||
if mpn in MPN or len(MPN) >= 2:
|
||||
continue
|
||||
|
||||
print(f"MPN > {mpn}")
|
||||
|
||||
MPN.add(mpn)
|
||||
|
||||
sku = product.digi_key_part_number
|
||||
|
||||
man_name = product.manufacturer.value
|
||||
|
||||
if man_name in manufacturers.keys():
|
||||
manufacturer = manufacturers[man_name]
|
||||
else:
|
||||
|
||||
# Search InvenTree for manufacturer name
|
||||
query = Company.list(inventree, search=man_name)
|
||||
|
||||
if len(query) == 0:
|
||||
|
||||
print(f"Creating new manufacturer: '{man_name}'")
|
||||
|
||||
manufacturer = Company.create(inventree, data={
|
||||
'is_supplier': False,
|
||||
'is_manufacturer': True,
|
||||
'name': man_name,
|
||||
})
|
||||
|
||||
else:
|
||||
manufacturer = query[0]
|
||||
|
||||
if manufacturer == None:
|
||||
continue
|
||||
|
||||
manufacturers[man_name] = manufacturer
|
||||
|
||||
m_parts = ManufacturerPart.list(inventree, MPN=mpn)
|
||||
|
||||
if len(m_parts) == 0:
|
||||
print(f"Creating new part: {man_name} -> {mpn}")
|
||||
manufacturer_part = ManufacturerPart.create(inventree, data={
|
||||
'part': res.pk,
|
||||
'manufacturer': manufacturer.pk,
|
||||
'MPN': mpn,
|
||||
})
|
||||
|
||||
print(manufacturer_part)
|
||||
else:
|
||||
manufacturer_part = m_parts[0]
|
||||
|
||||
# Check if a "supplier part" exists
|
||||
s_parts = SupplierPart.list(
|
||||
inventree,
|
||||
manufacturer_part=manufacturer_part.pk,
|
||||
supplier=DIGIKEY_PK
|
||||
)
|
||||
|
||||
if s_parts is None or len(s_parts) == 0:
|
||||
print(f"Creating new supplier part")
|
||||
|
||||
SupplierPart.create(inventree, data={
|
||||
'part': res.pk,
|
||||
'supplier': DIGIKEY_PK,
|
||||
'manufacturer_part': manufacturer_part.pk,
|
||||
'SKU': sku,
|
||||
'link': product.product_url,
|
||||
'description': product.product_description,
|
||||
})
|
||||
|
67
scripts/create_capacitor_stock.py
Normal file
67
scripts/create_capacitor_stock.py
Normal file
@ -0,0 +1,67 @@
|
||||
from inventree.api import InvenTreeAPI
|
||||
|
||||
from inventree.part import Part, PartCategory
|
||||
from inventree.stock import StockItem, StockLocation
|
||||
from inventree.company import SupplierPart
|
||||
|
||||
import random
|
||||
import sys
|
||||
|
||||
INVENTREE_URL = "http://localhost:8000"
|
||||
INVENTREE_USERNAME = "admin"
|
||||
INVENTREE_PASSWORD = "inventree"
|
||||
|
||||
api = InvenTreeAPI(INVENTREE_URL, username=INVENTREE_USERNAME, password=INVENTREE_PASSWORD)
|
||||
|
||||
capacitors = Part.list(api, category=6)
|
||||
|
||||
storage = StockLocation(api, pk=8)
|
||||
|
||||
count = 0
|
||||
|
||||
for cap in capacitors:
|
||||
|
||||
if random.random() > 0.65:
|
||||
continue
|
||||
|
||||
# Get the first matching supplierpart
|
||||
sp_list = SupplierPart.list(api, part=cap.pk)
|
||||
|
||||
for sp in sp_list:
|
||||
if random.random() > 0.6:
|
||||
continue
|
||||
|
||||
status = 10
|
||||
|
||||
q = random.random()
|
||||
|
||||
quantity = 1000
|
||||
|
||||
if q < 0.1:
|
||||
quantity = 500
|
||||
|
||||
elif q > 0.85:
|
||||
quantity = 4000
|
||||
|
||||
if random.random() < 0.2:
|
||||
quantity += int(random.random() * 2000)
|
||||
|
||||
if random.random() > 0.95:
|
||||
status = 55 # Damaged
|
||||
elif random.random() > 0.95:
|
||||
status = 50 # Attention
|
||||
|
||||
StockItem.create(api, data={
|
||||
'location': storage.pk,
|
||||
'part': cap.pk,
|
||||
'quantity': quantity,
|
||||
'supplier_part': sp.pk,
|
||||
'packaging': 'reel',
|
||||
'status': status,
|
||||
})
|
||||
|
||||
count += 1
|
||||
|
||||
print(f"Created {count} new stock items")
|
||||
|
||||
|
43
scripts/create_capacitors.py
Normal file
43
scripts/create_capacitors.py
Normal file
@ -0,0 +1,43 @@
|
||||
from inventree.api import InvenTreeAPI
|
||||
|
||||
from inventree.part import Part, PartCategory
|
||||
from inventree.stock import StockItem, StockLocation
|
||||
from inventree.company import SupplierPart
|
||||
|
||||
import random
|
||||
import sys
|
||||
|
||||
INVENTREE_URL = "http://localhost:8000"
|
||||
INVENTREE_USERNAME = "admin"
|
||||
INVENTREE_PASSWORD = "inventree"
|
||||
|
||||
api = InvenTreeAPI(INVENTREE_URL, username=INVENTREE_USERNAME, password=INVENTREE_PASSWORD)
|
||||
|
||||
category = 6
|
||||
|
||||
packages = [
|
||||
'0402',
|
||||
'0603',
|
||||
'0805',
|
||||
]
|
||||
|
||||
values = [
|
||||
# '100pF',
|
||||
'100nF',
|
||||
'1uF',
|
||||
'10uF',
|
||||
]
|
||||
|
||||
for package in packages:
|
||||
for value in values:
|
||||
name = f"C_{value}_{package}"
|
||||
description = f"{value} in {package} SMD package"
|
||||
keywords = "cap smd ceramic"
|
||||
|
||||
Part.create(api, data={
|
||||
'name': name,
|
||||
'category': category,
|
||||
'description': description,
|
||||
'keywords': keywords,
|
||||
'purchaseable': True,
|
||||
})
|
64
scripts/create_resistor_stock.py
Normal file
64
scripts/create_resistor_stock.py
Normal file
@ -0,0 +1,64 @@
|
||||
from inventree.api import InvenTreeAPI
|
||||
|
||||
from inventree.part import Part, PartCategory
|
||||
from inventree.stock import StockItem, StockLocation
|
||||
from inventree.company import SupplierPart
|
||||
|
||||
import random
|
||||
import sys
|
||||
|
||||
INVENTREE_URL = "http://localhost:8000"
|
||||
INVENTREE_USERNAME = "admin"
|
||||
INVENTREE_PASSWORD = "inventree"
|
||||
|
||||
api = InvenTreeAPI(INVENTREE_URL, username=INVENTREE_USERNAME, password=INVENTREE_PASSWORD)
|
||||
|
||||
resistors = Part.list(api, category=5)
|
||||
|
||||
storage = StockLocation(api, pk=8)
|
||||
|
||||
count = 0
|
||||
|
||||
for resistor in resistors:
|
||||
|
||||
if random.random() > 0.65:
|
||||
continue
|
||||
|
||||
q = random.random()
|
||||
|
||||
quantity = 1000
|
||||
|
||||
if q < 0.1:
|
||||
quantity = 2000
|
||||
|
||||
elif q > 0.85:
|
||||
quantity = 4000
|
||||
|
||||
# Get the first matching supplierpart
|
||||
sp_list = SupplierPart.list(api, part=resistor.pk)
|
||||
|
||||
for sp in sp_list:
|
||||
if random.random() > 0.6:
|
||||
continue
|
||||
|
||||
status = 10
|
||||
|
||||
if random.random() > 0.95:
|
||||
status = 55 # Damaged
|
||||
elif random.random() > 0.95:
|
||||
status = 50 # Attention
|
||||
|
||||
StockItem.create(api, data={
|
||||
'location': storage.pk,
|
||||
'part': resistor.pk,
|
||||
'quantity': quantity,
|
||||
'supplier_part': sp.pk,
|
||||
'packaging': 'reel',
|
||||
'status': status,
|
||||
})
|
||||
|
||||
count += 1
|
||||
|
||||
print(f"Created {count} new stock items")
|
||||
|
||||
|
131
scripts/resistors_digikey.py
Normal file
131
scripts/resistors_digikey.py
Normal file
@ -0,0 +1,131 @@
|
||||
from inventree.api import InvenTreeAPI
|
||||
from inventree.part import Part, PartCategory
|
||||
from inventree.base import Parameter
|
||||
from inventree.company import Company, ManufacturerPart, SupplierPart, ManufacturerPartParameter
|
||||
|
||||
import os
|
||||
import sys
|
||||
import digikey
|
||||
from digikey.v3.productinformation import KeywordSearchRequest
|
||||
|
||||
os.environ['DIGIKEY_CLIENT_ID'] = 'DjV4w1v0ebNTiL7Nqvslw0GkNYuYdrLG'
|
||||
os.environ['DIGIKEY_CLIENT_SECRET'] = 'dK0dTRimeq3aiPH1'
|
||||
os.environ['DIGIKEY_CLIENT_SANDBOX'] = 'False'
|
||||
os.environ['DIGIKEY_STORAGE_PATH'] = 'C:\\Users\\Oliver\\Desktop\\digikey\\'
|
||||
|
||||
INVENTREE_URL = "http://localhost:8000"
|
||||
INVENTREE_USERNAME = "admin"
|
||||
INVENTREE_PASSWORD = "inventree"
|
||||
|
||||
inventree = InvenTreeAPI(INVENTREE_URL, username=INVENTREE_USERNAME, password=INVENTREE_PASSWORD)
|
||||
|
||||
resistors = Part.list(inventree, category=5)
|
||||
|
||||
def getParameter(result, name):
|
||||
|
||||
for param in result.parameters:
|
||||
if param.parameter.lower() == name.lower():
|
||||
return param
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def getValue(result, name):
|
||||
|
||||
param = getParameter(result, name)
|
||||
|
||||
if param:
|
||||
return param.value
|
||||
else:
|
||||
return None
|
||||
|
||||
manufacturers = {}
|
||||
|
||||
DIGIKEY_PK = 1
|
||||
|
||||
for res in resistors:
|
||||
|
||||
search_term = res.name.replace('_', ' ').replace('R ' , 'Resistor ')
|
||||
|
||||
print(res.name, res.description)
|
||||
|
||||
request = KeywordSearchRequest(search_term, record_count=25)
|
||||
|
||||
result = digikey.keyword_search(body=request)
|
||||
|
||||
# Set of manufacturer part numbers
|
||||
MPN = set()
|
||||
|
||||
for product in result.products:
|
||||
|
||||
mpn = product.manufacturer_part_number
|
||||
|
||||
print(f"> {mpn}")
|
||||
|
||||
if mpn in MPN or len(MPN) >= 5:
|
||||
continue
|
||||
|
||||
MPN.add(mpn)
|
||||
|
||||
sku = product.digi_key_part_number
|
||||
|
||||
man_name = product.manufacturer.value
|
||||
|
||||
if man_name in manufacturers.keys():
|
||||
manufacturer = manufacturers[man_name]
|
||||
else:
|
||||
|
||||
# Search InvenTree for manufacturer name
|
||||
query = Company.list(inventree, search=man_name)
|
||||
|
||||
if len(query) == 0:
|
||||
|
||||
print(f"Creating new manufacturer: '{man_name}'")
|
||||
|
||||
manufacturer = Company.create(inventree, data={
|
||||
'is_supplier': False,
|
||||
'is_manufacturer': True,
|
||||
'name': man_name,
|
||||
})
|
||||
|
||||
else:
|
||||
manufacturer = query[0]
|
||||
|
||||
manufacturers[man_name] = manufacturer
|
||||
|
||||
m_parts = ManufacturerPart.list(inventree, MPN=mpn)
|
||||
|
||||
print("Existing Manufacturer Parts:")
|
||||
|
||||
for mp in m_parts:
|
||||
print(f" - {mp.MPN}, {mp.manufacturer}")
|
||||
|
||||
if len(m_parts) == 0:
|
||||
print(f"Creating new part: {man_name} -> {mpn}")
|
||||
manufacturer_part = ManufacturerPart.create(inventree, data={
|
||||
'part': res.pk,
|
||||
'manufacturer': manufacturer.pk,
|
||||
'MPN': mpn,
|
||||
})
|
||||
else:
|
||||
manufacturer_part = m_parts[0]
|
||||
|
||||
# Check if a "supplier part" exists
|
||||
s_parts = SupplierPart.list(
|
||||
inventree,
|
||||
manufacturer_part=manufacturer_part.pk,
|
||||
supplier=DIGIKEY_PK
|
||||
)
|
||||
|
||||
if s_parts is None or len(s_parts) == 0:
|
||||
print(f"Creating new supplier part")
|
||||
|
||||
SupplierPart.create(inventree, data={
|
||||
'part': res.pk,
|
||||
'supplier': DIGIKEY_PK,
|
||||
'manufacturer_part': manufacturer_part.pk,
|
||||
'SKU': sku,
|
||||
'link': product.product_url,
|
||||
'description': product.product_description,
|
||||
})
|
||||
|
Reference in New Issue
Block a user