This repository has been archived by the owner on Mar 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
294 lines (243 loc) · 9.59 KB
/
db.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
#-*- coding: utf-8 -*-
import pymongo
from pymongo import MongoClient
import random
import pprint
import sys
class DB():
def __init__(self):
try:
self.client = MongoClient()
self.client = MongoClient("192.168.1.100", 37111, serverSelectionTimeoutMS = 5000)
self.db = self.client.warehouse
self.client.server_info()
self.material = self.db.material
self.component = self.db.component
self.order = self.db.order
self.product = self.db.product
print("Connected to database")
except pymongo.errors.ConnectionFailure as e:
print("Can not connect to database")
sys.exit(e)
return 0
def __del__(self):
self.client.close()
# Random free id from material collection
def material_id(self):
id = int(random.random()*100000)
id = (id*10)+1
if self.material.find({"_id": id})==0 | id<100000:
self.material_id()
else:
return str(id)
# Random free id from component collection
def component_id(self):
id = int(random.random()*100000)
id = (id*10)+2
if self.component.find({"_id": id})==0 | id<100000:
self.component_id()
else:
return str(id)
# Random free id from product collection
def product_id(self):
id = int(random.random()*100000)
id = (id*10)+3
if self.product.find({"_id": id})==0 | id<100000:
self.product_id()
else:
return str(id)
def new_material(self, name, quantity, description, value, supplier, time):
id = self.material_id()
material = { "_id": id, "name": name, "quantity": quantity, "description": description, "value": value, "supplier": supplier, "time": time}
try:
self.material.insert_one(material)
return 1
except:
print("Can not inster material into collection")
return 0
def new_component(self, name, quantity, description, value, supplier, time):
id = self.component_id()
component = { "_id": id, "name": name, "quantity": quantity, "description": description, "value": value, "supplier": supplier, "time": time}
try:
self.component.insert_one(component)
return 1
except:
print("Can not inster component into collection")
return 0
def new_product(self, name, description):
id = self.product_id()
product = { "_id": id, "name": name, "description": description}
try:
self.product.insert_one(product)
return 1
except:
print("Can not inster product into collection")
return 1
def extend_component(self, component_id, material_id, quantity):
tmp = self.material.find_one({"_id": material_id})
try:
self.component.update(
{ "_id": component_id },
{ "$push": { "materials": { "id": material_id,"name": tmp["name"] ,"quantity": quantity } } }
)
return 1
except:
print("Can not extend component's materials list")
return 0
def extend_product(self, product_id, component_id, quantity):
tmp = self.component.find_one({"_id": component_id})
try:
self.product.update(
{ "_id": product_id },
{ "$push": { "content": { "id": component_id,"name": tmp["name"] ,"quantity": quantity } } }
)
return 1
except:
print("Can not extend product's content list")
return 0
def check_collection(self, id):
code=int(id)
if code>999995 or code<100000: return 0
elif code%10==1: return 1
elif code%10==2: return 2
elif code%10==3: return 3
else: return 0
def add(self, id, quantity=1):
if self.check_collection(id)==1: self.add_material(id, quantity)
elif self.check_collection(id)==2: self.add_component(id, quantity)
else: return 0
return 1
def sub(self, id, quantity=1):
if self.check_collection(id)==1: self.sub_material(id, quantity)
elif self.check_collection(id)==2: self.sub_component(id, quantity)
else: return 0
return 1
def update_quantity(self, id, quantity):
if self.check_collection(id)==1: self.update_material(id, quantity)
elif self.check_collection(id)==2: self.update_component(id, quantity)
else: return 0
return 1
def add_component(self, id, qnt=1):
try:
self.component.update({ "_id": id },{ "$inc": { "quantity": qnt } })
except:
print("Can not add component")
def sub_component(self, id, qnt=1):
try:
self.component.update({ "_id": id },{ "$inc": { "quantity": -qnt } })
except:
print("Can not substract component")
def add_material(self, id, qnt=1):
try:
self.material.update({ "_id": id },{ "$inc": { "quantity": qnt } })
except:
print("Can not add material")
def sub_material(self, id, qnt=1):
try:
self.material.update({ "_id": id },{ "$inc": { "quantity": -qnt } })
except:
print("Can not substract material")
def add_product(self, id):
try:
tmp = self.product.find_one({"_id": id})
except:
print("Can not find in product list")
for x in tmp["content"]:
self.add_component(x["id"], x["quantity"])
def update_component(self, id, quantity):
try:
self.component.update({ "_id": id },{ "$set": { "quantity": quantity } })
except:
print("Can not update element quantity")
def update_material(self, id, quantity):
try:
self.material.update({ "_id": id },{ "$set": { "quantity": quantity } })
except:
print("Can not update material quantity")
def sub_product_sub_component(self, id):
try:
tmp = self.product.find_one({"_id": id})
except:
print("Can not find in product list")
for x in tmp["content"]:
self.sub_component(x["id"], x["quantity"])
def add_component_sub_material(self, id, quantity=1):
try:
tmp = self.component.find_one({"_id": id})
self.add_component(id, quantity)
except:
print("Can not find in component list")
return 0
try:
for x in tmp["materials"]:
self.sub_material(x["id"], x["quantity"]*quantity)
return 1
except: return 1
def show(self, id, all=False):
grup=self.check_collection(id)
if grup==1:
try:
tmp = self.material.find_one({"_id": id})
print("\nColection: Material")
print("ID: {}".format(tmp["_id"]))
print("Name: {}".format(tmp["name"]))
# print("Description: {}".format(tmp["description"]))
print("Quantity: {}".format(tmp["quantity"]))
# print("Supplier: {}".format(tmp["supplier"]))
# print("Value: {}".format(tmp["value"]))
# print("Delivery time: {}".format(tmp["time"]))
return 1
except:
print("Can not find in material list")
return 0
elif grup==2:
try:
tmp = self.component.find_one({"_id": id})
print("\nColection: Component")
print("ID: {}".format(tmp["_id"]))
print("Name: {}".format(tmp["name"]))
# print("Description: {}".format(tmp["description"]))
print("Quantity: {}".format(tmp["quantity"]))
# print("Supplier: {}".format(tmp["supplier"]))
# print("Value: {}".format(tmp["value"]))
# print("Delivery time: {}".format(tmp["time"]))
if all==True:
try:
for x in tmp["materials"]:
print("\nMaterial:")
print("ID: {}".format(x["id"]))
print("Name: {}".format(x["name"]))
print("Quantity: {}".format(x["quantity"]))
except:
print ("No materials")
return 0
return 2
except:
print("Can not find in component list")
return 0
elif grup==3:
try:
tmp = self.product.find_one({"_id": id})
print("\nColection: Product")
print("Product ID: {}".format(tmp["_id"]))
print("Product name: {}".format(tmp["name"]))
return 3
except:
print("Can not find in product list")
return 0
else:
print("Incorrect ID")
return 0
def erase_order(self):
self.order.remove({})
for x in self.component.find():
if x["supplier"]!= "Kell ideas":
self.order.insert(x)
else:
for y in x["materials"]:
tmp = self.material.find_one({"_id": y["id"]})
if self.order.count_documents({"_id": tmp["_id"]}, limit=1) !=0:
continue
else:
self.order.insert(tmp)
# def update_order(self, id, quantity)