-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.py
239 lines (188 loc) · 7.16 KB
/
cli.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
import click
import requests
API_BASE_URL = "http://localhost:5001"
@click.group(invoke_without_command=True)
@click.pass_context
def cli(ctx) -> None:
"""LMS Command Line Interface"""
click.echo("If this is the first time you use this application, you can use the admin credentials")
click.echo("username=admin, password=admin")
click.echo("")
while True:
click.echo("Available actions:")
click.echo("1. Login to the app")
click.echo("2. Register a new user")
click.echo("3. List all current users")
click.echo("4. Create a Module")
click.echo("5. Create an Assignment")
click.echo("6. Submit a Grade")
click.echo("7. View Grades as a Student")
click.echo("8. Logout")
click.echo("9. Exit")
choice = click.prompt("Please select an action", type=int)
if choice == 1:
login()
elif choice == 2:
register_user()
elif choice == 3:
list_all_users()
elif choice == 4:
create_module()
elif choice == 5:
create_assignment()
elif choice == 6:
submit_grade()
elif choice == 7:
view_grades()
elif choice == 8:
logout()
elif choice == 9:
click.echo("Exiting...")
break
else:
click.echo("Invalid choice. Please select again.")
def register_user() -> None:
"""Register a new user."""
username = click.prompt("Please enter a username", type=str)
password = click.prompt("Please enter a password", type=str, hide_input=True)
role = click.prompt("Please enter a role (Admin, Teacher, Student)", type=str)
first_name = click.prompt("Please enter your first name", type=str)
last_name = click.prompt("Please enter your last name", type=str)
email = click.prompt("Please enter your email", type=str)
click.echo("")
user_data = {
"username": username,
"password": password,
"role": role,
"first_name": first_name,
"last_name": last_name,
"email": email,
}
response = requests.post(f"{API_BASE_URL}/users/create", json=user_data)
data = response.json()
message = data.get("message")
click.echo(message)
click.echo("")
def login() -> None:
"""Login to the system"""
username = click.prompt("Please enter a username", type=str)
password = click.prompt("Please enter a password", type=str, hide_input=True)
click.echo("")
user_data = {"username": username, "password": password}
response = requests.post(f"{API_BASE_URL}/login", json=user_data)
data = response.json()
message = data.get("message")
click.echo(message)
click.echo("")
def list_all_users() -> None:
"""List all available users in the system"""
response = requests.get(f"{API_BASE_URL}/users/list")
data = response.json()
if isinstance(data, dict):
message = data.get("message")
click.echo(message)
else:
start_number = 1
click.echo("")
click.echo("Current users in the system:")
for user in data:
click.echo(
f'- User {start_number}. first name: {user.get("first_name")}, '
f'last name: {user.get("last_name")}, username: {user.get("username")}'
)
start_number += 1
click.echo("")
def create_module() -> None:
"""Create a new module."""
title = click.prompt("Please enter the module title", type=str)
description = click.prompt("Please enter the module description", type=str)
click.echo("")
module_data = {"title": title, "description": description}
response = requests.post(f"{API_BASE_URL}/modules/create", json=module_data)
data = response.json()
message = data.get("message")
click.echo(message)
click.echo("")
def create_assignment() -> None:
"""Create a new assignment."""
click.echo("List of available modules to add an assignment to")
response = requests.get(f"{API_BASE_URL}/modules/list")
data = response.json()
if isinstance(data, dict):
message = data.get("message")
click.echo(message)
else:
click.echo("")
click.echo("Current modules in the system:")
for module in data:
click.echo(f'- Module ID: {module.get("id")}. Title: {module.get("title")}')
click.echo("")
title = click.prompt("Please enter the assignment title", type=str)
description = click.prompt("Please enter the assignment description", type=str)
module_id = click.prompt("Please enter the module ID", type=int)
due_date = click.prompt("Please enter the due date (YYYY-MM-DD)", type=str)
click.echo("")
assignment_data = {"title": title, "description": description, "module_id": module_id, "due_date": due_date}
response = requests.post(f"{API_BASE_URL}/assignments/create", json=assignment_data)
data = response.json()
message = data.get("message")
click.echo(message)
click.echo("")
def submit_grade() -> None:
"""Submit a grade."""
click.echo("List of available students")
response = requests.get(f"{API_BASE_URL}/users/list_students")
data = response.json()
if isinstance(data, dict):
message = data.get("message")
click.echo(message)
else:
click.echo("")
click.echo("Current students in the system:")
for student in data:
click.echo(
f'- Student ID: {student.get("id")}. Name: {student.get("first_name")} - {student.get("last_name")}'
)
response = requests.get(f"{API_BASE_URL}/assignments/list")
data = response.json()
if isinstance(data, dict):
message = data.get("message")
click.echo(message)
else:
click.echo("")
click.echo("Current assigments in the system:")
for assignment in data:
click.echo(f'- Assignment ID: {assignment.get("id")}. Title: {assignment.get("title")}')
student_id = click.prompt("Please enter the student ID", type=int)
assignment_id = click.prompt("Please enter the assignment ID", type=int)
score = click.prompt("Please enter the score", type=float)
click.echo("")
grade_data = {"student_id": student_id, "assignment_id": assignment_id, "score": score}
response = requests.post(f"{API_BASE_URL}/grades/create", json=grade_data)
data = response.json()
message = data.get("message")
click.echo(message)
click.echo("")
def view_grades() -> None:
"""View all grades as a student."""
response = requests.get(f"{API_BASE_URL}/grades/view")
data = response.json()
if isinstance(data, dict):
message = data.get("message")
click.echo(message)
else:
click.echo("")
click.echo("Your grades:")
for grade in data:
click.echo(f'- Assignment ID: {grade.get("assignment_id")}, Score: {grade.get("score")}')
click.echo("")
def logout() -> None:
"""Log out of the app"""
click.echo("")
response = requests.put(f"{API_BASE_URL}/logout")
data = response.json()
message = data.get("message")
click.echo(message)
click.echo("")
if __name__ == "__main__":
cli()