test-platform/server/delete-question.py

42 lines
1.2 KiB
Python
Raw Normal View History

2024-06-01 13:49:13 +03:00
import mariadb as mdb
import traceback
import json
import sys
import os
from httputils import parse_query, escape_sql_string
def readfile(path):
if os.path.exists(path):
return open(path).read()
args = {"host": "127.0.0.1",
"port": 3306,
"user": "root",
"password": "",
"database": "test_holder"}
settings = json.loads(readfile("cgi/db-settings.json"))
args.update(settings)
db_connection = mdb.connect(**args)
args = parse_query(os.environ['QUERY_STRING'])
if not 'id' in args:
print("Content-Type: text/plain; charset=UTF-8\r\n\r\nВи не зазначили ідентифікатор запитання\r\n")
sys.exit(0)
cur = db_connection.cursor()
try:
cur.execute(f"SELECT test.id FROM test JOIN question ON test.id = question.tstID WHERE question.id = {args['id']};")
test_id = iter(cur).__next__()[0]
cur.execute(f"DELETE FROM question WHERE id = {args['id']};")
db_connection.commit()
print(f"Location: /index.py?mode=view-test&id={test_id}\r\n\r\n")
except Exception as e:
print(f"Content-Type: text/plain; charset=UTF-8\r\n\r\nНе вдалося видалити запитання ({e})\r\n{traceback.format_exc()}\r\n")