test-platform/server/create-test.py

41 lines
1.0 KiB
Python
Raw Normal View History

2024-05-28 17:47:08 +03:00
import mariadb as mdb
import json
import sys
import os
from httputils import parse_query
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 'name' 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"INSERT INTO test ( name ) VALUES ( '{args['name']}' );")
db_connection.commit()
cur.execute(f"SELECT id FROM test ORDER BY id DESC;")
new_id = iter(cur).__next__()[0]
print(f"Location: /index.py?mode=view-test&id={new_id}\r\n\r\n")
except:
print(f"Content-Type: text/plain\r\n\r\nНе вдалося створити новий тест\r\n")