add sql proc to fuck the retarded requirements

This commit is contained in:
ridethepig 2022-12-12 13:31:06 +08:00
parent 7fb945217f
commit 1ebba3d0dd
5 changed files with 92 additions and 36 deletions

View File

@ -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

View File

@ -72,3 +72,44 @@ create trigger `trig_update_stat_note_del`
update user_stat set user_notecount=user_notecount-1 where user_stat.user_id=OLD.user_id;
end ##
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 ;

View File

@ -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 = 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,7 +110,6 @@ def removeuser():
except pymysql.Error as _e:
error = "删除发生未知错误: %s" %(_e)
db.rollback()
finally:
cur.close()
if error is None:
if rowcnt == 0:

View File

@ -52,7 +52,7 @@
</div>
<div class="form-group">
<div class="col-3 text-center">
<label class="form-label" for="userlimit">用户空间大小</label>
<label class="form-label" for="userlimit">用户空间大小(GB)</label>
</div>
<div class="col-8">
<input class="form-input" name="userlimit" id="userlimit" required>

View File

@ -66,19 +66,25 @@ def addbook():
booktypes.remove("")
print(booktypes)
for booktype in booktypes:
cur.execute("select type_id from typetable where type_name=%s", (booktype,))
typeid = cur.fetchone()
if typeid is None:
cur.execute("insert into typetable(`type_name`) values (%s)", (booktype,))
cur.execute("select type_id from typetable where type_name=%s", (booktype,))
typeid = cur.fetchone()
typeid = typeid['type_id']
try:
cur.execute("insert into book_type values(%s,%s)",(typeid, bookid))
cur.callproc("add_new_book_type", args=(bookid, booktype))
db.commit()
except pymysql.Error as _e:
error = "未知错误: %s" % (_e)
db.rollback()
# cur.execute("select type_id from typetable where type_name=%s", (booktype,))
# typeid = cur.fetchone()
# if typeid is None:
# cur.execute("insert into typetable(`type_name`) values (%s)", (booktype,))
# cur.execute("select type_id from typetable where type_name=%s", (booktype,))
# typeid = cur.fetchone()
# typeid = typeid['type_id']
# try:
# cur.execute("insert into book_type values(%s,%s)",(typeid, bookid))
# db.commit()
# except pymysql.Error as _e:
# error = "未知错误: %s" % (_e)
# db.rollback()
if error is None:
error = "新建图书《%s》完成" % (bookname)
# else:
@ -258,18 +264,9 @@ def book_update(id):
booktypes.remove("")
print(booktypes)
for booktype in booktypes:
cur.execute("select type_id from typetable where type_name=%s", (booktype,))
typeid = cur.fetchone()
if typeid is None:
cur.execute("insert into typetable(`type_name`) values (%s)", (booktype,))
cur.execute("select type_id from typetable where type_name=%s", (booktype,))
typeid = cur.fetchone()
typeid = typeid['type_id']
try:
cur.execute("insert into book_type values(%s,%s)",(typeid, id))
cur.callproc("add_new_book_type", args=(id, booktype))
db.commit()
except pymysql.IntegrityError:
db.rollback()
except pymysql.Error as _e:
error = "未知错误: %s" % (_e)
db.rollback()
@ -307,7 +304,7 @@ def doc_upload(id):
error = None
if fileobj and fileobj.filename:
_, filename = os.path.split(fileobj.filename)
filepath = os.path.join(current_app.config['UPLOADDIR'], filename + f'.{int(datetime.datetime.timestamp(datetime.now()))}')
filepath = os.path.join(current_app.config['UPLOADDIR'], filename + f'.{int(datetime.timestamp(datetime.now()))}')
fileobj.save(filepath)
filesz = os.stat(filepath).st_size // 1024 # original in bytes
filetype = fileobj.mimetype