-
Notifications
You must be signed in to change notification settings - Fork 1
/
partkeepr-to-inventree.py
executable file
·755 lines (656 loc) · 31.7 KB
/
partkeepr-to-inventree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
#!/usr/bin/python3
import requests
import json
import pprint
import getopt
import sys
import time
import shutil
import tempfile
import os
import datetime
import re
import logging
from inventree.api import InvenTreeAPI
from inventree.part import PartCategory, Part, ParameterTemplate, Parameter, InternalPrice
from inventree.stock import StockItem, StockLocation
from inventree.company import Company, ManufacturerPart, SupplierPart, SupplierPriceBreak
DEFAULT_PARTKEEPR = "https://admin:[email protected]"
DEFAULT_INVENTREE = "http://admin:[email protected]:1337"
DEFAULT_CURRENCY = "EUR"
verbose = False
#logging from https://stackoverflow.com/questions/11325019/how-to-output-to-the-console-and-file
logger = logging.getLogger('partkeepr-to-inventree')
logging.getLogger().setLevel(logging.INFO)
file_log_handler = logging.FileHandler('partkeepr-to-inventree.log', mode='w')
logger.addHandler(file_log_handler)
stderr_log_handler = logging.StreamHandler()
logger.addHandler(stderr_log_handler)
# nice output format
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s', datefmt='%H:%M:%S')
file_log_handler.setFormatter(formatter)
stderr_log_handler.setFormatter(formatter)
def getFromPartkeepr(url, base, auth):
sep = '?'
if('?' in url):
sep='&'
full_url = f'{base}{url}{sep}itemsPerPage=100000'
full_url = full_url.replace('/partkeepr/partkeepr','/partkeepr')
r = requests.get(full_url, auth=auth)
if (r.status_code == 200):
return r.json()["hydra:member"]
return None
def getImageFromPartkeepr(url, base, auth, filename="image"):
full_url = f'{base}{url}/getImage'
full_url = full_url.replace('/partkeepr/partkeepr','/partkeepr')
r = requests.get(full_url, auth=auth, stream=True)
if (r.status_code == 200):
r.raw.decode_content = True
tmp = tempfile.NamedTemporaryFile(delete=False, suffix="f-%s" % (filename))
shutil.copyfileobj(r.raw, tmp)
return tmp.name
return None
def getFileFromPartkeepr(url, base, auth, filename="file"):
full_url = f'{base}{url}/getFile'
full_url = full_url.replace('/partkeepr/partkeepr','/partkeepr')
r = requests.get(full_url, auth=auth, stream=True)
if (r.status_code == 200):
r.raw.decode_content = True
path = f'/tmp/{filename}'
f = open(path, 'wb')
shutil.copyfileobj(r.raw, f)
f.close()
return path
return None
def create(cls, inventree, attributes):
n = 0
while True:
try:
c = cls.create(inventree, attributes)
return c
except Exception as err:
n += 1
if verbose:
logger.error(f'failed ({n}), waiting...')
if n >= 10:
logger.error(f'failed to create {cls.__name__} with attributes {attributes}: {err=}, {type(err)=}')
break
time.sleep(3)
def upload_image(item, file):
n = 0
while True:
try:
rc = item.uploadImage(file)
return rc
except Exception as err:
n += 1
if verbose:
logger.error(f'failed ({n}), waiting...')
if n >= 10:
logger.error(f'failed to upload image {file}: {err=}, {type(err)=}')
break
time.sleep(3)
def upload_attachment(item, file, comment=None):
n = 0
while True:
try:
rc = item.uploadAttachment(file, comment=comment)
return rc
except Exception as err:
n += 1
if verbose:
logger.error(f'failed ({n}), waiting...')
if n >= 10:
logger.error(f'failed to upload attachment {file}: {err=}, {type(err)=}')
break
time.sleep(3)
def create_it_category_w_parent(category, category_map, parent_id, inventree_api):
category_id = int(category['@id'].rpartition("/")[2])
# Handle parent_id being None (i.e., root categories)
if parent_id is not None:
parent_it_pk = category_map.get(parent_id, None)
else:
parent_it_pk = None
# Handle None or missing description
description = category.get("description", None) or ""
# Log the category creation attempt
if verbose:
logger.info(f'Creating PartCategory "{category["name"]}", parent:{parent_it_pk}')
# Try to create the category
try:
icategory = create(PartCategory, inventree_api, {
'name': category["name"],
'description': description, # Ensure a valid description is passed
'parent': parent_it_pk,
})
return category_id, icategory
except requests.exceptions.HTTPError as e:
# Log the error if the API request fails
logger.error(f"ERROR - failed to create PartCategory with attributes "
f"{{'name': category['name'], 'description': description, 'parent': parent_it_pk}}: err={e}")
return category_id, None
def create_child_categories(parent_category, category_map, inventree_api):
'''Recursively runs through all children, child-children, ... and creates them in inventree'''
for sub_category in parent_category["children"]:
sub_category_id = int(sub_category['@id'].rpartition("/")[2])
if sub_category_id not in category_map:
# category doesn't yet exist, create it!
parent_id = int(sub_category['parent'].rpartition("/")[2])
# Create category and handle None case
id, icategory = create_it_category_w_parent(sub_category, category_map, parent_id, inventree_api)
if icategory is not None:
category_map[id] = icategory.pk
else:
# Handle the case where icategory is None, log an error or continue
print(f"Warning: Failed to create category for {sub_category['name']}")
# Continue recursing through children entries
parent_category = sub_category
category_map = create_child_categories(parent_category, category_map, inventree_api)
return category_map
def retry(retries, func, *args, **kwargs):
for attempt in range(retries):
try:
response = func(*args, **kwargs)
break
except Exception as e:
logger.error(f'Error running {func.__name__} for {attempt+1}. time. Error message:')
logger.error(f'\t{str(e)}')
logger.error('trying again...')
return response
def copy_stock_history(pkpr_key, partkeepr_url, partkeepr_auth, inventree_api, stock_item_pk):
pkpr_filter = '[{"subfilters":[],"property":"part","operator":"=","value":"/partkeepr/api/parts/' + str(pkpr_key) +'"}]'
pkpr_stock_changes = getFromPartkeepr(f'/api/stock_entries?filter={pkpr_filter}', partkeepr_url, partkeepr_auth)
if len(pkpr_stock_changes) == 0:
if verbose:
logger.info('No Stock changes available for this part. Continue..')
return
stock_changes = [entr['stockLevel'] for entr in pkpr_stock_changes]
#generate stock changes without crossing below 0 (every stock change below 0 is no stock change)
stock_levels = [sum(stock_changes[0:i+1]) for i in range(len(stock_changes))]
stock_levels_no_negative = [max(0,entr) for entr in stock_levels] # delete negative entries for inventree
stock_levels_no_negative.insert(0,0)
stock_changes_no_negative = [0 - (stock_levels_no_negative[i] - el) for i,el in enumerate(stock_levels_no_negative[1:])]
if verbose:
logger.info(f"Copying stock history for Part: {pkpr_stock_changes[0]['part']['name']}")
#upload to Inventree
part_StockItem = retry(10, StockItem, api=inventree_api, pk=stock_item_pk)
for i, pkpr_el in enumerate(pkpr_stock_changes):
time = datetime.datetime.strptime(pkpr_el['dateTime'], '%Y-%m-%dT%H:%M:%S%z').strftime('%d.%m.%Y %H:%M')
comment = pkpr_el['comment'] if pkpr_el['comment'] != None else '-'
user = pkpr_el['user']['username'] if pkpr_el['user'] else ''
note = f"(PartKeepr) {time} {user}: {comment}"
if stock_changes[i] != stock_changes_no_negative[i]:
note += ' (Entry adjusted to prevent below 0 stock!)'
price = None
if pkpr_el['price']:
if float(pkpr_el['price']) == 0:
price = float(pkpr_el['price'])
stock_update = stock_changes_no_negative[i]
if stock_update > 0:
retry(10,part_StockItem.addStock, quantity=stock_update, notes = note)
else:
retry(10,part_StockItem.removeStock, quantity=-stock_update, notes = note)
def usage():
print("""partkeepr-to-inventree [options]
-v --verbose operate more verbosely (logging AND terminal output)
-h --help show this usage information
-p URL --partkeepr=URL use this URL to connect PartKeepr
-i URL --inventree=URL use this URL to connect InvenTree
-w X --wipe=X wipe given kind of objects
--wipe-all wipe all objects
--default-currency=STR use 3 letter currency string as default
--copy-history copy stock history into InvenTree Stock Tracking
object kinds are Part, PartCategory, StockLocation, Company, FootprintTemplate
URLs are given as http[s]://USER:[email protected]""")
def main():
global verbose
try:
opts, args = getopt.getopt(sys.argv[1:], "hvp:i:w:", ["help", "verbose", "partkeepr=", "inventree=", "wipe=",
"wipe-all", "default-currency=", "copy-history"])
except getopt.GetoptError as err:
usage()
sys.exit(2)
partkeepr_auth_url = DEFAULT_PARTKEEPR
inventree_auth_url = DEFAULT_INVENTREE
default_currency = DEFAULT_CURRENCY
wipe = []
for o, a in opts:
if o in ("-v", "--verbose"):
verbose = True
elif o in ("-h", "--help"):
usage()
sys.exit()
elif o in ("-p", "--partkeepr"):
partkeepr_auth_url = a
elif o in ("-i", "--inventree"):
inventree_auth_url = a
elif o in ("-w", "--wipe"):
wipe.append(a)
elif o in ("--wipe-all"):
wipe = ["Part", "PartCategory", "StockLocation", "Company", "FootprintTemplate" ]
elif o in ("--default-currency"):
default_currency = a
elif o in ("--copy-history"):
copy_history = True
parts1 = inventree_auth_url.partition("//")
parts2 = parts1[2].rpartition("@")
parts3 = parts2[0].partition(":")
url = f'{parts1[0]}//{parts2[2]}'
inventree = InvenTreeAPI(url, username=parts3[0], password=parts3[2])
parts1 = partkeepr_auth_url.partition("//")
parts2 = parts1[2].rpartition("@")
parts3 = parts2[0].partition(":")
partkeepr_url = f'{parts1[0]}//{parts2[2]}'
partkeepr_auth = (parts3[0], parts3[2])
for table in wipe:
if table == "Part":
logger.info(f'deleting StockItems...')
stock_items = StockItem.list(inventree)
for stock_item in stock_items:
if verbose:
logger.info(f'delete StockItem "{stock_item.part}"')
stock_item.delete()
logger.info(f'deleting Parts...')
parts = Part.list(inventree)
for part in parts:
if verbose:
logger.info(f'delete Part "{part.name}"')
try:
part._data['active'] = False
part._data['image'] = None # needs to be removed, as otherwise saving doesn't work
part.save()
part.delete()
except Exception as err:
logger.info(f'deleting Part "{part.name}" failed')
elif table == "PartCategory":
logger.info(f'deleting PartCategories...')
categories = PartCategory.list(inventree)
for category in categories:
if verbose:
logger.info(f'delete PartCategory "{category.name}"')
try:
category.delete()
except Exception as err:
logger.error(f'failed')
elif table == "StockLocation":
logger.info(f'deleting StockLocations...')
locations = StockLocation.list(inventree)
for location in locations:
if verbose:
logger.info(f'delete StockLocation "{location.name}"')
try:
location.delete()
except Exception as err:
logger.error(f'deleting StockLocation "{location.name}" failed')
elif table == "Company":
logger.info(f'deleting Companies...')
companies = Company.list(inventree)
for company in companies:
if verbose:
logger.info(f'delete Company "{company.name}"')
try:
company.delete()
except Exception as err:
logger.error(f'deleting Company "{company.name}" failed')
elif table == "FootprintTemplate":
logger.info(f'deleting "Footprint" Parameter Template...')
param_templates = ParameterTemplate.list(inventree)
foundFootprintTemplate = False
for param_template in param_templates:
if param_template['name'] == 'Footprint':
foundFootprintTemplate = True
try:
param_template.delete()
except Exception as err:
logger.error(f'deleting "Footprint" Parameter Template failed')
if not foundFootprintTemplate:
logger.error(f'Did not find any "Footprint" parameter template.')
else:
logger.error(f'unknown table {table} to wipe')
companies = getFromPartkeepr("/api/manufacturers", partkeepr_url, partkeepr_auth)
logger.info(f'found {len(companies)} manufacturers, creating Companies...')
company_map = {} # mapped by name
for company in companies:
if not company["name"] in company_map:
if verbose:
logger.info(f'create Company "{company["name"]}"')
if ("url" in company) and (company["url"] != None):
website = company["url"]
if (not "http" in website) and len(website) >= 3:
website = "https://" + website
else:
website = ""
icompany = create(Company, inventree, {
'name': company["name"],
'website': website,
'is_customer': 0,
'is_manufacturer': 1,
'is_supplier': 0,
})
company_map[company["name"]] = icompany.pk
if ("icLogos" in company) and (company["icLogos"] != None) and len(company["icLogos"]) >= 1:
path = getImageFromPartkeepr(company["icLogos"][0]["@id"], partkeepr_url, partkeepr_auth, filename=company["icLogos"][0]["originalFilename"])
if verbose:
logger.info(f'uploading logo {company["icLogos"][0]["originalFilename"]}')
upload_image(icompany, path)
os.unlink(path)
companies = getFromPartkeepr("/api/distributors", partkeepr_url, partkeepr_auth)
logger.info(f'found {len(companies)} distributors, creating Companies...')
for company in companies:
if not company["name"] in company_map:
if verbose:
logger.info(f'create Company "{company["name"]}"')
if ("url" in company) and (company["url"] != None):
website = company["url"]
if not "http" in website:
website = "https://" + website
else:
website = ""
icompany = create(Company, inventree, {
'name': company["name"],
'website': website,
'is_customer': 0,
'is_manufacturer': 0,
'is_supplier': 1,
})
company_map[company["name"]] = icompany.pk
if ("icLogos" in company) and (company["icLogos"] != None) and len(company["icLogos"]) >= 1:
path = getImageFromPartkeepr(company["icLogos"][0]["@id"], partkeepr_url, partkeepr_auth, filename=company["icLogos"][0]["originalFilename"])
if verbose:
logger.info(f'uploading logo {company["icLogos"][0]["originalFilename"]}')
upload_image(icompany, path)
os.unlink(path)
else:
logger.error(f'Company "{company["name"]} already exists (as manufaturer?) while creating supplier')
sys.exit(1)
categories = getFromPartkeepr("/api/part_categories", partkeepr_url, partkeepr_auth)
logger.info(f'found {len(categories)} part categories, creating PartCategories...')
category_map = {} # mapped by @id
for category in categories:
if not int(category['@id'].rpartition("/")[2]) in category_map:
if category["parent"]:
parent_id = int(category["parent"]["@id"].rpartition("/")[2])
else:
parent_id = None
id, icategory = create_it_category_w_parent(category, category_map, parent_id, inventree)
# Ensure icategory is not None before trying to access .pk
if icategory is not None:
category_map[id] = icategory.pk
else:
# Log a warning or skip this category creation
print(f"Warning: Failed to create category '{category['name']}' with ID {id}. Skipping.")
continue # Skip further processing for this category
# Process child categories recursively
parent_category = category
category_map = create_child_categories(parent_category, category_map, inventree)
# we convert Partkeepr location categories and locations to an
# IntenTree hiercarchy of stock locations
location_categories = getFromPartkeepr("/api/storage_location_categories", partkeepr_url, partkeepr_auth)
logger.info(f'found {len(location_categories)} location categories, creating StockLocations...')
location_map = {} # mapped by @id
for location in location_categories:
if location["parent"]:
parent_pk = location_map[location["parent"]["@id"]]
else:
parent_pk = None
if location["description"]:
description = location["description"]
else:
description = "-"
if verbose:
logger.info(f'create StockLocation "{location["name"]}", parent:{parent_pk}')
ilocation = create(StockLocation, inventree, {
'name': location["name"],
'description': description,
'parent': parent_pk,
})
location_map[location['@id']] = ilocation.pk
locations = getFromPartkeepr("/api/storage_locations", partkeepr_url, partkeepr_auth)
logger.info(f'found {len(locations)} locations, creating StockLocations...')
for location in locations:
if location["category"]:
parent_pk = location_map[location["category"]["@id"]]
else:
parent_pk = None
if verbose:
logger.info(f'create StockLocation "{location["name"]}", parent:{parent_pk}')
ilocation = create(StockLocation, inventree, {
'name': location["name"],
'description': location["name"], # no description on PartKeepr
'parent': parent_pk,
})
location_map[location['@id']] = ilocation.pk
parts = getFromPartkeepr("/api/parts", partkeepr_url, partkeepr_auth)
logger.info(f'found {len(parts)} parts')
#create Footprtint parameter template
logger.info(f'Obtaining Footprints from Parts')
footprint_names = []
for part in parts:
try:
footprint_names.append(part['footprint']['name'])
except TypeError:
continue
footprint_names = list(set(footprint_names)) # make distinct
footprint_names.sort()
ifptemplate = create(ParameterTemplate, inventree, {
"name": "Footprint",
"units": "",
"description": "Footprint in text format",
"checkbox": False,
"choices": ','.join(footprint_names)
})
#create Parts
logger.info(f'Creating Parts, Attachements, StockItems, SupplierParts')
supplier_part_map = {}
created_IPNs_map = {} #key = "IPN"+"name"
for part_idx, part in enumerate(parts):
#print(part)
category_pk = category_map[int(part["category"]["@id"].rpartition("/")[2])]
part_name = part["name"].strip() #remove leading + trailing space, as it will anyways be done by inventree api
if part["storageLocation"]:
try:
location_pk = location_map[part["storageLocation"]["@id"]]
except:
location_pk = None
logger.error(f'could not handle storageLocation {part["storageLocation"]["@id"]} while creating Part {part_name}')
else:
location_pk = None
if part["averagePrice"]:
price = float(part["averagePrice"])
else:
price = None
revision = None
if ("description" in part) and (part["description"] != None) and (len(part["description"]) >= 1):
description = part["description"]
else:
description = ""
if len(description) > 250:
description = description[:247] + "..."
if part["internalPartNumber"].strip() == '' or part["internalPartNumber"] == '-':
ipn = '#' + str(part["@id"].rpartition("/")[2])
else:
ipn = part["internalPartNumber"]
units = None
if ("partUnit" in part) and (part["partUnit"] != None) and "shortName" in part["partUnit"]:
units = part["partUnit"]["shortName"]
quantity = max(0,part["stockLevel"]) # Inventree does not allow stock below 0
notes = f"**Partkeepr Status:** `{part['status']}`\n\n" if part["status"] != "" else ""
notes += part["comment"]
ipart = None
if (ipn+part_name) not in created_IPNs_map or part_name != created_IPNs_map[(ipn+part_name)]['name']:
#check entry with same IPN and name were created before
if verbose:
logger.info(f'({part_idx+1}/{len(parts)}) Create Part "{part["name"]}", category:{category_pk}, quantity:{quantity}')
ipart = create(Part, inventree, {
'name': part_name,
'description': description,
'IPN': ipn,
'category': category_pk,
'location': location_pk,
'active': True,
'virtual': False,
'minimum_stock': part["minStockLevel"],
'notes': notes,
'revision': revision,
#'link': xxx,
#'image': xxx,
'units': units,
'assembly': part["metaPart"],
})
if ipart != None:
created_IPNs_map[(ipn+part_name)] = ipart
else: # Part Entry already created, only add the StockItem and continue with next Part
if verbose:
logger.info(f'create additional StockItem for "{created_IPNs_map[ipn+part_name]["name"]}", category:{category_pk}, quantity:{quantity}')
istock = create(StockItem, inventree, {
'part': created_IPNs_map[(ipn+part_name)].pk,
'quantity': 0 if copy_history else quantity,
'delete_on_deplete': False,
'location': location_pk,
'notes': part['partCondition'] if part["partCondition"] != "" else None
})
if copy_history:
copy_stock_history(
pkpr_key = int(part["@id"].rpartition("/")[2]),
partkeepr_url = partkeepr_url,
partkeepr_auth = partkeepr_auth,
inventree_api = inventree,
stock_item_pk = istock.pk,
)
continue
if verbose:
logger.info(f'create StockItem "{part["name"]}", category:{category_pk}, quantity:{quantity}')
istock = create(StockItem, inventree, {
'part': ipart.pk,
'quantity': 0 if copy_history else quantity,
'delete_on_deplete': False,
'location': location_pk,
'notes': part['partCondition'] if part["partCondition"] != "" else None
})
internal_price = None
if verbose:
logger.info(f'create additional InternalPrice for "{created_IPNs_map[ipn+part_name]["name"]}", price:{price}')
internal_price = create(InternalPrice, inventree, {
'part': ipart.pk,
'quantity':quantity,
'price': price,
})
if copy_history:
copy_stock_history(
pkpr_key = int(part["@id"].rpartition("/")[2]),
partkeepr_url = partkeepr_url,
partkeepr_auth = partkeepr_auth,
inventree_api = inventree,
stock_item_pk = istock.pk,
)
impart = None
if (part["manufacturers"] != None) and (len(part["manufacturers"]) >= 1):
for manufacturer in part["manufacturers"]:
if manufacturer["manufacturer"] == None:
mpk = None
logger.error(f'no actual manufacturer data known while creating ManufacturerPart {part_name}')
elif manufacturer["manufacturer"]["name"] in company_map:
mpk = company_map[manufacturer["manufacturer"]["name"]]
else:
mpk = None
logger.error(f'manufacturer "{manufacturer["manufacturer"]["name"]}" not known as a Company while creating ManufacturerPart {part_name}')
if (manufacturer["partNumber"] != None) and (len(manufacturer["partNumber"]) >= 1):
mpn = manufacturer["partNumber"]
else:
mpn = "?" # XXX None
logger.error(f'manufacturer part number unknown while creating ManufacturerPart {part_name}')
if (mpk != None) and (mpn != None):
if verbose:
logger.info(f'create ManufacturerPart "{part["name"]}"')
impart = create(ManufacturerPart, inventree, {
'part': ipart.pk,
'manufacturer': mpk,
'MPN': mpn,
})
if (part["distributors"] != None) and (len(part["distributors"]) >= 1):
for distributor in part["distributors"]:
#assign distributor
if distributor["distributor"]["name"] in company_map:
spk = company_map[distributor["distributor"]["name"]]
else:
spk = None
logger.info(f'distributor "{distributor["distributor"]["name"]}" not known as a Company while creating SupplierPart {part_name}')
#assign manufacturer
mpk = None
if impart != None and len(part["manufacturers"]) == 1: #assignment only clear if only one manufacturer is assigned
mpk = impart.pk
#assign sku
if len(distributor["sku"]) >= 1:
sku = distributor["sku"]
elif len(distributor["orderNumber"]) >= 1:
sku = distributor["orderNumber"]
else:
sku = "?" # must not be an empty string?!
if verbose:
logger.error(f'distributor SKU not defined while creating SupplierPart "{part_name}", using a "-" placeholder')
if (spk != None) and (sku != None):
key = f'{ipart.pk}:{spk}:{sku}'
if not key in supplier_part_map:
if verbose:
logger.info(f'create SupplierPart "{part["name"]}"')
#print(part)
#print(f'XXX {ipart.pk} {spk}({distributor["distributor"]["name"]}) {sku}')
ispart = create(SupplierPart, inventree, {
'part': ipart.pk,
'supplier': spk,
'SKU': sku,
'manufacturer_part': mpk,
})
supplier_part_map[key] = ispart
else:
if verbose:
logger.info(f'SupplierPart matching "{key}" for Part "{part_name}" was already created. Just add additional PriceBreak.')
if distributor['price'] != None and distributor['price'] != "0.0000":
if distributor['currency'] == None:
currency = default_currency
else:
currency = distributor['currency']
I_pr_break = create(SupplierPriceBreak, inventree, {
'part': supplier_part_map[key].pk,
'quantity': distributor['packagingUnit'],
'price': distributor['price'],
'supplier': spk,
'price_currency': currency
})
if (part["attachments"] != None) and len(part["attachments"]) >= 1:
for attachment in part["attachments"]:
filename = re.sub('\?[^ ]{0,}','',attachment["originalFilename"]) # remove ?[...] (sometimes found in pkpr after file type)
# upload first found image as displayed picture
if attachment["isImage"] and ipart._data['image'] == None:
path = getImageFromPartkeepr(attachment["@id"], partkeepr_url, partkeepr_auth, filename=filename)
if path != None: #sometimes the source file might be deleted in partkeepr -> skip these
if verbose:
logger.info(f'uploading image {path} for Part "{part_name}"')
upload_image(ipart, path)
os.unlink(path)
else:
logger.error(f'Failed to upload file "{filename}" to part "{part["name"]}". Partkeepr did not provide the file!')
path = getFileFromPartkeepr(attachment["@id"], partkeepr_url, partkeepr_auth, filename=filename)
if path != None: #sometimes the source file might be deleted in partkeepr -> skip these
if verbose:
logger.info(f'uploading attachment {path} for Part "{part_name}"')
if (attachment["description"] != None) and (len(attachment["description"]) >= 1):
comment = attachment["description"]
else:
comment = None
upload_attachment(ipart, path, comment=comment)
os.unlink(path)
else:
logger.error(f'Failed to upload file "{filename}" to part "{part["name"]}". Partkeepr did not provide the file!')
if (part["footprint"] != None):
if part["footprint"]["name"]:
if part["footprint"]["name"] != '':
if verbose:
logger.info(f'Add footprint "{part["footprint"]["name"]}" to Part "{part_name}"')
ifpparameter = create(Parameter, inventree, {
"part": ipart.pk,
"template": ifptemplate.pk,
"data": part["footprint"]["name"]
})
if __name__ == '__main__':
main()