forked from jacksonjohnston238/ECE461L-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
259 lines (204 loc) · 7.58 KB
/
app.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
from flask import Flask
from flask_cors import CORS, cross_origin
# from and import statements in order to access specific functions with these libraries
from pymongo import MongoClient
from flask_bcrypt import Bcrypt
import json
client = MongoClient("mongodb+srv://ecedatabaseuser:[email protected]/?retryWrites=true&w=majority")
# Initializes the database to be the test database in the client in MongoDB
database = client["ECE461L-FinalProject-Database"]
users = database.EncryptedUsers
hwsets = database.HardwareSets
projects = database.Projects
app = Flask(__name__, static_folder='./build', static_url_path='/')
CORS(app)
bcrypt = Bcrypt(app)
@app.route("/")
def index():
return app.send_static_file('index.html')
@app.route('/projects/<string:userID>')
def getProjects(userID):
# Return projects userID is in
projectsList = []
for project in projects.find():
del project['_id']
if userID in project['Authorized Users']:
projectsList.append(project)
# Return list of hwsets
hwsetsList = []
for hwset in hwsets.find():
del hwset['_id']
hwsetsList.append(hwset)
return {
'projects' : projectsList,
'hwsets' : hwsetsList
}
@app.route('/checkin/<string:projectid>/<string:hwsetname>/<int:qty>')
def checkIn_hardware(projectid, hwsetname, qty):
hwset_query = {"Name": hwsetname}
hwset_document = hwsets.find_one(hwset_query)
available_units = hwset_document["Availability"]
project = projects.find_one({"ProjectID": projectid})
project_hardware = project['HWSets']
checked_out = project_hardware[hwsetname]
if qty > checked_out:
qty_checked_in = checked_out
else:
qty_checked_in = qty
project_hardware[hwsetname] = project_hardware[hwsetname] - qty_checked_in
projects.update_one({"ProjectID": projectid}, {"$set": {"HWSets": project_hardware}})
hwsets.update_one({"Name": hwsetname}, {"$set": {"Availability": qty_checked_in + available_units}})
return {
'projectid': projectid,
'hwsetname': hwsetname,
'qty': qty,
'response': f'{qty_checked_in} hardware checked into {hwsetname}'
}
@app.route('/checkout/<string:projectid>/<string:hwsetname>/<int:qty>')
def checkOut_hardware(projectid, hwsetname, qty):
hwset_query = {"Name": hwsetname}
hwset_document = hwsets.find_one(hwset_query)
availability = hwset_document["Availability"]
project = projects.find_one({"ProjectID": projectid})
project_hardware = project['HWSets']
if qty > availability:
qty_checked_out = availability
hwsets.update_one({"Name": hwsetname}, {"$set": {"Availability": 0}})
else:
qty_checked_out = qty
project_hardware[hwsetname] = project_hardware[hwsetname] + qty_checked_out
projects.update_one({"ProjectID": projectid}, {"$set": {"HWSets": project_hardware}})
hwsets.update_one({"Name": hwsetname}, {"$set": {"Availability": availability - qty_checked_out}})
return {
'projectid': projectid,
'hwsetname': hwsetname,
'qty': qty,
'response': f'{qty_checked_out} hardware checked out from {hwsetname}'
}
@app.route('/join/<string:projectid>/<string:userid>')
def joinProject(projectid, userid):
project = projects.find_one({"ProjectID": projectid})
users = project['Users']
users.append(userid)
projects.update_one({"ProjectID": projectid}, {"$set": {"Users": users}})
return {
'response': f'Joined {projectid}'
}
@app.route('/leave/<string:projectid>/<string:userid>')
def leaveProject(projectid, userid):
project = projects.find_one({"ProjectID": projectid})
users = project['Users']
users.remove(userid)
projects.update_one({"ProjectID": projectid}, {"$set": {"Users": users}})
return {
'response': f'Left {projectid}'
}
@app.route("/signup/<string:username>/<string:userID>/<string:password>/<string:confirmPassword>")
def signup(username, userID, password, confirmPassword):
if password == confirmPassword:
pw_hash = bcrypt.generate_password_hash(password).decode('utf-8')
id_hash = bcrypt.generate_password_hash(userID).decode('utf-8')
newUserDoc = {
'Username': username,
'UserID': id_hash,
'Password': pw_hash
}
result = False
for user in users.find():
idCheck = user['UserID']
result = bcrypt.check_password_hash(idCheck, userID)
if result:
response = 'account with that userID already exists'
break
if not result:
users.insert_one(newUserDoc)
response = 'new user created'
#if users.find_one({"UserID": userID}) != None:
# response = 'account with that userID already exists'
#else:
# users.insert_one(newUserDoc)
# response = 'new user created'
else:
response = 'passwords must match'
return {
'response': response,
'userID': userID
}
@app.route("/login/<string:userID>/<string:password>")
def login(userID, password):
result = False
for user in users.find():
user_document = user
idCheck = user['UserID']
result = bcrypt.check_password_hash(idCheck, userID)
if result:
break
if not result:
return {
'response' : 'userID not found'
}
#user_query = {"UserID": userID}
#user_document = users.find_one(user_query)
#if user_document == None:
# return {
# 'response' : 'userID not found'
# }
passwordCheck = user_document["Password"]
result = bcrypt.check_password_hash(passwordCheck, password)
if result:
response = 'successfully logged in'
else:
response = 'incorrect password'
return {
'response': response,
'userID': userID
}
# Create a project
@app.route("/createproject/<string:projectName>/<string:description>/<string:projectID>/<string:authorizedUsers>")
def createProject(projectName, description, projectID, authorizedUsers):
authorizedUserArray = authorizedUsers.split(',')
project_document = {
'ProjectID': projectID,
'ProjectName': projectName,
'Users': [],
'Project Description': description,
'HWSets': {'HWSet1': 0, 'HWSet2': 0},
'Authorized Users': authorizedUserArray
}
if projects.find_one({"ProjectID": projectID}) != None:
response = 'project with that ProjectID already exists'
else:
projects.insert_one(project_document)
response = 'new project created'
return {
'response': response
}
@app.route("/addusers/<string:userID>/<string:projectID>")
def adduser(userID, projectID):
result = False
for user in users.find():
user_document = user
idCheck = user['UserID']
result = bcrypt.check_password_hash(idCheck, userID)
if result:
break
if not result:
return {
'response' : 'user does not exist'
}
project = projects.find_one({"ProjectID": projectID})
authUsers = project['Authorized Users']
if(userID in authUsers):
return {
'response' : 'user already authorized'
}
authUsers.append(userID)
projects.update_one({"ProjectID": projectID}, {"$set": {"Authorized Users": authUsers}})
response = f'added {userID} to project {projectID}'
return {
'response' : response
}
if __name__ == "__main__":
app.run(host='0.0.0.0')
# Close the database collection
# client.close()