test-platform/server/edit-response-option.py

46 lines
1.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)
if not 'label' 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"UPDATE response_option SET label = '{escape_sql_string(args['label'])}' WHERE id = {args['id']};")
db_connection.commit()
cur.execute(f"SELECT question.id FROM question JOIN response_option ON question.id = response_option.qstID WHERE response_option.id = {args['id']};")
quest_id = iter(cur).__next__()[0]
print(f"Location: /index.py?mode=view-question&id={quest_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")