forked from rgilham/PK2InvenTree
-
Notifications
You must be signed in to change notification settings - Fork 2
/
migratePKCategories.py
167 lines (140 loc) · 4.76 KB
/
migratePKCategories.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
import partkeepr
from inventree.api import InvenTreeAPI
from inventree.part import PartCategory,Part,PartAttachment
from inventree.stock import StockItem, StockLocation
import tempfile
import requests
import os
from configparser import ConfigParser
config = ConfigParser()
config.read('config.ini')
SERVER_ADDRESS = config.get('inventree', 'server_address')
MY_USERNAME = config.get('inventree', 'user')
MY_PASSWORD = config.get('inventree', 'password')
api = InvenTreeAPI(SERVER_ADDRESS, username=MY_USERNAME, password=MY_PASSWORD)
#delete all the exisiting parts
itparts = Part.list(api)
for p in itparts:
print("delete p ",p.name)
p.__setitem__('active', False)
p.__setitem__('image', None)
p.save()
p.delete()
#delete all of the exisitng categories
cat = PartCategory.list(api)
for c in cat:
print("delete c ",c.name)
c.delete()
def checkAndCreatePartCat(partnodeCat,parent):
partnodeCat = partnodeCat.replace('/','-')
try:
parent = parent.replace('/', '-')
except:
pass
#print("checkAndCreatePartCat %s %s" % (partnodeCat,parent))
cats = PartCategory.list(api,search=partnodeCat)
for c in cats:
if (c.name==partnodeCat):
#print ("found ",partnodeCat)
#print(c.name)
if (c.parent==None) and (parent==None):
return c
if not(c.parent==None):
parentCat = PartCategory(api,c.parent)
#print("parentCat ",parentCat)
#print("parentCat ", parentCat.name)
if (parentCat.name == parent):
return c
if (parent==None):
cat = PartCategory.create(api,{
'name' : partnodeCat,
'description' : ''
})
#print("created ",cat," ",cat.pk)
return cat
else:
parentCat = PartCategory.list(api, search=parent)
#print("got parent ",parentCat)
if (len(parentCat)>0):
parentpk=None
for pc in parentCat:
if (pc.name==parent):
parentpk = pc
break
#print(parentpk.name)
#print(parentpk.pk)
cat = PartCategory.create(api, {
'name': partnodeCat,
'description': '',
'parent' : parentpk.pk
})
print("created ",cat," ",cat.pk)
return cat
else:
print("checkAndCreatePartCat %s %s" % (partnodeCat, parent))
print("Error parent given but not created previously")
return None
def getorCreateLocation(part):
print(part.name, " loca ",part.storageLocation)
if (len(part.storageLocation)>0):
itloca = StockLocation.list(api,search=part.storageLocation)
for loc in itloca:
if (loc.name == part.storageLocation):
return loc
return StockLocation.create(api, {
'name': part.storageLocation,
'parent': ""
})
else:
#create or return unknownloadtion
itloca = StockLocation.list(api, search='UNKNOWN')
if (len(itloc)>0):
return itloca[0]
return 0
class DownloadedAttachment:
"Context manager for a downloaded attachment with the correct file name"
def __init__(self, attachment_data):
self.path = os.path.join(tempfile.gettempdir(), attachment_data.filename)
self.url = attachment_data.url
def __enter__(self):
with requests.get(self.url, auth=partkeepr.auth) as r:
open(self.path, 'wb').write(r.content)
return self
def __exit__(self, exc_type, exc_value, traceback):
os.unlink(self.path)
def createITPart(part,ITCat):
print("create part %s cat %s" % (part,ITCat.name))
if len(part.description)==0:
part.description=part.name
np = Part.create(api, {
'name' : part.name,
'description' : part.description,
'notes' : part.comment,
'category' : ITCat.pk,
'active' : True,
'virtual' : False,
'IPN' : part.IPN
})
if part.image:
with DownloadedAttachment(part.image) as file:
np.uploadImage(file.path)
for attachment in part.attachments:
with DownloadedAttachment(attachment) as file:
np.uploadAttachment(attachment=file.path)
return np
allPKparts=partkeepr.getallParts()
print(allPKparts)
for pkpart in allPKparts:
catpath = pkpart.categoryPath[1:]
root = None
for p in catpath:
catforPart = checkAndCreatePartCat(p,root)
root = p
newPart = createITPart(pkpart, catforPart)
print(newPart._data)
itloc = getorCreateLocation(pkpart)
stockit = StockItem.create(api,{
'part' : newPart.pk,
'quantity' : pkpart.stock,
'location' : itloc.pk
})