-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulate_db.py
More file actions
89 lines (74 loc) · 2.98 KB
/
Copy pathpopulate_db.py
File metadata and controls
89 lines (74 loc) · 2.98 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
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
from iblog import db
from iblog import Role, User, app
def fix_role_permission_to_ones_lacking():
# Update the user list so that all the user accounts that were create
# b4 adding roles and permission existed have a role assigned
admin_role = Role.query.filter_by(name="Administrator").first()
default_role = Role.query.filter_by(default=True).first()
for u in User.query.all():
if u.role is None:
if u.email == app.config["IBLOG_ADMIN"]:
u.role = admin_role
else:
u.role = default_role
db.session.commit()
with app.app_context():
# brute force: remove the old tables
db.drop_all()
# create all model tables subclass of db.Model
db.create_all()
Role.insert_roles()
fix_role_permission_to_ones_lacking()
# add to roles table using Role model
# admin_role = Role(name="Admin")
# mod_role = Role(name="Moderator")
# user_role = Role(name="User")
# # add to users table using User model
# # role is available as it's a high level representation of
# # the one-to-many relationship
# user_john = User(username="john", email="john@gmail.com",
# role=admin_role)
# user_susan = User(username="susan", email="susan@gmail.com",
# role=user_role)
# user_david = User(username="david", email="david@gmail.com",
# role=user_role)
# # object only exist in python side so no id at first
# print("before commit: ", admin_role.id)
# # prepare objects to be written to the database
# # db.session.add(admin_role)
# # db.session.add(user_role)
# # db.session.add(mod_role)
# # db.session.add(user_john)
# # db.session.add(user_susan)
# # db.session.add(user_david)
# try:
# db.session.add_all(
# [admin_role, mod_role, user_role, user_john, user_susan,
# user_david]
# )
# except Exception as err:
# print(err, type(err))
# # db sessions are also called transactions
# # if error occurs during any transactions
# # we can roll back or restore to the state they have in the db
# db.session.rollback()
# # write objects to the database
# db.session.commit()
# print("after commit: ", admin_role.id)
# # just for information
# def modify_row():
# admin_role.name = "Administrato"
# db.session.add(admin_role)
# db.session.commit()
# def delete_row():
# db.session.delete(mod_role)
# db.session.commit()
# def get_object_from_database():
# user_role = Role.query.filter_by(name="User").first()
# return user_role
# def get_users_from_role():
# # we can apply filter because we have used lazy="dynamic"
# # this won't execute the query right away so we can
# # add to the query
# print(user_role.users.order_by(User.username).all())
# print(user_role.users.order_by(User.username).count())