forked from hspsh/pythonhacking-flask
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanage.py
More file actions
46 lines (32 loc) · 1.01 KB
/
manage.py
File metadata and controls
46 lines (32 loc) · 1.01 KB
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
from flask_sqlalchemy import SQLAlchemy
from flask_script import Manager, prompt_bool
from src import app, db
from src.models import Task, User
from helpers.db_init_helpers import USERS, TASKS
manager = Manager(app)
def create_users(conn: SQLAlchemy, users: list):
for user_args in users:
conn.session.add(User(**user_args))
def create_tasks(conn: SQLAlchemy, tasks: list):
for task_args in tasks:
conn.session.add(Task(**task_args))
def fill_db(conn: SQLAlchemy):
"""Fill database with initial fake data"""
create_users(conn, USERS)
create_tasks(conn, TASKS)
conn.session.commit()
@manager.command
def init_db():
"""Initialize database"""
db.create_all()
fill_db(conn=db)
print('Database has been initialized')
@manager.command
def drop_db():
"""Drop database"""
msg = 'Are you sure you want to delete your database?'
if prompt_bool(msg):
db.drop_all()
print('Database has been dropped.')
if __name__ == '__main__':
manager.run()