Testys commited on
Commit
d1ae182
·
verified ·
1 Parent(s): 8f6e099

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +165 -116
src/streamlit_app.py CHANGED
@@ -19,15 +19,17 @@ def login(username, password):
19
  st.session_state.user = username
20
  st.success("Logged in successfully!")
21
  else:
22
- st.error("Login failed!")
23
 
24
  def logout():
25
  st.session_state.token = None
26
  st.session_state.user = None
27
  st.success("Logged out!")
28
 
29
- def api_call(method, endpoint, data=None, headers=None):
30
  url = f"{BASE_URL}{endpoint}"
 
 
31
  if headers is None:
32
  headers = {}
33
  if st.session_state.token:
@@ -67,12 +69,13 @@ else:
67
  skip = st.number_input("Skip", min_value=0, value=0)
68
  limit = st.number_input("Limit", min_value=1, value=100)
69
  if st.button("Get Students"):
70
- response = api_call('GET', f'/admin/students/?skip={skip}&limit={limit}')
 
71
  if response.status_code == 200:
72
  students = response.json()
73
  st.json(students)
74
  else:
75
- st.error("Failed to fetch students")
76
  elif action == "Create":
77
  full_name = st.text_input("Full Name")
78
  matric_no = st.text_input("Matric No")
@@ -80,215 +83,261 @@ else:
80
  department = st.selectbox("Department", ["Computer Science", "Engineering", "Business Administration", "Law", "Medicine"])
81
  password = st.text_input("Password", type="password")
82
  if st.button("Create Student"):
83
- data = {"full_name": full_name, "matric_no": matric_no, "email": email, "department": department, "password": password}
84
- response = api_call('POST', '/admin/students/', data)
 
 
 
 
 
 
85
  if response.status_code == 201:
86
  st.success("Student created!")
87
  st.json(response.json())
88
  else:
89
- st.error("Failed to create student")
90
  elif action == "Lookup":
91
- lookup_type = st.selectbox("Lookup by", ["Matric No", "Tag ID"])
92
- if lookup_type == "Matric No":
93
- matric_no = st.text_input("Matric No")
94
- if st.button("Lookup"):
95
- response = api_call('GET', f'/admin/students/lookup?matric_no={matric_no}')
96
- if response.status_code == 200:
97
- st.json(response.json())
98
- else:
99
- st.error("Student not found")
100
- else:
101
- tag_id = st.text_input("Tag ID")
102
- if st.button("Lookup"):
103
- response = api_call('GET', f'/admin/students/lookup?tag_id={tag_id}')
104
  if response.status_code == 200:
105
  st.json(response.json())
106
  else:
107
- st.error("Student not found")
108
  elif action == "Read Single":
109
  student_id = st.number_input("Student ID", min_value=1)
110
- if st.button("Read"):
111
  response = api_call('GET', f'/admin/students/{student_id}')
112
  if response.status_code == 200:
113
  st.json(response.json())
114
  else:
115
- st.error("Student not found")
116
  elif action == "Update":
117
  student_id = st.number_input("Student ID", min_value=1)
118
- full_name = st.text_input("Full Name")
119
- department = st.selectbox("Department", ["Computer Science", "Engineering", "Business Administration", "Law", "Medicine"])
120
- email = st.text_input("Email")
121
- if st.button("Update"):
122
- data = {"full_name": full_name, "department": department, "email": email}
123
- response = api_call('PUT', f'/admin/students/{student_id}', data)
124
- if response.status_code == 200:
125
- st.success("Student updated!")
126
- st.json(response.json())
 
 
 
 
127
  else:
128
- st.error("Failed to update student")
 
 
 
 
 
129
  elif action == "Delete":
130
  student_id = st.number_input("Student ID", min_value=1)
131
- if st.button("Delete"):
132
  response = api_call('DELETE', f'/admin/students/{student_id}')
133
  if response.status_code == 200:
134
  st.success("Student deleted!")
135
  st.json(response.json())
136
  else:
137
- st.error("Failed to delete student")
138
 
139
  elif page == "Users":
140
  st.header("User Management")
141
- action = st.selectbox("Action", ["Create", "List", "Lookup", "Update", "Delete"])
142
- if action == "Create":
 
 
 
 
 
 
 
 
 
 
 
143
  username = st.text_input("Username")
144
- password = st.text_input("Password", type="password")
145
  email = st.text_input("Email")
146
  full_name = st.text_input("Full Name")
 
147
  role = st.selectbox("Role", ["admin", "staff"])
148
- department = st.selectbox("Department", ["Computer Science", "Engineering", "Business Administration", "Law", "Medicine"])
149
  if st.button("Create User"):
150
- data = {"username": username, "password": password, "email": email, "full_name": full_name, "role": role, "department": department}
151
- response = api_call('POST', '/admin/users/', data)
 
 
 
 
 
 
 
152
  if response.status_code == 201:
153
  st.success("User created!")
154
  st.json(response.json())
155
  else:
156
- st.error("Failed to create user")
157
- elif action == "List":
158
- if st.button("Get Users"):
159
- response = api_call('GET', '/admin/users/')
 
160
  if response.status_code == 200:
161
- users = response.json()
162
- st.json(users)
163
  else:
164
- st.error("Failed to fetch users")
165
- elif action == "Lookup":
166
- lookup_type = st.selectbox("Lookup by", ["Username", "Tag ID"])
167
- if lookup_type == "Username":
168
- username = st.text_input("Username")
169
- if st.button("Lookup"):
170
- response = api_call('GET', f'/admin/users/lookup?username={username}')
171
- if response.status_code == 200:
172
- st.json(response.json())
173
- else:
174
- st.error("User not found")
175
- else:
176
- tag_id = st.text_input("Tag ID")
177
- if st.button("Lookup"):
178
- response = api_call('GET', f'/admin/users/lookup?tag_id={tag_id}')
179
- if response.status_code == 200:
180
- st.json(response.json())
181
- else:
182
- st.error("User not found")
183
  elif action == "Update":
184
  user_id = st.number_input("User ID", min_value=1)
185
- username = st.text_input("Username")
186
- email = st.text_input("Email")
187
- full_name = st.text_input("Full Name")
188
- role = st.selectbox("Role", ["admin", "staff"])
189
- department = st.selectbox("Department", ["Computer Science", "Engineering", "Business Administration", "Law", "Medicine"])
190
- password = st.text_input("Password", type="password")
191
- if st.button("Update"):
192
- data = {"username": username, "email": email, "full_name": full_name, "role": role, "department": department, "password": password}
193
- response = api_call('PUT', f'/admin/users/{user_id}', data)
194
- if response.status_code == 200:
195
- st.success("User updated!")
196
- st.json(response.json())
 
 
 
 
 
 
 
 
 
 
197
  else:
198
- st.error("Failed to update user")
 
 
 
 
 
199
  elif action == "Delete":
200
  user_id = st.number_input("User ID", min_value=1)
201
- if st.button("Delete"):
202
  response = api_call('DELETE', f'/admin/users/{user_id}')
203
  if response.status_code == 200:
204
  st.success("User deleted!")
205
  st.json(response.json())
206
  else:
207
- st.error("Failed to delete user")
208
 
209
  elif page == "Tags":
210
  st.header("Tag Management")
211
  action = st.selectbox("Action", ["Link", "Unlink"])
212
  if action == "Link":
213
  tag_id = st.text_input("Tag ID")
214
- matric_no = st.text_input("Matric No")
215
- username = st.text_input("Username")
216
  if st.button("Link Tag"):
217
- data = {"tag_id": tag_id, "matric_no": matric_no, "username": username}
218
- response = api_call('POST', '/admin/tags/link', data)
219
- if response.status_code == 200:
220
- st.success("Tag linked!")
221
- st.json(response.json())
222
  else:
223
- st.error("Failed to link tag")
 
 
 
 
 
 
 
 
 
 
224
  elif action == "Unlink":
225
  tag_id = st.text_input("Tag ID")
226
  if st.button("Unlink Tag"):
227
- response = api_call('DELETE', f'/admin/tags/{tag_id}/unlink')
228
  if response.status_code == 200:
229
  st.success("Tag unlinked!")
230
  st.json(response.json())
231
  else:
232
- st.error("Failed to unlink tag")
233
 
234
  elif page == "Devices":
235
  st.header("Device Management")
236
- action = st.selectbox("Action", ["Create", "List", "Delete"])
237
- if action == "Create":
 
 
 
 
 
 
 
 
238
  device_name = st.text_input("Device Name")
239
  location = st.text_input("Location")
240
  department = st.selectbox("Department", ["Computer Science", "Engineering", "Business Administration", "Law", "Medicine"])
241
  if st.button("Create Device"):
242
- data = {"device_name": device_name, "location": location, "department": department}
243
- response = api_call('POST', '/admin/devices/', data)
 
 
 
 
244
  if response.status_code == 201:
245
  st.success("Device created!")
246
  st.json(response.json())
247
  else:
248
- st.error("Failed to create device")
249
- elif action == "List":
250
- if st.button("Get Devices"):
251
- response = api_call('GET', '/admin/devices/')
252
- if response.status_code == 200:
253
- devices = response.json()
254
- st.json(devices)
255
- else:
256
- st.error("Failed to fetch devices")
257
  elif action == "Delete":
258
  device_id = st.number_input("Device ID", min_value=1)
259
- if st.button("Delete"):
260
  response = api_call('DELETE', f'/admin/devices/{device_id}')
261
  if response.status_code == 200:
262
  st.success("Device deleted!")
263
  st.json(response.json())
264
  else:
265
- st.error("Failed to delete device")
266
 
267
  elif page == "Clearance":
268
  st.header("Clearance Management")
269
  matric_no = st.text_input("Matric No")
270
  department = st.selectbox("Department", ["Library", "Student Affairs", "Bursary", "Academic Affairs", "Health Center"])
271
  status = st.selectbox("Status", ["pending", "approved", "rejected"])
272
- remarks = st.text_area("Remarks")
273
  if st.button("Update Clearance"):
274
- data = {"matric_no": matric_no, "department": department, "status": status, "remarks": remarks}
275
- response = api_call('PUT', '/clearance/update', data)
 
 
 
 
 
276
  if response.status_code == 200:
277
  st.success("Clearance updated!")
278
  st.json(response.json())
279
  else:
280
- st.error("Failed to update clearance")
281
 
282
  elif page == "RFID":
283
  st.header("RFID Check Status")
284
  tag_id = st.text_input("Tag ID")
285
  if st.button("Check Status"):
286
  data = {"tag_id": tag_id}
287
- response = api_call('POST', '/rfid/check-status', data)
 
288
  if response.status_code == 200:
289
  st.json(response.json())
290
  else:
291
- st.error("Failed to check status")
292
 
293
  elif page == "Scanners":
294
  st.header("Scanner Management")
@@ -297,18 +346,18 @@ else:
297
  api_key = st.text_input("Device API Key")
298
  if st.button("Activate Scanner"):
299
  data = {"api_key": api_key}
300
- response = api_call('POST', '/rfid/scanners/activate', data)
301
  if response.status_code == 204:
302
  st.success("Scanner activated!")
303
  else:
304
- st.error("Failed to activate")
305
  elif action == "Retrieve":
306
  if st.button("Retrieve Tag"):
307
  response = api_call('GET', '/rfid/scanners/retrieve')
308
  if response.status_code == 200:
309
  st.json(response.json())
310
  else:
311
- st.error("No tag scanned yet")
312
 
313
  elif page == "Profile":
314
  st.header("My Profile")
@@ -317,4 +366,4 @@ else:
317
  if response.status_code == 200:
318
  st.json(response.json())
319
  else:
320
- st.error("Failed to fetch profile")
 
19
  st.session_state.user = username
20
  st.success("Logged in successfully!")
21
  else:
22
+ st.error(f"Login failed: {response.text}")
23
 
24
  def logout():
25
  st.session_state.token = None
26
  st.session_state.user = None
27
  st.success("Logged out!")
28
 
29
+ def api_call(method, endpoint, params=None, data=None, headers=None):
30
  url = f"{BASE_URL}{endpoint}"
31
+ if params:
32
+ url += '?' + '&'.join([f"{k}={v}" for k, v in params.items()])
33
  if headers is None:
34
  headers = {}
35
  if st.session_state.token:
 
69
  skip = st.number_input("Skip", min_value=0, value=0)
70
  limit = st.number_input("Limit", min_value=1, value=100)
71
  if st.button("Get Students"):
72
+ params = {"skip": skip, "limit": limit}
73
+ response = api_call('GET', '/admin/students/', params=params)
74
  if response.status_code == 200:
75
  students = response.json()
76
  st.json(students)
77
  else:
78
+ st.error(f"Failed to fetch students: {response.text}")
79
  elif action == "Create":
80
  full_name = st.text_input("Full Name")
81
  matric_no = st.text_input("Matric No")
 
83
  department = st.selectbox("Department", ["Computer Science", "Engineering", "Business Administration", "Law", "Medicine"])
84
  password = st.text_input("Password", type="password")
85
  if st.button("Create Student"):
86
+ data = {
87
+ "full_name": full_name,
88
+ "matric_no": matric_no,
89
+ "email": email,
90
+ "department": department,
91
+ "password": password
92
+ }
93
+ response = api_call('POST', '/admin/students/', data=data)
94
  if response.status_code == 201:
95
  st.success("Student created!")
96
  st.json(response.json())
97
  else:
98
+ st.error(f"Failed to create student: {response.text}")
99
  elif action == "Lookup":
100
+ matric_no = st.text_input("Matric No (optional)")
101
+ tag_id = st.text_input("Tag ID (optional)")
102
+ if st.button("Lookup Student"):
103
+ if not matric_no and not tag_id:
104
+ st.error("Provide either Matric No or Tag ID")
105
+ else:
106
+ params = {}
107
+ if matric_no:
108
+ params["matric_no"] = matric_no
109
+ if tag_id:
110
+ params["tag_id"] = tag_id
111
+ response = api_call('GET', '/admin/students/lookup', params=params)
 
112
  if response.status_code == 200:
113
  st.json(response.json())
114
  else:
115
+ st.error(f"Failed to lookup student: {response.text}")
116
  elif action == "Read Single":
117
  student_id = st.number_input("Student ID", min_value=1)
118
+ if st.button("Get Student"):
119
  response = api_call('GET', f'/admin/students/{student_id}')
120
  if response.status_code == 200:
121
  st.json(response.json())
122
  else:
123
+ st.error(f"Failed to fetch student: {response.text}")
124
  elif action == "Update":
125
  student_id = st.number_input("Student ID", min_value=1)
126
+ full_name = st.text_input("Full Name (optional)")
127
+ email = st.text_input("Email (optional)")
128
+ department = st.selectbox("Department (optional)", [None, "Computer Science", "Engineering", "Business Administration", "Law", "Medicine"])
129
+ if st.button("Update Student"):
130
+ data = {}
131
+ if full_name:
132
+ data["full_name"] = full_name
133
+ if email:
134
+ data["email"] = email
135
+ if department:
136
+ data["department"] = department
137
+ if not data:
138
+ st.error("Provide at least one field to update")
139
  else:
140
+ response = api_call('PUT', f'/admin/students/{student_id}', data=data)
141
+ if response.status_code == 200:
142
+ st.success("Student updated!")
143
+ st.json(response.json())
144
+ else:
145
+ st.error(f"Failed to update student: {response.text}")
146
  elif action == "Delete":
147
  student_id = st.number_input("Student ID", min_value=1)
148
+ if st.button("Delete Student"):
149
  response = api_call('DELETE', f'/admin/students/{student_id}')
150
  if response.status_code == 200:
151
  st.success("Student deleted!")
152
  st.json(response.json())
153
  else:
154
+ st.error(f"Failed to delete student: {response.text}")
155
 
156
  elif page == "Users":
157
  st.header("User Management")
158
+ action = st.selectbox("Action", ["List", "Create", "Read Single", "Update", "Delete"])
159
+ if action == "List":
160
+ skip = st.number_input("Skip", min_value=0, value=0)
161
+ limit = st.number_input("Limit", min_value=1, value=100)
162
+ if st.button("Get Users"):
163
+ params = {"skip": skip, "limit": limit}
164
+ response = api_call('GET', '/admin/users/', params=params)
165
+ if response.status_code == 200:
166
+ users = response.json()
167
+ st.json(users)
168
+ else:
169
+ st.error(f"Failed to fetch users: {response.text}")
170
+ elif action == "Create":
171
  username = st.text_input("Username")
 
172
  email = st.text_input("Email")
173
  full_name = st.text_input("Full Name")
174
+ password = st.text_input("Password", type="password")
175
  role = st.selectbox("Role", ["admin", "staff"])
176
+ department = st.selectbox("Department (optional)", [None, "Computer Science", "Engineering", "Business Administration", "Law", "Medicine"])
177
  if st.button("Create User"):
178
+ data = {
179
+ "username": username,
180
+ "email": email,
181
+ "full_name": full_name,
182
+ "password": password,
183
+ "role": role,
184
+ "department": department
185
+ }
186
+ response = api_call('POST', '/admin/users/', data=data)
187
  if response.status_code == 201:
188
  st.success("User created!")
189
  st.json(response.json())
190
  else:
191
+ st.error(f"Failed to create user: {response.text}")
192
+ elif action == "Read Single":
193
+ user_id = st.number_input("User ID", min_value=1)
194
+ if st.button("Get User"):
195
+ response = api_call('GET', f'/admin/users/{user_id}')
196
  if response.status_code == 200:
197
+ st.json(response.json())
 
198
  else:
199
+ st.error(f"Failed to fetch user: {response.text}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
200
  elif action == "Update":
201
  user_id = st.number_input("User ID", min_value=1)
202
+ username = st.text_input("Username (optional)")
203
+ email = st.text_input("Email (optional)")
204
+ full_name = st.text_input("Full Name (optional)")
205
+ password = st.text_input("Password (optional)", type="password")
206
+ role = st.selectbox("Role (optional)", [None, "admin", "staff"])
207
+ department = st.selectbox("Department (optional)", [None, "Computer Science", "Engineering", "Business Administration", "Law", "Medicine"])
208
+ if st.button("Update User"):
209
+ data = {}
210
+ if username:
211
+ data["username"] = username
212
+ if email:
213
+ data["email"] = email
214
+ if full_name:
215
+ data["full_name"] = full_name
216
+ if password:
217
+ data["password"] = password
218
+ if role:
219
+ data["role"] = role
220
+ if department:
221
+ data["department"] = department
222
+ if not data:
223
+ st.error("Provide at least one field to update")
224
  else:
225
+ response = api_call('PUT', f'/admin/users/{user_id}', data=data)
226
+ if response.status_code == 200:
227
+ st.success("User updated!")
228
+ st.json(response.json())
229
+ else:
230
+ st.error(f"Failed to update user: {response.text}")
231
  elif action == "Delete":
232
  user_id = st.number_input("User ID", min_value=1)
233
+ if st.button("Delete User"):
234
  response = api_call('DELETE', f'/admin/users/{user_id}')
235
  if response.status_code == 200:
236
  st.success("User deleted!")
237
  st.json(response.json())
238
  else:
239
+ st.error(f"Failed to delete user: {response.text}")
240
 
241
  elif page == "Tags":
242
  st.header("Tag Management")
243
  action = st.selectbox("Action", ["Link", "Unlink"])
244
  if action == "Link":
245
  tag_id = st.text_input("Tag ID")
246
+ matric_no = st.text_input("Matric No (for student, optional)")
247
+ username = st.text_input("Username (for user, optional)")
248
  if st.button("Link Tag"):
249
+ if not matric_no and not username:
250
+ st.error("Provide either Matric No or Username")
 
 
 
251
  else:
252
+ data = {"tag_id": tag_id}
253
+ if matric_no:
254
+ data["matric_no"] = matric_no
255
+ if username:
256
+ data["username"] = username
257
+ response = api_call('POST', '/admin/tags/link', data=data)
258
+ if response.status_code == 200:
259
+ st.success("Tag linked!")
260
+ st.json(response.json())
261
+ else:
262
+ st.error(f"Failed to link tag: {response.text}")
263
  elif action == "Unlink":
264
  tag_id = st.text_input("Tag ID")
265
  if st.button("Unlink Tag"):
266
+ response = api_call('DELETE', f'/admin/tags/unlink/{tag_id}')
267
  if response.status_code == 200:
268
  st.success("Tag unlinked!")
269
  st.json(response.json())
270
  else:
271
+ st.error(f"Failed to unlink tag: {response.text}")
272
 
273
  elif page == "Devices":
274
  st.header("Device Management")
275
+ action = st.selectbox("Action", ["List", "Create", "Delete"])
276
+ if action == "List":
277
+ if st.button("Get Devices"):
278
+ response = api_call('GET', '/admin/devices/')
279
+ if response.status_code == 200:
280
+ devices = response.json()
281
+ st.json(devices)
282
+ else:
283
+ st.error(f"Failed to fetch devices: {response.text}")
284
+ elif action == "Create":
285
  device_name = st.text_input("Device Name")
286
  location = st.text_input("Location")
287
  department = st.selectbox("Department", ["Computer Science", "Engineering", "Business Administration", "Law", "Medicine"])
288
  if st.button("Create Device"):
289
+ data = {
290
+ "device_name": device_name,
291
+ "location": location,
292
+ "department": department
293
+ }
294
+ response = api_call('POST', '/admin/devices/', data=data)
295
  if response.status_code == 201:
296
  st.success("Device created!")
297
  st.json(response.json())
298
  else:
299
+ st.error(f"Failed to create device: {response.text}")
 
 
 
 
 
 
 
 
300
  elif action == "Delete":
301
  device_id = st.number_input("Device ID", min_value=1)
302
+ if st.button("Delete Device"):
303
  response = api_call('DELETE', f'/admin/devices/{device_id}')
304
  if response.status_code == 200:
305
  st.success("Device deleted!")
306
  st.json(response.json())
307
  else:
308
+ st.error(f"Failed to delete device: {response.text}")
309
 
310
  elif page == "Clearance":
311
  st.header("Clearance Management")
312
  matric_no = st.text_input("Matric No")
313
  department = st.selectbox("Department", ["Library", "Student Affairs", "Bursary", "Academic Affairs", "Health Center"])
314
  status = st.selectbox("Status", ["pending", "approved", "rejected"])
315
+ remarks = st.text_area("Remarks (optional)")
316
  if st.button("Update Clearance"):
317
+ data = {
318
+ "matric_no": matric_no,
319
+ "department": department,
320
+ "status": status,
321
+ "remarks": remarks if remarks else None
322
+ }
323
+ response = api_call('PUT', '/clearance/update', data=data)
324
  if response.status_code == 200:
325
  st.success("Clearance updated!")
326
  st.json(response.json())
327
  else:
328
+ st.error(f"Failed to update clearance: {response.text}")
329
 
330
  elif page == "RFID":
331
  st.header("RFID Check Status")
332
  tag_id = st.text_input("Tag ID")
333
  if st.button("Check Status"):
334
  data = {"tag_id": tag_id}
335
+ headers = {'x-api-key': 'some_api_key_if_needed'} # If required, but backend uses Security
336
+ response = api_call('POST', '/rfid/check-status', data=data, headers=headers)
337
  if response.status_code == 200:
338
  st.json(response.json())
339
  else:
340
+ st.error(f"Failed to check status: {response.text}")
341
 
342
  elif page == "Scanners":
343
  st.header("Scanner Management")
 
346
  api_key = st.text_input("Device API Key")
347
  if st.button("Activate Scanner"):
348
  data = {"api_key": api_key}
349
+ response = api_call('POST', '/rfid/scanners/activate', data=data)
350
  if response.status_code == 204:
351
  st.success("Scanner activated!")
352
  else:
353
+ st.error(f"Failed to activate: {response.text}")
354
  elif action == "Retrieve":
355
  if st.button("Retrieve Tag"):
356
  response = api_call('GET', '/rfid/scanners/retrieve')
357
  if response.status_code == 200:
358
  st.json(response.json())
359
  else:
360
+ st.error(f"No tag scanned yet: {response.text}")
361
 
362
  elif page == "Profile":
363
  st.header("My Profile")
 
366
  if response.status_code == 200:
367
  st.json(response.json())
368
  else:
369
+ st.error(f"Failed to fetch profile: {response.text}")