forked from SerhoLiu/serholiu.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.py
103 lines (84 loc) · 3.06 KB
/
tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 用于创建数据库,请按照下面的要求填写相关的信息,再运行`python create_db.py`,
# 并将生成的数据库拷贝到 blog 目录下
import os
import sys
import base64
import getopt
import sqlite3
from miniakio.libs import crypto
# 编辑下面的信息
USERNAME = "SErHo" # 用户名
EMAIL = "[email protected]" # 邮箱,登陆的时候使用
PASSWORD = "123456" # 登陆密码
DBFILE = "example/newblog.db" # 数据库名称,请保持和 blog/config.py 中设置的名称相同
# 请不要改动下面的内容
def create_db(conn):
c = conn.cursor()
c.execute("""
CREATE TABLE users (id INTEGER NOT NULL PRIMARY KEY,
salt VARCHAR(12) NOT NULL, username VARCHAR(50) NOT NULL,
password VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL);
""")
c.execute("""
CREATE TABLE posts (id INTEGER NOT NULL PRIMARY KEY,
title VARCHAR(100) NOT NULL, slug VARCHAR(100) NOT NULL,
content TEXT NOT NULL, tags VARCHAR(255) NOT NULL,
category VARCHAR(30) NOT NULL, published VARCHAR(30) NOT NULL,
comment INTEGER NOT NULL);
""")
c.execute("""
CREATE TABLE tags (id INTEGER NOT NULL PRIMARY KEY,
name VARCHAR(50) NOT NULL, post_id INTEGER NOT NULL);
""")
c.execute("CREATE UNIQUE INDEX users_id ON users(id);")
c.execute("CREATE UNIQUE INDEX posts_id ON posts(id);")
c.execute("CREATE INDEX posts_slug ON posts(slug);")
c.execute("CREATE INDEX tags_name ON tags(name);")
c.execute("CREATE UNIQUE INDEX tags_id ON tags(id);")
conn.commit()
def create_user(conn):
c = conn.cursor()
salt = crypto.get_random_string()
enpass = crypto.PasswordCrypto.get_encrypted(PASSWORD)
c.execute("""
INSERT INTO users ( salt, username, password, email) VALUES (?,?,?,?)
""", (salt, USERNAME, enpass, EMAIL))
conn.commit()
def get_secret():
return base64.b64encode(os.urandom(32)).decode("utf-8")
def main(argv):
help = """
Usage: python tools -o <opt>
opt list:
createdb 创建数据库并添加用户信息(请先填写相关信息)
getsecret 随机生成一个 Cookie Secret
"""
opt = ""
try:
opts, args = getopt.getopt(argv, "ho:", ["opt="])
except getopt.GetoptError:
print(help)
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print(help)
sys.exit()
elif opt in ("-o", "--opt"):
opt = arg
else:
print(help)
sys.exit()
if opt == "createdb":
conn = sqlite3.connect(DBFILE)
print("开始创建数据库...")
create_db(conn)
print("数据库创建完毕,开始创建用户账户...")
create_user(conn)
conn.close()
print("用户创建成功,请务必将生成的数据库文件拷贝到 blogconfig 中设置的目录里!!!")
elif opt == "getsecret":
print(get_secret())
if __name__ == '__main__':
main(sys.argv[1:])