diff --git a/sql/db_create.sql b/sql/db_create.sql index 08232aa..8d4f86a 100644 --- a/sql/db_create.sql +++ b/sql/db_create.sql @@ -50,8 +50,8 @@ create table document doc_type varchar(100) NOT NULL, book_id int NOT NULL, user_id int NOT NULL, - CONSTRAINT `fk_document_user_id` FOREIGN KEY (`user_id`) REFERENCES user(`user_id`) ON DELETE CASCADE, - CONSTRAINT `fk_document_book_id` FOREIGN KEY (`book_id`) REFERENCES book(`book_id`) ON DELETE CASCADE + CONSTRAINT `fk_document_user_id` FOREIGN KEY (`user_id`) REFERENCES user(`user_id`) ON DELETE RESTRICT, + CONSTRAINT `fk_document_book_id` FOREIGN KEY (`book_id`) REFERENCES book(`book_id`) ON DELETE RESTRICT ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; create index `idx_doc_name` on document(doc_name); create table note diff --git a/sql/trigger.sql b/sql/trigger.sql index 3863952..7c48207 100644 --- a/sql/trigger.sql +++ b/sql/trigger.sql @@ -71,4 +71,45 @@ create trigger `trig_update_stat_note_del` for each row begin update user_stat set user_notecount=user_notecount-1 where user_stat.user_id=OLD.user_id; end ## -DELIMITER ; \ No newline at end of file +DELIMITER ; + +drop procedure if exists `clean_up_type_author`; +DELIMITER ## + +create procedure `clean_up_type_author`(in uid_in int) begin +declare done boolean default false; +declare v_bookid int; +declare cur cursor for select distinct book_id from book where book.user_id = uid_in; +declare continue handler for not found set done=true; + + open cur; +mainloop: loop + fetch cur into v_bookid; + if done then + leave mainloop; + end if; + delete from typetable where type_id in (select type_id from book_type where book_id=v_bookid); + delete from book_type where book_id = v_bookid; + end loop; + close cur; +end ## + +DELIMITER ; + +drop procedure if exists `add_new_book_type`; +DELIMITER ## +create procedure `add_new_book_type`(in bookid_in int, in typename_in varchar(200)) begin +declare isnewtype int default 0; +declare isduplicated int default 0; +declare v_typeid int; + select count(type_name) into isnewtype from typetable where type_name=typename_in; + if isnewtype=0 then + insert into typetable (`type_name`) values (typename_in); + end if; + select type_id into v_typeid from typetable where type_name = typename_in; + select count(book_id) into isduplicated from book_type where type_id=v_typeid and book_id=bookid_in; + if isduplicated=0 then + insert into book_type (`type_id`,`book_id`) values (v_typeid, bookid_in); + end if; +end ## +DELIMITER ; diff --git a/src/admin.py b/src/admin.py index 904f674..d2c336b 100644 --- a/src/admin.py +++ b/src/admin.py @@ -2,6 +2,7 @@ from flask import ( Blueprint, flash, g, redirect, render_template, request, url_for ) import re +import os from werkzeug.exceptions import abort from werkzeug.security import generate_password_hash @@ -69,21 +70,39 @@ def adduser(): flash(error) return render_template("admin/adduser.html", cur_time=datetime.now()) +def remove_user_doc(uid): + db = get_db() + cur = db.cursor() + cur.execute("select * from document where user_id=%s", (uid)) + documents = cur.fetchall() + for document in documents: + docpath = os.path.join(os.getcwd(),document['doc_url']) + if os.path.exists(docpath): + os.remove(docpath) + error = None + try: + cur.execute("delete from document where user_id=%s", (uid)) + db.commit() + except pymysql.Error as _e: + error = "严重错误:%s; 请联系管理员解决问题" % (_e) + db.rollback() + cur.close() + return error + @bp.route('/removeuser', methods=("GET",)) @admin_login_required def removeuser(): - error = "请求错误" - if request.method == 'GET': - error = None - uid_to_del = request.args.get("uid") - if uid_to_del is not None and uid_to_del.isdecimal: - db = get_db() - cur = db.cursor() - rowcnt = 0 + error = None + uid_to_del = request.args.get("uid") + if uid_to_del is not None and uid_to_del.isdecimal: + db = get_db() + cur = db.cursor() + rowcnt = 0 + error = remove_user_doc(uid_to_del) + if error is None: try: - rowcnt = cur.execute( - "delete from user where user.user_id=%s", (uid_to_del, ) - ) + cur.callproc('clean_up_type_author', args=(uid_to_del,)) + rowcnt = cur.execute("delete from user where user.user_id=%s", (uid_to_del, )) db.commit() except pymysql.IntegrityError as _e: error = "用户未做好被删除的准备:%s" % (_e) @@ -91,8 +110,7 @@ def removeuser(): except pymysql.Error as _e: error = "删除发生未知错误: %s" %(_e) db.rollback() - finally: - cur.close() + cur.close() if error is None: if rowcnt == 0: error = "由于uid不存在,可能没有被删除,请再次检查删除结果" diff --git a/src/templates/admin/adduser.html b/src/templates/admin/adduser.html index c1d2060..f76585b 100644 --- a/src/templates/admin/adduser.html +++ b/src/templates/admin/adduser.html @@ -52,7 +52,7 @@