add book ok
This commit is contained in:
parent
5e858c3c4b
commit
a7cd65c3f3
@ -42,7 +42,7 @@ book_pubdate datetime DEFAULT NULL,
|
|||||||
book_lang varchar(100) DEFAULT NULL,
|
book_lang varchar(100) DEFAULT NULL,
|
||||||
book_author varchar(100) DEFAULT NULL,
|
book_author varchar(100) DEFAULT NULL,
|
||||||
user_id int NOT 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);
|
-- 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,
|
doc_type varchar(100) NOT NULL,
|
||||||
book_id int NOT NULL,
|
book_id int NOT NULL,
|
||||||
user_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_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 RESTRICT
|
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);
|
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 '',
|
note_content text NOT NULL DEFAULT '',
|
||||||
book_id int NOT NULL,
|
book_id int NOT NULL,
|
||||||
user_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_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 RESTRICT
|
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);
|
create index `idx_note_name` on note(note_name);
|
||||||
|
|
||||||
@ -134,8 +134,8 @@ create table book_type
|
|||||||
type_id int NOT NULL,
|
type_id int NOT NULL,
|
||||||
book_id int NOT NULL,
|
book_id int NOT NULL,
|
||||||
PRIMARY KEY (`type_id`, `book_id`),
|
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_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 RESTRICT
|
CONSTRAINT `fk_bt_type_id` FOREIGN KEY (`type_id`) REFERENCES typetable(`type_id`) ON DELETE CASCADE
|
||||||
);
|
);
|
||||||
-- insert into book_type values (1,1);
|
-- 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_bookcount int NOT NULL DEFAULT 0,
|
||||||
user_doccount int NOT NULL DEFAULT 0,
|
user_doccount int NOT NULL DEFAULT 0,
|
||||||
user_notecount 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)
|
CONSTRAINT `ck_usedspace` CHECK (user_usedspace <= user_limit)
|
||||||
);
|
);
|
||||||
-- insert into user_stat values (1,1,1,1,1,1);
|
-- insert into user_stat values (1,1,1,1,1,1);
|
||||||
@ -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-exp.min.css') }}">
|
||||||
<link rel="stylesheet" href="{{ url_for('static', filename='spectre-icons.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') }}">
|
<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>
|
<!-- <nav>
|
||||||
<h1>BookManage</h1>
|
<h1>BookManage</h1>
|
||||||
|
|||||||
@ -65,6 +65,11 @@
|
|||||||
<h2>新建图书</h2>
|
<h2>新建图书</h2>
|
||||||
</div>
|
</div>
|
||||||
<div class="panel-body">
|
<div class="panel-body">
|
||||||
|
{%if error%}
|
||||||
|
<div class="toast">
|
||||||
|
{{error}}
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
<form method="post" class="form-group">
|
<form method="post" class="form-group">
|
||||||
<label class="form-label" for="bookname">书名*</label>
|
<label class="form-label" for="bookname">书名*</label>
|
||||||
<input class="form-input" name="bookname" id="bookname" required>
|
<input class="form-input" name="bookname" id="bookname" required>
|
||||||
@ -84,7 +89,7 @@
|
|||||||
<input class="form-input" name="booktype" id="booktype">
|
<input class="form-input" name="booktype" id="booktype">
|
||||||
<div>
|
<div>
|
||||||
{% for atype in typelist%}
|
{% 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 %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
<input class="btn btn-primary input-group-btn p-centered" type="submit" value="提交">
|
<input class="btn btn-primary input-group-btn p-centered" type="submit" value="提交">
|
||||||
|
|||||||
@ -46,11 +46,22 @@
|
|||||||
<div class="panel-title">图书分类</div>
|
<div class="panel-title">图书分类</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="panel-body">
|
<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>
|
<ul>
|
||||||
{% for atype in typelist%}
|
{% for atype in typelist%}
|
||||||
<li>
|
<li>
|
||||||
TID:{{ atype['type_id'] }}, 类型名: {{ user['user_name'] }}
|
TID:{{ atype['type_id'] }}, 类型名: {{ atype['type_name'] }}
|
||||||
<a href={{"/addtype/removeuser?uid=" ~ user['user_id']}}>删除</a>
|
<a href="/removetype?tid={{atype['type_id']}}">删除</a>
|
||||||
</li>
|
</li>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
|
|||||||
58
src/templates/user/result.html
Normal file
58
src/templates/user/result.html
Normal 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 %}
|
||||||
79
src/user.py
79
src/user.py
@ -25,10 +25,8 @@ def home():
|
|||||||
@login_required
|
@login_required
|
||||||
def addbook():
|
def addbook():
|
||||||
db = get_db()
|
db = get_db()
|
||||||
if request.method == 'GET':
|
error = None
|
||||||
typelist = db.execute("select type_name from typetable").fetchall()
|
if request.method == 'POST':
|
||||||
return render_template("user/addbook.html", typelist=typelist)
|
|
||||||
elif request.method == 'POST':
|
|
||||||
bookname = request.form['bookname']
|
bookname = request.form['bookname']
|
||||||
bookisbn = request.form['bookisbn']
|
bookisbn = request.form['bookisbn']
|
||||||
bookpublisher = request.form['bookpublisher']
|
bookpublisher = request.form['bookpublisher']
|
||||||
@ -51,10 +49,11 @@ def addbook():
|
|||||||
db.execute(
|
db.execute(
|
||||||
"insert into book (`book_name`, `book_isbn`"
|
"insert into book (`book_name`, `book_isbn`"
|
||||||
", `book_publisher`, `book_lang`, `book_author`, `user_id`) "
|
", `book_publisher`, `book_lang`, `book_author`, `user_id`) "
|
||||||
"values (?,?,?,?,?,?,?)",
|
"values (?,?,?,?,?,?)",
|
||||||
(bookname, bookisbn, bookpublisher, booklang, bookauthor, g.user['user_id'],))
|
(bookname, bookisbn, bookpublisher, booklang, bookauthor, g.user['user_id'],))
|
||||||
bookid = db.execute("select max(book_id) from book").fetchone()
|
|
||||||
db.commit()
|
db.commit()
|
||||||
|
bookid = db.execute("select max(book_id) from book").fetchone()
|
||||||
|
bookid = bookid['max(book_id)']
|
||||||
error = None
|
error = None
|
||||||
if booktype is not None:
|
if booktype is not None:
|
||||||
booktypes = booktype.split(";")
|
booktypes = booktype.split(";")
|
||||||
@ -68,6 +67,7 @@ def addbook():
|
|||||||
(booktype,))
|
(booktype,))
|
||||||
typeid = db.execute("select type_id from typetable where type_name=?",
|
typeid = db.execute("select type_id from typetable where type_name=?",
|
||||||
(booktype,)).fetchone()
|
(booktype,)).fetchone()
|
||||||
|
typeid = typeid['type_id']
|
||||||
try:
|
try:
|
||||||
db.execute("insert into book_type values(?,?)",
|
db.execute("insert into book_type values(?,?)",
|
||||||
(bookid, typeid))
|
(bookid, typeid))
|
||||||
@ -75,17 +75,64 @@ def addbook():
|
|||||||
except sqlite3.Error as _e:
|
except sqlite3.Error as _e:
|
||||||
error = "未知错误: %s" % (_e)
|
error = "未知错误: %s" % (_e)
|
||||||
db.rollback()
|
db.rollback()
|
||||||
if error is not None:
|
if error is None:
|
||||||
flash(error)
|
error = "新建图书《%s》完成" % (bookname)
|
||||||
else:
|
# else:
|
||||||
return render_template("user/addbook.html", success=True)
|
# return render_template("user/addbook.html", success=True)
|
||||||
return render_template("user/addbook.html")
|
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
|
@login_required
|
||||||
def addtype():
|
def tags():
|
||||||
if request.method == 'POST':
|
|
||||||
|
|
||||||
db = get_db()
|
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()
|
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())
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user