add book ok

This commit is contained in:
ridethepig 2022-11-13 10:48:03 +08:00
parent 5e858c3c4b
commit a7cd65c3f3
6 changed files with 149 additions and 28 deletions

View File

@ -42,7 +42,7 @@ book_pubdate datetime DEFAULT NULL,
book_lang varchar(100) DEFAULT NULL,
book_author varchar(100) DEFAULT NULL,
user_id int NOT NULL,
CONSTRAINT `fk_book_user_id` FOREIGN KEY (`user_id`) REFERENCES user(`user_id`) ON DELETE RESTRICT
CONSTRAINT `fk_book_user_id` FOREIGN KEY (`user_id`) REFERENCES user(`user_id`) ON DELETE CASCADE
);
-- insert into book (`book_name`, `book_isbn`, `book_publisher`, `book_pubdate`, `book_lang`, `user_id`) values ('mom pig', 'emm', NULL, NULL, 'CN', 1);
@ -59,8 +59,8 @@ doc_date timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
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 RESTRICT,
CONSTRAINT `fk_document_book_id` FOREIGN KEY (`book_id`) REFERENCES book(`book_id`) ON DELETE RESTRICT
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
);
create index `idx_doc_name` on document(doc_name);
@ -76,8 +76,8 @@ note_date datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
note_content text NOT NULL DEFAULT '',
book_id int NOT NULL,
user_id int NOT NULL,
CONSTRAINT `fk_note_user_id` FOREIGN KEY (`user_id`) REFERENCES user(`user_id`) ON DELETE RESTRICT,
CONSTRAINT `fk_note_book_id` FOREIGN KEY (`book_id`) REFERENCES book(`book_id`) ON DELETE RESTRICT
CONSTRAINT `fk_note_user_id` FOREIGN KEY (`user_id`) REFERENCES user(`user_id`) ON DELETE CASCADE,
CONSTRAINT `fk_note_book_id` FOREIGN KEY (`book_id`) REFERENCES book(`book_id`) ON DELETE CASCADE
);
create index `idx_note_name` on note(note_name);
@ -134,8 +134,8 @@ create table book_type
type_id int NOT NULL,
book_id int NOT NULL,
PRIMARY KEY (`type_id`, `book_id`),
CONSTRAINT `fk_bt_book_id` FOREIGN KEY (`book_id`) REFERENCES book(`book_id`) ON DELETE RESTRICT,
CONSTRAINT `fk_bt_type_id` FOREIGN KEY (`type_id`) REFERENCES typetable(`type_id`) ON DELETE RESTRICT
CONSTRAINT `fk_bt_book_id` FOREIGN KEY (`book_id`) REFERENCES book(`book_id`) ON DELETE CASCADE,
CONSTRAINT `fk_bt_type_id` FOREIGN KEY (`type_id`) REFERENCES typetable(`type_id`) ON DELETE CASCADE
);
-- insert into book_type values (1,1);
@ -149,7 +149,7 @@ user_usedspace int NOT NULL DEFAULT 0,
user_bookcount int NOT NULL DEFAULT 0,
user_doccount int NOT NULL DEFAULT 0,
user_notecount int NOT NULL DEFAULT 0,
CONSTRAINT `fk_user_stat_user` FOREIGN KEY (`user_id`) REFERENCES user(`user_id`) ON DELETE RESTRICT,
CONSTRAINT `fk_user_stat_user` FOREIGN KEY (`user_id`) REFERENCES user(`user_id`) ON DELETE CASCADE,
CONSTRAINT `ck_usedspace` CHECK (user_usedspace <= user_limit)
);
-- insert into user_stat values (1,1,1,1,1,1);

View File

@ -3,7 +3,7 @@
<link rel="stylesheet" href="{{ url_for('static', filename='spectre-exp.min.css') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='spectre-icons.min.css') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='spectre.min.css') }}">
<script src="{{ url_for('static', filename='axios.min.js')}}"></script>
<!-- <script src="{{ url_for('static', filename='axios.min.js')}}"></script> -->
<!-- <nav>
<h1>BookManage</h1>

View File

@ -65,6 +65,11 @@
<h2>新建图书</h2>
</div>
<div class="panel-body">
{%if error%}
<div class="toast">
{{error}}
</div>
{% endif %}
<form method="post" class="form-group">
<label class="form-label" for="bookname">书名*</label>
<input class="form-input" name="bookname" id="bookname" required>
@ -84,7 +89,7 @@
<input class="form-input" name="booktype" id="booktype">
<div>
{% for atype in typelist%}
<span class="chip btn" onclick={{"switch_type(" ~ atype ~ ")"}}>{{atype}}</span>
<span class="chip btn" onclick="switch_type('{{atype['type_name']}}')">{{atype['type_name']}}</span>
{% endfor %}
</div>
<input class="btn btn-primary input-group-btn p-centered" type="submit" value="提交">

View File

@ -46,11 +46,22 @@
<div class="panel-title">图书分类</div>
</div>
<div class="panel-body">
{% if error%}
<div class="toast">
<button class="btn btn-clear float-right"></button>
{{error}}
</div>
{% endif %}
<form method="post" class="input-group">
<label class="form-label px-1" for="typename">新建类型</label>
<input class="form-input" name="typename" id="typename" required>
<input class="btn btn-primary input-group-btn" type="submit" value="提交">
</form>
<ul>
{% for atype in typelist%}
<li>
TID:{{ atype['type_id'] }}, 类型名: {{ user['user_name'] }}
<a href={{"/addtype/removeuser?uid=" ~ user['user_id']}}>删除</a>
TID:{{ atype['type_id'] }}, 类型名: {{ atype['type_name'] }}
<a href="/removetype?tid={{atype['type_id']}}">删除</a>
</li>
{% endfor %}
</ul>

View File

@ -0,0 +1,58 @@
{% extends 'base.html' %}
{% block header %}
<style>
.maindiv {
height: 100%;
width: 100%;
position: fixed;
z-index: 1;
top: 20;
left: 0;
overflow-x: hidden;
padding-left: 1.2rem;
padding-right: 2rem;
}
.sidenav {
height: 100%;
max-width: 160px;
z-index: 1;
overflow-x: hidden;
}
</style>
{% endblock %}
{% block content %}
<div class="container maindiv">
<div class="columns">
<div class="column col-3">
<ul class="nav">
<li class="nav-item">
<a href="/home">主页</a>
</li>
<li class="nav-item">
<a href="/search">搜索</a>
</li>
<li class="nav-item">
<a href="/addbook">增加</a>
</li>
<li class="nav-item">
<a href="/tags">分类</a>
</li>
</ul>
</div>
<div class="column col-9 panel">
<div class="panel-header">
<div class="panel-title">{{opname}}</div>
</div>
<div class="panel-body">
<p>{{opresult}}</p>
<p><a href="javascript:history.back(-1)">返回</a></p>
</div>
<div class="panel-footer">
Rendered @ {{cur_time}}
</div>
</div>
</div>
</div>
{% endblock %}

View File

@ -25,10 +25,8 @@ def home():
@login_required
def addbook():
db = get_db()
if request.method == 'GET':
typelist = db.execute("select type_name from typetable").fetchall()
return render_template("user/addbook.html", typelist=typelist)
elif request.method == 'POST':
error = None
if request.method == 'POST':
bookname = request.form['bookname']
bookisbn = request.form['bookisbn']
bookpublisher = request.form['bookpublisher']
@ -51,10 +49,11 @@ def addbook():
db.execute(
"insert into book (`book_name`, `book_isbn`"
", `book_publisher`, `book_lang`, `book_author`, `user_id`) "
"values (?,?,?,?,?,?,?)",
"values (?,?,?,?,?,?)",
(bookname, bookisbn, bookpublisher, booklang, bookauthor, g.user['user_id'],))
bookid = db.execute("select max(book_id) from book").fetchone()
db.commit()
bookid = db.execute("select max(book_id) from book").fetchone()
bookid = bookid['max(book_id)']
error = None
if booktype is not None:
booktypes = booktype.split(";")
@ -68,6 +67,7 @@ def addbook():
(booktype,))
typeid = db.execute("select type_id from typetable where type_name=?",
(booktype,)).fetchone()
typeid = typeid['type_id']
try:
db.execute("insert into book_type values(?,?)",
(bookid, typeid))
@ -75,17 +75,64 @@ def addbook():
except sqlite3.Error as _e:
error = "未知错误: %s" % (_e)
db.rollback()
if error is not None:
flash(error)
else:
return render_template("user/addbook.html", success=True)
return render_template("user/addbook.html")
if error is None:
error = "新建图书《%s》完成" % (bookname)
# else:
# return render_template("user/addbook.html", success=True)
typelist = db.execute("select type_name from typetable").fetchall()
return render_template("user/addbook.html", typelist=typelist, error=error,
cur_time=datetime.datetime.now())
@bp.route("/addtype", methods=('GET', 'POST', 'PUT'))
@bp.route("/tags", methods=('GET', 'POST'))
@login_required
def addtype():
if request.method == 'POST':
def tags():
db = get_db()
error = None
if request.method == 'POST':
typename = request.form['typename']
if typename is None or len(typename) == 0:
error = "不合法的分类名"
rowcount = 0
if error is None:
try:
rowcount = db.execute("insert into typetable(`type_name`) values(?)",
(typename,)).rowcount
db.commit()
except sqlite3.IntegrityError:
error = "类型名称已经存在: %s" % (typename)
db.rollback()
except sqlite3.Error as _e:
error = "未知错误: %s" % (_e)
db.rollback()
if error is None:
if rowcount == 0:
error = "新建可能失败,再次检查是否完成"
else:
error = "新建类型\"%s\"完成" % (typename)
typelist = db.execute("select * from typetable").fetchall()
return render_template("user/addtype.html", typelist=typelist)
return render_template("user/addtype.html", typelist=typelist, error=error,
cur_time=datetime.datetime.now())
@bp.route("/removetype", methods=("GET",))
@login_required
def removetype():
db = get_db()
tid_to_del = request.args.get("tid")
error = None
if tid_to_del is None or not tid_to_del.isnumeric():
error = "Invalid argument"
rowcnt = 0
if error is None:
try:
rowcnt = db.execute("delete from typetable where type_id=?", (tid_to_del,)).rowcount
db.commit()
except sqlite3.Error as _e:
error = "删除失败:%s" % (_e)
db.rollback()
if error is None:
if rowcnt == 0:
error = "删除可能失败,再次检查是否完成"
else:
error = "删除(type_id=%s)完成" % tid_to_del
return render_template("user/result.html", opname="删除分类",
opresult=error, cur_time=datetime.datetime.now())