-
Notifications
You must be signed in to change notification settings - Fork 4
/
partkeepr.py
241 lines (198 loc) · 6.51 KB
/
partkeepr.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
# coding: utf-8
import requests
import json
import pprint
server = "http://partkeepr.inventech.co.za"
api = "/api/parts"
searchfilter = '?filter={"property":"name","operator":"LIKE","value":"%s%%"}'
auth=('gilham','gilham')
class part(object):
def __init__(self,req):
#print(req)
#print (req["name"])
self.name = req["name"]
self.description = req["description"]
self.id = req["@id"][req["@id"].rfind("/")+1:]
try:
self.footprint = req["footprint"]["name"]
except TypeError:
self.footprint = ""
try:
self.category = req["category"]["name"]
except TypeError:
self.category = ""
try:
self.categoryPath = req["category"]["categoryPath"].split(u' ➤ ')
except TypeError:
self.categoryPath =""
try:
self.storageLocation = req["storageLocation"]["name"]
except TypeError:
self.storageLocation=""
try:
self.MFR = req["manufacturers"][0]["manufacturer"]["name"]
except (TypeError,IndexError):
self.MFR = "-"
try:
self.MPN = req["manufacturers"][0]["partNumber"]
except (TypeError , IndexError):
self.MPN = "-"
try:
self.IPN = req['internalPartNumber']
except (TypeError, IndexError):
self.IPN =self.MPN
try:
self.stock = req['stockLevel']
except (TypeError, IndexError):
self.stock = 1
try:
self.price = req['averagePrice']
except (TypeError, IndexError):
self.price = 0
def getValues(self):
return [self.category,self.name,self.description,self.footprint]
def __str__(self):
return '%s %s %s %s ' % (self.name,self.description,self.footprint,self.category)
def getPartsValues(listparts):
pvales = []
for p in listparts:
pvales.append(p.getValues())
return pvales
def searchComponent(value):
url = server+api+searchfilter
r = requests.get( url % value,auth=auth)
if (r.status_code == 200):
listofparts = []
for p in r.json()["hydra:member"]:
listofparts.append(part(p))
return listofparts
else:
return None
def getDefaultHdr():
return [" Category ","Value"," Description ","Footprint"]
def getProjectList():
url = server + "/api/projects"
r = requests.get(url, auth=auth)
if (r.status_code == 200):
listofprojects = []
for p in r.json()["hydra:member"]:
listofprojects.append(p["name"])
return listofprojects
else:
return []
def createProject(projectName,projectDescript,parts):
projectDict = dict(name=projectName,description=projectDescript)
#{"quantity":1,"remarks":"R1","overageType":"","overage":0,"lotNumber":"","part":"/api/parts/4"}
partList = []
for partid,partv in parts.items():
partList.append({"quantity": len(partv['remark']), "remarks": "%s" % (",".join(partv['remark'])), "overageType": "", "overage": 0, "lotNumber": "", "part": "/api/parts/%s" % partid})
projectDict['parts']=partList
url = server + "/api/projects"
r = requests.post(url, data=json.dumps(projectDict),auth=auth)
if r.status_code == 201:
return True
return False
def buildPartCategoriesa(pcat):
#print(pcat)
#cat = []
#print(len(pcat))
#return
#print(pcat[5]['name'])
cat = {}
for p in pcat:
#print(p['name']+" "+p['@id'])
#cat[p] = p['children']
#children = {}
children=[]
for c in p['children']:
children.append(c['name'])
#print("children ", c['name'])
#buildPartCategories(c['children'])
#print(pcat['name'])
cat[p['name']]=children
print(cat)
return cat
for p in pcat["name"]:
#print (p)
l = [p["name"]]
for c in p["children"]:
l.append(buildPartCategories(c))
cat.append(l)
return cat
def getPartCategories():
url = server + "/api/part_categories"
r = requests.get(url, auth=auth)
if (r.status_code == 200):
#print(pprint.pprint(r.json()))
#print(r.json()["hydra:member"])
listofcategories = buildPartCategories(r.json()["hydra:member"])
#print(listofcategories)
print(listofcategories.keys())
pass
#return listofprojects
else:
return []
def getallParts():
allfilter ="?itemsPerPage=9999999999"
url = server + api + allfilter
r = requests.get(url, auth=auth)
if (r.status_code == 200):
listofparts = []
for p in r.json()["hydra:member"]:
pkpart = part(p)
#print(pkpart)
listofparts.append(pkpart)
#print(listofparts)
#print(len(listofparts))
return listofparts
else:
return None
def recursiveCat(pathlist,catdict):
print(pathlist, catdict)
if (len(pathlist) >= 1):
node = recursiveCat(pathlist[1:], pathlist[0])
print (node)
print (catdict)
return [catdict,node]
return catdict
print(pathlist)
print(catdict)
if (len(pathlist)>1):
if pathlist[0] in catdict:
twig = catdict[pathlist[0]]['children']
else:
twig = {'childrenlist':[],'children':{}}
leaf = recursiveCat(pathlist[1:],twig)
print("leaf %s" % leaf)
print("pathlist %s" % pathlist)
print("twig %s" % twig)
twig['childrenlist'].extend(leaf.keys())
twig['children'].update(leaf)
print("twig %s" % twig)
set(twig['childrenlist'])
print("twig %s" % twig)
print("catdict %s" % catdict)
catdict['childrenlist'].extend(twig.keys())
#print(catdict['childrenlist'])
#set(catdict['childrenlist'])
#print(catdict['childrenlist'])
catdict['children'].update(twig)
node = {pathlist[0]: {'childrenlist':[],"children":{}}}
return node
def getPartCategories(allparts):
#given the list of parts build up a dictionary of dictionaries
for part in allparts:
print(part.name)
partcat = part.category
print(partcat)
partcatpath = part.categoryPath
print(partcatpath)
partparts = partcatpath.split(u' ➤ ')
print(part.name)
print(partparts)
catdict=[]
print("recursiveCat")
cat = recursiveCat(partparts,catdict)
print (cat)
print (catdict)
break