following flask tutorial
This commit is contained in:
parent
ef7a365287
commit
6336506974
3
.gitignore
vendored
3
.gitignore
vendored
@ -7,3 +7,6 @@ $*
|
||||
~$*
|
||||
.$*
|
||||
*.db
|
||||
venv/
|
||||
__pycache__/
|
||||
.vscode/
|
||||
File diff suppressed because one or more lines are too long
6
README.md
Normal file
6
README.md
Normal file
@ -0,0 +1,6 @@
|
||||
```shell
|
||||
cd src
|
||||
python -m venv venv
|
||||
pip install flask
|
||||
flask --app main run
|
||||
```
|
||||
29
src/__init__.py
Normal file
29
src/__init__.py
Normal file
@ -0,0 +1,29 @@
|
||||
import os
|
||||
|
||||
from flask import Flask
|
||||
|
||||
|
||||
def create_app(test_config=None):
|
||||
# create and configure the app
|
||||
app = Flask(__name__, instance_relative_config=True)
|
||||
app.config.from_mapping(
|
||||
SECRET_KEY='dev',
|
||||
DATABASE=os.path.join(app.instance_path, 'dbproj.db'),
|
||||
)
|
||||
|
||||
if test_config is None:
|
||||
app.config.from_pyfile('config.py', silent=True)
|
||||
else:
|
||||
app.config.from_mapping(test_config)
|
||||
|
||||
try:
|
||||
os.makedirs(app.instance_path)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
@app.route('/hello')
|
||||
def hello():
|
||||
return 'Hello, World!'
|
||||
from . import db
|
||||
db.init_app(app)
|
||||
return app
|
||||
35
src/db.py
Normal file
35
src/db.py
Normal file
@ -0,0 +1,35 @@
|
||||
import sqlite3
|
||||
import click
|
||||
from flask import current_app, g
|
||||
|
||||
def get_db():
|
||||
if 'db' not in g:
|
||||
g.db = sqlite3.connect(
|
||||
current_app.config['DATABASE'],
|
||||
detect_types=sqlite3.PARSE_DECLTYPES
|
||||
)
|
||||
g.db.row_factory = sqlite3.Row
|
||||
|
||||
return g.db
|
||||
|
||||
|
||||
def close_db(e=None):
|
||||
db = g.pop('db', None)
|
||||
|
||||
if db is not None:
|
||||
db.close()
|
||||
|
||||
@click.command('init-db')
|
||||
def init_db():
|
||||
db = get_db()
|
||||
click.echo("Initializing database schema...")
|
||||
with current_app.open_resource('sql/db_create_sqlite.sql') as f:
|
||||
db.executescript(f.read().decode('utf8'))
|
||||
click.echo("Initializing database triggers...")
|
||||
with current_app.open_resource('sql/trigger_sqlite.sql') as f:
|
||||
db.executescript(f.read().decode('utf8'))
|
||||
click.echo("Initialized database.")
|
||||
|
||||
def init_app(app):
|
||||
app.teardown_appcontext(close_db)
|
||||
app.cli.add_command(init_db)
|
||||
111
src/sql/db_create.sql
Normal file
111
src/sql/db_create.sql
Normal file
@ -0,0 +1,111 @@
|
||||
drop database if exists bigwork;
|
||||
create database bigwork;
|
||||
|
||||
create table `admin`
|
||||
(
|
||||
passwd varchar(100) NOT NULL
|
||||
);
|
||||
|
||||
insert into `admin` (`passwd`) values('');
|
||||
|
||||
create table user
|
||||
(
|
||||
user_id int PRIMARY KEY AUTO_INCREMENT,
|
||||
user_name varchar(100) NOT NULL,
|
||||
user_mail varchar(100) NOT NULL,
|
||||
user_passwd varchar(100) NOT NULL,
|
||||
user_limit int NOT NULL,
|
||||
user_regtime datetime NOT NULL
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
||||
|
||||
create table book
|
||||
(
|
||||
book_id int PRIMARY KEY AUTO_INCREMENT,
|
||||
book_name varchar(100) NOT NULL,
|
||||
book_isbn varchar(100) DEFAULT NULL,
|
||||
book_publisher varchar(100) DEFAULT NULL,
|
||||
book_pubdate datetime DEFAULT NULL,
|
||||
book_lang varchar(100) DEFAULT NULL,
|
||||
user_id int NOT NULL,
|
||||
CONSTRAINT `fk_book_user_id` FOREIGN KEY (`user_id`) REFERENCES user(`user_id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
||||
|
||||
create table document
|
||||
(
|
||||
doc_id int PRIMARY KEY AUTO_INCREMENT,
|
||||
doc_name varchar(100) NOT NULL,
|
||||
doc_url varchar(100) NOT NULL,
|
||||
doc_size int NOT NULL,
|
||||
doc_date datetime NOT NULL,
|
||||
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`),
|
||||
CONSTRAINT `fk_document_book_id` FOREIGN KEY (`book_id`) REFERENCES book(`book_id`),
|
||||
INDEX (doc_name(10)),
|
||||
INDEX (doc_date)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
||||
|
||||
create table note
|
||||
(
|
||||
note_id int PRIMARY KEY AUTO_INCREMENT,
|
||||
note_name varchar(100) NOT NULL,
|
||||
note_date datetime NOT NULL,
|
||||
note_content mediumtext 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`),
|
||||
CONSTRAINT `fk_note_book_id` FOREIGN KEY (`book_id`) REFERENCES book(`book_id`),
|
||||
INDEX (note_name(10))
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
||||
|
||||
create table record
|
||||
(
|
||||
record_time timestamp PRIMARY KEY,
|
||||
record_type varchar(10) NOT NULL,
|
||||
doc_URL varchar(100) NOT NULL,
|
||||
user_id int NOT NULL,
|
||||
INDEX (record_type)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
||||
|
||||
create table typetable
|
||||
(
|
||||
type_id int PRIMARY KEY AUTO_INCREMENT,
|
||||
type_name varchar(20) NOT NULL,
|
||||
UNIQUE (type_name)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
||||
|
||||
create table author
|
||||
(
|
||||
author_id int PRIMARY KEY AUTO_INCREMENT,
|
||||
author_name varchar(50) NOT NULL,
|
||||
INDEX (author_name(10))
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
||||
|
||||
create table book_author
|
||||
(
|
||||
author_id int NOT NULL,
|
||||
book_id int NOT NULL,
|
||||
PRIMARY KEY (`author_id`, `book_id`),
|
||||
CONSTRAINT `fk_ba_book_id` FOREIGN KEY (`book_id`) REFERENCES book(`book_id`),
|
||||
CONSTRAINT `fk_ba_author_id` FOREIGN KEY (`author_id`) REFERENCES author(`author_id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
||||
|
||||
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`),
|
||||
CONSTRAINT `fk_bt_type_id` FOREIGN KEY (`type_id`) REFERENCES typetable(`type_id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
||||
|
||||
create table user_stat
|
||||
(
|
||||
user_id int PRIMARY KEY,
|
||||
user_usedspace int NOT NULL DEFAULT 0,
|
||||
user_bookcount int NOT NULL DEFAULT 0,
|
||||
user_doccount int NOT NULL DEFAULT 0,
|
||||
user_typecount int NOT NULL DEFAULT 0,
|
||||
CONSTRAINT `fk_user_stat_user` FOREIGN KEY REFERENCES user(`user_id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
||||
154
src/sql/db_create_sqlite.sql
Normal file
154
src/sql/db_create_sqlite.sql
Normal file
@ -0,0 +1,154 @@
|
||||
drop table if exists `admin`;
|
||||
drop table if exists user;
|
||||
drop table if exists book;
|
||||
drop table if exists document;
|
||||
drop table if exists note;
|
||||
drop table if exists record;
|
||||
drop table if exists typetable;
|
||||
drop table if exists author;
|
||||
drop table if exists book_author;
|
||||
drop table if exists book_type;
|
||||
drop table if exists user_stat;
|
||||
create table `admin`
|
||||
(
|
||||
passwd varchar(100) NOT NULL
|
||||
);
|
||||
-- insert into `admin` (`passwd`) values('');
|
||||
-- sqlite default no foreign key
|
||||
PRAGMA FOREIGN_KEYS=ON;
|
||||
|
||||
-- user
|
||||
drop table if exists user;
|
||||
create table user
|
||||
(
|
||||
user_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_name varchar(100) NOT NULL UNIQUE,
|
||||
user_mail varchar(100) NOT NULL UNIQUE,
|
||||
user_passwd varchar(100) NOT NULL,
|
||||
user_limit int NOT NULL,
|
||||
user_regtime timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
-- insert into user (`user_name`, `user_mail`, `user_passwd`, `user_limit`) values ('catfood', 'a@a.com', 'lil0', 100);
|
||||
|
||||
-- book
|
||||
drop table if exists book;
|
||||
create table book
|
||||
(
|
||||
book_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
book_name varchar(100) NOT NULL,
|
||||
book_isbn varchar(100) DEFAULT NULL,
|
||||
book_publisher varchar(100) DEFAULT NULL,
|
||||
book_pubdate datetime DEFAULT NULL,
|
||||
book_lang 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
|
||||
);
|
||||
|
||||
-- insert into book (`book_name`, `book_isbn`, `book_publisher`, `book_pubdate`, `book_lang`, `user_id`) values ('mom pig', 'emm', NULL, NULL, 'CN', 1);
|
||||
|
||||
-- document
|
||||
drop table if exists document;
|
||||
create table document
|
||||
(
|
||||
doc_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
doc_name varchar(100) NOT NULL,
|
||||
doc_url varchar(100) NOT NULL,
|
||||
doc_size int NOT NULL,
|
||||
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
|
||||
);
|
||||
create index `idx_doc_name` on document(doc_name);
|
||||
|
||||
-- insert into document (`doc_name`,`doc_url`,`doc_size`,`doc_type`,`book_id`,`user_id`) values ("a.pdf", 'c:/shit/a.pdf', 10, 'pdf', 1, 1);
|
||||
|
||||
-- note
|
||||
drop table if exists note;
|
||||
create table note
|
||||
(
|
||||
note_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
note_name varchar(100) NOT NULL,
|
||||
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
|
||||
);
|
||||
create index `idx_note_name` on note(note_name);
|
||||
|
||||
-- insert into note (`note_name`, `note_content`,`book_id`,`user_id`) values ('long ago', 'well, nothing', 1, 1);
|
||||
|
||||
-- record
|
||||
drop table if exists record;
|
||||
create table record
|
||||
(
|
||||
record_time timestamp PRIMARY KEY DEFAULT CURRENT_TIMESTAMP,
|
||||
record_type varchar(10) NOT NULL,
|
||||
doc_URL varchar(100) NOT NULL,
|
||||
user_id int NOT NULL
|
||||
);
|
||||
|
||||
-- insert into record (`record_type`,`doc_URL`,`user_id`)
|
||||
-- values ('write', 'c:\a.pdf', 1);
|
||||
|
||||
-- typetable
|
||||
drop table if exists typetable;
|
||||
create table typetable
|
||||
(
|
||||
type_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
type_name varchar(20) NOT NULL UNIQUE
|
||||
);
|
||||
-- insert into typetable (`type_name`) values ('sjot');
|
||||
|
||||
-- author
|
||||
drop table if exists author;
|
||||
create table author
|
||||
(
|
||||
author_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
author_name varchar(50) NOT NULL
|
||||
);
|
||||
create index `idx_author_name` on author(author_name);
|
||||
-- insert into author (`author_name`) values ('alabama.shit');
|
||||
|
||||
-- book_author
|
||||
drop table if exists book_author;
|
||||
create table book_author
|
||||
(
|
||||
author_id int NOT NULL,
|
||||
book_id int NOT NULL,
|
||||
PRIMARY KEY (`author_id`, `book_id`),
|
||||
CONSTRAINT `fk_ba_book_id` FOREIGN KEY (`book_id`) REFERENCES book(`book_id`) ON DELETE RESTRICT,
|
||||
CONSTRAINT `fk_ba_author_id` FOREIGN KEY (`author_id`) REFERENCES author(`author_id`) ON DELETE RESTRICT
|
||||
);
|
||||
-- insert into book_author values (1,1);
|
||||
|
||||
-- book_type
|
||||
drop table if exists book_type;
|
||||
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
|
||||
);
|
||||
-- insert into book_type values (1,1);
|
||||
|
||||
-- user_stat
|
||||
drop table if exists user_stat;
|
||||
create table user_stat
|
||||
(
|
||||
user_id int PRIMARY KEY,
|
||||
user_limit int NOT NULL,
|
||||
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 `ck_usedspace` CHECK (user_usedspace <= user_limit)
|
||||
);
|
||||
-- insert into user_stat values (1,1,1,1,1,1);
|
||||
55
src/sql/trigger.sql
Normal file
55
src/sql/trigger.sql
Normal file
@ -0,0 +1,55 @@
|
||||
drop trigger if exists `trig_update_stat_book_ins`;
|
||||
DELIMITER ##
|
||||
create trigger `trig_update_stat_book_ins`
|
||||
after insert on book
|
||||
for each row begin
|
||||
update user_stat set user_bookcount=user_bookcount+1 where user_stat.user_id=NEW.user_id;
|
||||
end ##
|
||||
DELIMITER ;
|
||||
|
||||
drop trigger if exists `trig_update_stat_book_del`;
|
||||
DELIMITER ##
|
||||
create trigger `trig_update_stat_book_del`
|
||||
after delete on book
|
||||
for each row begin
|
||||
update user_stat set user_bookcount=user_bookcount-1 where user_stat.user_id=OLD.user_id;
|
||||
end ##
|
||||
DELIMITER ;
|
||||
|
||||
drop trigger if exists `trig_update_stat_doc_ins`;
|
||||
DELIMITER ##
|
||||
create trigger `trig_update_stat_doc_ins`
|
||||
after insert on document
|
||||
for each row begin
|
||||
update user_stat set user_doccount=user_doccount+1 where user_stat.user_id=NEW.user_id;
|
||||
update user_stat set user_usedspace=user_usedspace+NEW.doc_size where user_stat.user_id=NEW.user_id;
|
||||
end ##
|
||||
DELIMITER ;
|
||||
|
||||
drop trigger if exists `trig_update_stat_doc_del`;
|
||||
DELIMITER ##
|
||||
create trigger `trig_update_stat_doc_del`
|
||||
after delete on document
|
||||
for each row begin
|
||||
update user_stat set user_doccount=user_doccount-1 where user_stat.user_id=OLD.user_id;
|
||||
update user_stat set user_usedspace=user_usedspace-OLD.doc_size where user_stat.user_id=OLD.user_id;
|
||||
end ##
|
||||
DELIMITER ;
|
||||
|
||||
drop trigger if exists `trig_update_stat_type_ins`;
|
||||
DELIMITER ##
|
||||
create trigger `trig_update_stat_type_ins`
|
||||
after insert on typetable
|
||||
for each row begin
|
||||
update user_stat set user_typecount=user_typecount+1 where user_stat.user_id=NEW.user_id;
|
||||
end ##
|
||||
DELIMITER ;
|
||||
|
||||
drop trigger if exists `trig_update_stat_type_del`;
|
||||
DELIMITER ##
|
||||
create trigger `trig_update_stat_type_del`
|
||||
after delete on typetable
|
||||
for each row begin
|
||||
update user_stat set user_typecount=user_typecount-1 where user_stat.user_id=OLD.user_id;
|
||||
end ##
|
||||
DELIMITER ;
|
||||
62
src/sql/trigger_sqlite.sql
Normal file
62
src/sql/trigger_sqlite.sql
Normal file
@ -0,0 +1,62 @@
|
||||
drop trigger if exists `trig_create_user_stat`;
|
||||
create trigger `trig_create_user_stat`
|
||||
after insert on user
|
||||
for each row begin
|
||||
insert into user_stat (`user_id`, `user_limit`) values(NEW.user_id, NEW.user_limit);
|
||||
end;
|
||||
|
||||
drop trigger if exists `trig_delete_user_stat`;
|
||||
create trigger `trig_delete_user_stat`
|
||||
after delete on user
|
||||
for each row begin
|
||||
delete from user_stat where user_id=OLD.user_id;
|
||||
end;
|
||||
|
||||
drop trigger if exists `trig_update_stat_book_ins`;
|
||||
create trigger `trig_update_stat_book_ins`
|
||||
after insert on book
|
||||
for each row begin
|
||||
update user_stat set user_bookcount=user_bookcount+1 where user_stat.user_id=NEW.user_id;
|
||||
end;
|
||||
|
||||
|
||||
drop trigger if exists `trig_update_stat_book_del`;
|
||||
create trigger `trig_update_stat_book_del`
|
||||
after delete on book
|
||||
for each row begin
|
||||
update user_stat set user_bookcount=user_bookcount-1 where user_stat.user_id=OLD.user_id;
|
||||
end;
|
||||
|
||||
|
||||
drop trigger if exists `trig_update_stat_doc_ins`;
|
||||
create trigger `trig_update_stat_doc_ins`
|
||||
after insert on document
|
||||
for each row begin
|
||||
update user_stat set user_doccount=user_doccount+1 where user_stat.user_id=NEW.user_id;
|
||||
update user_stat set user_usedspace=user_usedspace+NEW.doc_size where user_stat.user_id=NEW.user_id;
|
||||
end;
|
||||
|
||||
|
||||
drop trigger if exists `trig_update_stat_doc_del`;
|
||||
create trigger `trig_update_stat_doc_del`
|
||||
after delete on document
|
||||
for each row begin
|
||||
update user_stat set user_doccount=user_doccount-1 where user_stat.user_id=OLD.user_id;
|
||||
update user_stat set user_usedspace=user_usedspace-OLD.doc_size where user_stat.user_id=OLD.user_id;
|
||||
end;
|
||||
|
||||
|
||||
drop trigger if exists `trig_update_stat_note_ins`;
|
||||
create trigger `trig_update_stat_note_ins`
|
||||
after insert on note
|
||||
for each row begin
|
||||
update user_stat set user_notecount=user_notecount+1 where user_stat.user_id=NEW.user_id;
|
||||
end;
|
||||
|
||||
|
||||
drop trigger if exists `trig_update_stat_note_del`;
|
||||
create trigger `trig_update_stat_note_del`
|
||||
after delete on note
|
||||
for each row begin
|
||||
update user_stat set user_notecount=user_notecount-1 where user_stat.user_id=OLD.user_id;
|
||||
end;
|
||||
15
src/templates/index.html
Normal file
15
src/templates/index.html
Normal file
@ -0,0 +1,15 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh">
|
||||
<head>
|
||||
<title>DBProject</title>
|
||||
<meta charset="utf-8">
|
||||
</head>
|
||||
<body>
|
||||
<h1>简单的电子书归档系统</h1>
|
||||
<div>
|
||||
<p>首先需要登陆</p>
|
||||
<a href="/loginuser">用户登陆</a>
|
||||
<a href="/loginadmin">管理员登陆</a>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Loading…
Reference in New Issue
Block a user