Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit fb057be

Browse files
Completing 1.2.2
1 parent cef525f commit fb057be

File tree

7 files changed

+158
-50
lines changed

7 files changed

+158
-50
lines changed

.idea/csv-editor.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

API/API.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"api": "",
2+
"api": "RLR",
33
"username": "",
44
"password": "",
55
"exclusion_titles": [","]

API/API_FrameWork.py

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import requests
22

3+
err_codes = {
4+
400: "Bad Request - Failed to access database or Bad Inputs <p> Most likely either Client-Side Issue or Frontend Issue </p>",
5+
401: "Unauthorized Access - Incorrect password or Authentication failure <p> Most likely either Client-Side Issue or Frontend Issue </p>",
6+
404: "Not found - API request not correct / File Not found <p> Most likely either Client-Side Issue or Frontend Issue </p>",
7+
409: 'Conflict - Already exists, Duplicate entry or Resource already exists <p> <p> Most likely a Backend Issue, please report it here: <a href="https://github.com/DefinetlyNotAI/Test-generator/issues/new/choose">Here</a> </p>',
8+
500: "Internal Server Error - SQLite error or Server Crash error <p> Most likely either Client-Side Issue or Frontend Issue </p>",
9+
503: 'Service Unavailable - Server error due to missing resources <p> Most likely a Backend Issue, please report it here: <a href="https://github.com/DefinetlyNotAI/Test-generator/issues/new/choose">Here</a> </p>',
10+
520: 'Unknown error - Caught exception <p> Most likely a Backend Issue, please report it here: <a href="https://github.com/DefinetlyNotAI/Test-generator/issues/new/choose">Here</a></p>',
11+
}
12+
313

414
def framework():
515
"""
@@ -9,34 +19,36 @@ def framework():
919
msg (str): A message indicating the status of the operation.
1020
code (int): The HTTP status code of the response.
1121
"""
12-
url = "http://127.0.0.1:5000/upload"
13-
download_url = "http://127.0.0.1:5000/download_exam"
22+
config_upload_url = "http://127.0.0.1:5000/upload"
23+
exam_download_url = "http://127.0.0.1:5000/download_exam"
24+
log_download_url = "http://127.0.0.1:5000/download_log"
1425

1526
# Open files within the same scope as the POST request to ensure they stay open
1627
with open("db.config", "rb") as config_file, open(
17-
"API.json", "rb"
28+
"API.json", "rb"
1829
) as api_file, open(
1930
"Test.csv", "rb"
2031
) as csv_file: # Optional, can be omitted if not needed
2132
files = {
2233
"db.config": config_file,
2334
"API.json": api_file,
24-
"Test.csv": csv_file, # Include this line conditionally based on whether Test.csv is actually being uploaded
35+
"Test.csv": csv_file,
36+
# Include this line conditionally based on whether Test.csv is actually being uploaded
2537
}
2638
try:
27-
response = requests.post(url, files=files)
39+
response = requests.post(config_upload_url, files=files)
2840
# Check the response outside the 'with' block since the files are no longer needed
2941
code = response.status_code
3042
msg = response.text
3143
except requests.exceptions.RequestException as e:
3244
code = 503
3345
tempMessage = 'Service Unavailable - Server error <p> Most likely a Backend Issue, please report it here: <a href="https://github.com/DefinetlyNotAI/Test-generator/issues/new/choose">Here</a> </p>'
34-
msg = f"<html><body><h1>Error</h1><h2>Error Number: {code}</h2><p>{e}</p>{tempMessage}<p></p></body></html>"
46+
msg = f"<html><body><h1>Error</h1><h2>Error Number: {code}</h2><p>{e}</p><p>{tempMessage}</p></body></html>"
3547

3648
if code == 201:
3749
# Attempt to download Exam.xlsx
3850
try:
39-
download_response = requests.get(download_url)
51+
download_response = requests.get(exam_download_url)
4052
if download_response.status_code == 200:
4153
with open("Exam.xlsx", "wb") as f:
4254
f.write(download_response.content)
@@ -47,4 +59,38 @@ def framework():
4759
msg = e
4860
code = 500
4961

62+
elif code == 202:
63+
try:
64+
download_response = requests.get(log_download_url)
65+
if download_response.status_code == 200:
66+
with open("Server.log", "wb") as f:
67+
f.write(download_response.content)
68+
code = 200
69+
else:
70+
# Handle errors based on the status code
71+
if download_response.status_code == 401:
72+
code = 401 # Unauthorized
73+
tempMessage = "Unauthorized Access - Incorrect password or Authentication failure"
74+
msg = f"<html><body><h1>Error</h1><h2>Error Number: {code}</h2><p>{tempMessage}</p></body></html>"
75+
76+
elif download_response.status_code == 403:
77+
code = 403 # Forbidden
78+
tempMessage = "Forbidden Access - Missing Authorization header"
79+
msg = f"<html><body><h1>Error</h1><h2>Error Number: {code}</h2><p>{tempMessage}</p></body></html>"
80+
81+
else:
82+
code = 500 # Internal Server Error
83+
tempMessage = "An unexpected error occurred."
84+
msg = f"<html><body><h1>Error</h1><h2>Error Number: {code}</h2><p>{tempMessage}</p></body></html>"
85+
86+
except Exception as e:
87+
msg = e
88+
code = 500
89+
90+
if msg is None:
91+
msg = "An unexpected error occurred."
92+
5093
return msg, code
94+
95+
96+
print(framework())

DataBase.py

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import pandas as pd
1212

1313

14-
def check_for_LIST(value):
14+
def check_ERROR(value):
1515
"""
1616
Check if the input value contains the word 'ERROR'.
1717
@@ -303,18 +303,18 @@ def add_exclusion_db(name, titles, password, special=None):
303303
304304
Note:
305305
- The `um.add_exclusion_db_main()` function is called to add the exclusion to the database.
306-
- If the `check_for_LIST()` function returns True for the value returned by `um.add_exclusion_db_main()`, it is returned.
306+
- If the `check_ERROR()` function returns True for the value returned by `um.add_exclusion_db_main()`, it is returned.
307307
- If the `special` parameter is not provided, the `um.add_exclusion_db_main()` function is called with a comma and the password as arguments.
308308
- If an error occurs, a formatted string indicating the error is returned.
309309
310310
"""
311311
try:
312312
value = um.add_exclusion_db_main(name, titles, password)
313-
if check_for_LIST(value):
313+
if check_ERROR(value):
314314
return value
315315
if not special:
316316
msg = um.add_exclusion_db_main(name, ",", password)
317-
if check_for_LIST(msg):
317+
if check_ERROR(msg):
318318
return msg
319319
return value
320320
except Exception as e:
@@ -370,14 +370,14 @@ def extract_user_info(data):
370370
try:
371371
# Safely accessing the values from the user_data dictionary
372372
username = data.get("Username", "Unknown")
373-
if check_for_LIST(username):
373+
if check_ERROR(username):
374374
return username
375375
password = data.get("Password", "Unknown")
376-
if check_for_LIST(password):
376+
if check_ERROR(password):
377377
return username
378378
exclusion_titles = data.get("Exclusion_titles", [])
379379
if isinstance(exclusion_titles, str):
380-
if check_for_LIST(exclusion_titles):
380+
if check_ERROR(exclusion_titles):
381381
return exclusion_titles
382382

383383
return username, password, exclusion_titles
@@ -790,22 +790,22 @@ def exam_generator(username):
790790
# Read the CSV file and validate the config file
791791
questions = read_csv("Test.csv")
792792
if isinstance(questions, str):
793-
if check_for_LIST(questions):
793+
if check_ERROR(questions):
794794
return questions
795795

796796
config_data = read_config("db.config")
797797
if isinstance(config_data, str):
798-
if check_for_LIST(config_data):
798+
if check_ERROR(config_data):
799799
return config_data
800800

801801
Exclude_list = um.get_excluded_titles(username)
802802
if isinstance(Exclude_list, str):
803-
if check_for_LIST(Exclude_list):
803+
if check_ERROR(Exclude_list):
804804
return Exclude_list
805805

806806
temp = generate_exam(questions, config_data, Exclude_list)
807807
if isinstance(temp, str):
808-
if check_for_LIST(temp):
808+
if check_ERROR(temp):
809809
return temp
810810
else:
811811
exam, total_points, difficulty_ratios, total_titles = temp
@@ -838,7 +838,7 @@ def exam_generator(username):
838838
time.sleep(1)
839839

840840
msg = create_excel_from_txt(config_data["debug"])
841-
if check_for_LIST(msg):
841+
if check_ERROR(msg):
842842
return msg
843843

844844
return rf"""DOWNLOAD
@@ -860,10 +860,22 @@ def database_thread():
860860
try:
861861

862862
def init():
863+
"""
864+
Initializes the UserManager and API values based on the API configuration in the 'API.json' file.
865+
866+
Returns:
867+
- str: If the API configuration is invalid, returns a formatted error message.
868+
- str: If the API is 'REC', generates an exam based on the request and returns a formatted message.
869+
- str: If the API is 'RUG', creates a new user in the database based on the request and returns a formatted message.
870+
- str: If the API is 'RUD', adds exclusion titles to the database for a user based on the request and returns a formatted message.
871+
- str: If the API is 'RUR', removes a user from the database based on the request and returns a formatted message.
872+
- str: If the API is 'RLR', sends the server log and returns a formatted message.
873+
- str: If the API is invalid, returns a formatted error message.
874+
"""
863875
# Initialize the UserManager and API values
864876
temp = read_api()
865877
if isinstance(temp, str):
866-
if check_for_LIST(temp):
878+
if check_ERROR(temp):
867879
return temp
868880
else:
869881
api, username, password, exclusion_titles = temp
@@ -872,7 +884,7 @@ def init():
872884
log.info(f"A request has been made to generate an exam by the user {username}")
873885
if um.verify_password(username, password):
874886
DATA = exam_generator(username)
875-
if not check_for_LIST(DATA):
887+
if not check_ERROR(DATA):
876888
log.info("Exam generated successfully based on the request")
877889
else:
878890
DATA = "ERROR Invalid Username or Password && 401"
@@ -881,14 +893,14 @@ def init():
881893
f"A request has been made to create a new user by the following username {username}"
882894
)
883895
DATA = um.create_db(username, exclusion_titles)
884-
if not check_for_LIST(DATA):
896+
if not check_ERROR(DATA):
885897
log.info("User created successfully based on the request")
886898
elif api == "RUD":
887899
log.info(
888900
f"A request has been made to add the following exclusion titles {exclusion_titles} to the database for user {username}"
889901
)
890902
DATA = um.add_exclusion_db(username, exclusion_titles, password)
891-
if not check_for_LIST(DATA):
903+
if not check_ERROR(DATA):
892904
log.info(
893905
"Exclusion titles added successfully based on the request"
894906
)
@@ -897,8 +909,10 @@ def init():
897909
f"A request has been made to remove the user {username} from the database"
898910
)
899911
DATA = um.remove(username, password)
900-
if not check_for_LIST(DATA):
912+
if not check_ERROR(DATA):
901913
log.info("User removed successfully based on the request")
914+
elif api == "RLR":
915+
DATA = "LOG"
902916
else:
903917
DATA = "ERROR Invalid API && 404"
904918

ReadMe.md

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- [RUG API](#rug-api-)
2323
- [RUD API](#rud-api-)
2424
- [RUR API](#rur-api-)
25+
- [RLR API](#rlr-api-)
2526
- [Error Codes](#error-codes-)
2627
- [Framework Setup](#framework-setup-)
2728
- [Common Web Server Vulnerabilities](#common-web-server-vulnerabilities-)
@@ -137,9 +138,9 @@ If debug is true, the headers would be ['URL', 'Question', 'Title', 'Difficulty'
137138

138139
**Returns WEB UI:-**
139140
It will also return either one of 2 scenarios, If an error occurs,
140-
it makes it return an error code in html unless using the framework where it will return it in text.
141+
it makes it return an error code in html unless using the `log` where it will return it in text.
141142
OR if everything works fine it will return a confirmation message in html,
142-
unless using the framework where it will return it in text.
143+
unless using the `log` where it will return it in text.
143144

144145
**Required Format:-**
145146
API.json file format should be at minimum:
@@ -161,9 +162,9 @@ Request User Generation
161162

162163
**Returns:-**
163164
It will also return either one of 2 scenarios, If an error occurs,
164-
it makes it return an error code in html unless using the framework where it will return it in text.
165+
it makes it return an error code in html unless using the `log` where it will return it in text.
165166
OR if everything works fine it will return a confirmation message in html,
166-
unless using the framework where it will return it in text, usually it should also give you the password.
167+
unless using the `log` where it will return it in text, usually it should also give you the password.
167168

168169
**Required Format:-**
169170
API.json file format should be at minimum:
@@ -180,9 +181,9 @@ Request User DB Update
180181

181182
**Returns:-**
182183
It will also return either one of 2 scenarios, If an error occurs,
183-
it makes it return an error code in html unless using the framework where it will return it in text.
184+
it makes it return an error code in html unless using the `log` where it will return it in text.
184185
OR if everything works fine it will return a confirmation message in html,
185-
unless using the framework where it will return it in text.
186+
unless using the `log` where it will return it in text.
186187

187188
**Required Format:-**
188189
API.json file format should be at minimum:
@@ -200,19 +201,41 @@ Request User Removal
200201

201202
**Returns:-**
202203
It will also return either one of 2 scenarios, If an error occurs,
203-
it makes it return an error code in html unless using the framework where it will return it in text.
204+
it makes it return an error code in html unless using the `log` where it will return it in text.
204205
OR if everything works fine it will return a confirmation message in html,
205-
unless using the framework where it will return it in text.
206+
unless using the `log` where it will return it in text.
206207

207208
**Required Format:-**
208209
API.json file format should be at minimum:
210+
209211
```text
210212
{
211213
"api": "RUR",
212214
"username": "REPLACE_WITH_USERNAME",
213215
"password": "REPLACE_WITH_PASSWORD",
214216
}
215217
```
218+
219+
### RLR API 🔍
220+
221+
Request Log Retrieval
222+
223+
**Returns:-**
224+
It will also return either one of 2 scenarios, If an error occurs,
225+
it makes it return an error code in html unless using the `log` where it will return it in text.
226+
OR if everything works fine it will return a confirmation message in html,
227+
unless using the `log` where it will return it in text as well as the `Server.log`.
228+
229+
**Required Format:-**
230+
API.json file format should be at minimum:
231+
```text
232+
{
233+
"api": "RLR",
234+
}
235+
```
236+
237+
Do be careful, this sends the log back to you, by all means you need to be careful while using it.
238+
216239
## Error Codes ❌
217240

218241
In case of errors, the system returns specific HTTP status codes.

0 commit comments

Comments
 (0)