-
Notifications
You must be signed in to change notification settings - Fork 1
/
manage.py
117 lines (65 loc) · 2.83 KB
/
manage.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# coding: utf-8
"""
manage
~~~~~~
stamaimer 04/25/17
"""
import pymysql
from flask import current_app
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from app.model import db, Role, User
from app import create_app, model
app = create_app("config.DefaultConfig")
manager = Manager(app)
migrate = Migrate(app, db)
manager.add_command("db", MigrateCommand)
@manager.shell
def make_shell_context():
return dict(app=manager.app, db=db, model=model)
@manager.option("-u", "--user", dest="user", default="root")
@manager.option("-p", "--pswd", dest="pswd", default='')
def create_user(user, pswd):
with pymysql.connect(host=current_app.config["DB_HOST"],
port=current_app.config["DB_PORT"],
user=user, passwd=pswd) as cursor:
cursor.execute("GRANT ALL PRIVILEGES ON {}.* TO '{}'@'%' IDENTIFIED BY '{}'"
.format(current_app.config["DB_NAME"], current_app.config["DB_USER"], current_app.config["DB_PSWD"]))
@manager.option("-u", "--user", dest="user", default="root")
@manager.option("-p", "--pswd", dest="pswd", default='')
def delete_user(user, pswd):
with pymysql.connect(host=current_app.config["DB_HOST"],
port=current_app.config["DB_PORT"],
user=user, passwd=pswd) as cursor:
cursor.execute("REVOKE ALL PRIVILEGES ON {}.* FROM '{}'@'%'"
.format(current_app.config["DB_NAME"], current_app.config["DB_USER"]))
cursor.execute("DROP USER '{}'@'%'".format(current_app.config["DB_USER"]))
@manager.command
def delete_db():
with pymysql.connect(host=current_app.config["DB_HOST"],
port=current_app.config["DB_PORT"],
user=current_app.config["DB_USER"], passwd=current_app.config["DB_PSWD"]) as cursor:
cursor.execute("DROP DATABASE IF EXISTS {}".format(current_app.config["DB_NAME"]))
@manager.command
def create_db():
with pymysql.connect(host=current_app.config["DB_HOST"],
port=current_app.config["DB_PORT"],
user=current_app.config["DB_USER"], passwd=current_app.config["DB_PSWD"]) as cursor:
cursor.execute("CREATE DATABASE IF NOT EXISTS {} CHARACTER SET utf8 COLLATE utf8_general_ci"
.format(current_app.config["DB_NAME"]))
@manager.command
def resets_db():
delete_db()
create_db()
@manager.command
def fillup_data():
admin_role = Role("admin", "admin")
guest_role = Role("guest", "guest")
admin_role.save()
guest_role.save()
admin_user = User("[email protected]", "admin", "admin", [admin_role])
guest_user = User("[email protected]", "guest", "guest", [guest_role])
admin_user.save()
guest_user.save()
if __name__ == "__main__":
manager.run()