From cce5f55a2468abd51dc13e5cca4c7db9e6bed32a Mon Sep 17 00:00:00 2001 From: wqy Date: Sat, 3 Sep 2022 00:34:25 +0800 Subject: [PATCH] Title need undo redo --- api/s1_title.py | 16 ++++++++++++---- api/t_s1_title.py | 7 +++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/api/s1_title.py b/api/s1_title.py index 54b6bba..7a322cd 100644 --- a/api/s1_title.py +++ b/api/s1_title.py @@ -1,11 +1,19 @@ from psycopg.rows import dict_row +from operation import * from connection import g_conn_dict as conn -def set_title(name: str, value: str) -> None: - with conn[name].cursor() as cur: - cur.execute(f"update title set value = '{value}'") - def get_title(name: str) -> str: with conn[name].cursor(row_factory=dict_row) as cur: cur.execute(f"select * from title") return cur.fetchone()['value'] + +def set_title(name: str, value: str) -> None: + old = get_title(name) + + with conn[name].cursor() as cur: + sql = f"update title set value = '{value}'" + cur.execute(sql) + + redo = sql.replace("'", '"') + undo = f'update title set value = "{old}"' + add_operation(name, redo, undo) diff --git a/api/t_s1_title.py b/api/t_s1_title.py index 700c931..e5fa4e1 100644 --- a/api/t_s1_title.py +++ b/api/t_s1_title.py @@ -1,3 +1,4 @@ +from operation import execute_undo from project import * from s1_title import * @@ -20,5 +21,11 @@ print(get_title(p)) set_title(p, "test") print(get_title(p)) +execute_undo(p) +print(get_title(p)) + +execute_undo(p) +print(get_title(p)) + close_project(p) delete_project(p)