-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmakerbase.py
186 lines (171 loc) · 6.58 KB
/
makerbase.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
import json
import datetime
from bson import json_util
from bson.objectid import ObjectId
from pymongo import MongoClient
def parse_json(data):
return json.loads(json_util.dumps(data))
class MakerBase:
def __init__(self,config):
# Create a connection using MongoClient
client = MongoClient(config['database']['url'])
self.db = client['makerbase']
def search_drawer(self, drawer_name):
results = [x for x in self.db['items'].find(
{"category": {'$regex': drawer_name}})]
return parse_json(results)
def create_drawer(self, drawer_name, id):
return self.db["drawers"].insert_one(
{
"drawer_id": id,
"category": drawer_name,
"created": datetime.datetime.now(),
}
)
def locate_drawer(drawer_id):
raise NotImplementedError
def search_item(self, item_name):
results = [x for x in self.db['items'].find(
{"item_name": {'$regex': item_name}})]
return parse_json(results)
def create_item(self, item_name, drawer_id, user_id, quantity):
r = self.db["items"].insert_one(
{
"drawer_id": drawer_id,
"item_name": item_name,
"history": [
{
"action": "add",
"quantity": int(quantity),
"user_id": user_id,
"datetime": datetime.datetime.now(),
}
],
}
)
self.calculate_fields(r.inserted_id)
return r
def add_item(self, item_id, user_id, quantity):
self.db["items"].update_one(
{ '_id': ObjectId(item_id) },
{ '$push': {
'history': {
"action": "add",
"quantity": int(quantity),
"user_id": user_id,
"datetime": datetime.datetime.now(),
}
}
}
)
self.calculate_fields(item_id)
def remove_item(self, item_id, user_id, quantity):
self.db["items"].update_one(
{ '_id': ObjectId(item_id) },
{ '$push': {
'history': {
"action": "remove",
"quantity": int(quantity),
"user_id": user_id,
"datetime": datetime.datetime.now(),
}
}
}
)
self.calculate_fields(item_id)
def borrow_item(self, item_id, user_id, quantity):
self.db["items"].update_one(
{ '_id': ObjectId(item_id) },
{ '$push': {
'history': {
"action": "borrow",
"quantity": int(quantity),
"user_id": user_id,
"datetime": datetime.datetime.now(),
}
}
}
)
self.calculate_fields(item_id)
def return_item(self, item_id, user_id, quantity):
self.db["items"].update_one(
{ '_id': ObjectId(item_id) },
{ '$push': {
'history': {
"action": "return",
"quantity": int(quantity),
"user_id": user_id,
"datetime": datetime.datetime.now(),
}
}
}
)
self.calculate_fields(item_id)
def calculate_fields(self,item_id):
result = self.db['items'].update_one({'_id': ObjectId(item_id)}, [
{
'$set': {
'current_quantity': {
'$sum': {
'$map': {
'input': '$history',
'as': 'hh',
'in': {
'$cond': [
{
'$or': [
{
'$eq': [
'$$hh.action', 'add'
]
}, {
'$eq': [
'$$hh.action', 'return'
]
}
]
},
'$$hh.quantity',
{
'$subtract': [
0, '$$hh.quantity'
]
}
]
}
}
}
},
'total_quantity': {
'$sum': {
'$map': {
'input': '$history',
'as': 'hh',
'in': {
'$cond': [
{
'$eq': [
'$$hh.action', 'add'
]
}, '$$hh.quantity', {
'$cond': [
{
'$eq': [
'$$hh.action', 'remove'
]
}, {
'$subtract': [
0, '$$hh.quantity'
]
}, 0
]
}
]
}
}
}
}
}
}
])
return result