Fix create_db.py

This commit is contained in:
Jirayut 'Dear' Nimsaeng 2016-07-07 11:09:18 +07:00
parent c2a1c63dc8
commit c2f7a528d4

View File

@ -33,26 +33,40 @@ def connect_db(wait_time):
return False return False
def init_roles(db, role_names): def init_roles(db, role_names):
# Get key name of data
name_of_roles = map(lambda r: r.name, role_names) name_of_roles = map(lambda r: r.name, role_names)
# Query to get current data
rows = db.session.query(Role).filter(Role.name.in_(name_of_roles)).all() rows = db.session.query(Role).filter(Role.name.in_(name_of_roles)).all()
name_of_rows = map(lambda r: r.name, rows) name_of_rows = map(lambda r: r.name, rows)
roles = filter(lambda r: r.name not in rows, role_names)
# Check which data that need to insert
roles = filter(lambda r: r.name not in name_of_rows, role_names)
# Insert data
for role in roles: for role in roles:
db.session.add(role) db.session.add(role)
def init_settings(db, setting_names): def init_settings(db, setting_names):
# Get key name of data
name_of_settings = map(lambda r: r.name, setting_names) name_of_settings = map(lambda r: r.name, setting_names)
# Query to get current data
rows = db.session.query(Setting).filter(Setting.name.in_(name_of_settings)).all() rows = db.session.query(Setting).filter(Setting.name.in_(name_of_settings)).all()
# Check which data that need to insert
name_of_rows = map(lambda r: r.name, rows) name_of_rows = map(lambda r: r.name, rows)
settings = filter(lambda r: r.name not in name_of_rows, setting_names)
settings = filter(lambda r: r.name not in rows, setting_names) # Insert data
for setting in settings: for setting in settings:
db.session.add(setting) db.session.add(setting)
def init_records(): def init_records():
# create initial user roles and turn off maintenance mode
# Create initial user roles and turn off maintenance mode
admin_role = Role('Administrator', 'Administrator') admin_role = Role('Administrator', 'Administrator')
user_role = Role('User', 'User') user_role = Role('User', 'User')
maintenance_setting = Setting('maintenance', 'False') maintenance_setting = Setting('maintenance', 'False')