forked from fossasia/open-event-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
populate_db.py
153 lines (120 loc) · 4.96 KB
/
populate_db.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from app import current_app
from app.models import db
from app.helpers.data import get_or_create # , save_to_db
# Event Role-Service Permissions
from app.models.role import Role
from app.models.service import Service
from app.models.permission import Permission
from app.models.track import Track
from app.models.session import Session
from app.models.speaker import Speaker
from app.models.sponsor import Sponsor
from app.models.microlocation import Microlocation
from app.models.user import ORGANIZER, COORGANIZER, TRACK_ORGANIZER, MODERATOR, ATTENDEE
# Admin Panel Permissions
from app.models.panel_permissions import PanelPermission
from app.models.system_role import CustomSysRole
# User Permissions
from app.models.user_permissions import UserPermission
def create_roles():
get_or_create(Role, name=ORGANIZER, title_name='Organizer')
get_or_create(Role, name=COORGANIZER, title_name='Co-organizer')
get_or_create(Role, name=TRACK_ORGANIZER, title_name='Track Organizer')
get_or_create(Role, name=MODERATOR, title_name='Moderator')
get_or_create(Role, name=ATTENDEE, title_name='Attendee')
def create_services():
track = Track.get_service_name()
session = Session.get_service_name()
speaker = Speaker.get_service_name()
sponsor = Sponsor.get_service_name()
microlocation = Microlocation.get_service_name()
get_or_create(Service, name=track)
get_or_create(Service, name=session)
get_or_create(Service, name=speaker)
get_or_create(Service, name=sponsor)
get_or_create(Service, name=microlocation)
def create_permissions():
orgr = Role.query.get(1)
coorgr = Role.query.get(2)
track_orgr = Role.query.get(3)
mod = Role.query.get(4)
track = Service.query.get(1)
session = Service.query.get(2)
speaker = Service.query.get(3)
sponsor = Service.query.get(4)
microlocation = Service.query.get(5)
# For ORGANIZER
# All four permissions set to True
get_or_create(Permission, role=orgr, service=track)
get_or_create(Permission, role=orgr, service=session)
get_or_create(Permission, role=orgr, service=speaker)
get_or_create(Permission, role=orgr, service=sponsor)
get_or_create(Permission, role=orgr, service=microlocation)
# For COORGANIZER
perm, _ = get_or_create(Permission, role=coorgr, service=track)
perm.can_create, perm.can_delete = False, False
db.session.add(perm)
perm, _ = get_or_create(Permission, role=coorgr, service=session)
perm.can_create, perm.can_delete = False, False
db.session.add(perm)
perm, _ = get_or_create(Permission, role=coorgr, service=speaker)
perm.can_create, perm.can_delete = False, False
db.session.add(perm)
perm, _ = get_or_create(Permission, role=coorgr, service=sponsor)
perm.can_create, perm.can_delete = False, False
db.session.add(perm)
perm, _ = get_or_create(Permission, role=coorgr, service=microlocation)
perm.can_create, perm.can_delete = False, False
db.session.add(perm)
# For TRACK_ORGANIZER
perm, _ = get_or_create(Permission, role=track_orgr, service=track)
db.session.add(perm)
# For MODERATOR
perm, _ = get_or_create(Permission, role=mod, service=track)
perm.can_create, perm.can_update, perm.can_delete = False, False, False
db.session.add(perm)
def create_custom_sys_roles():
role, _ = get_or_create(CustomSysRole, name='Sales Admin')
db.session.add(role)
role, _ = get_or_create(CustomSysRole, name='Marketer')
db.session.add(role)
def create_panel_permissions():
sales_admin = CustomSysRole.query.filter_by(name='Sales Admin').first()
from app.views.super_admin import SALES
perm, _ = get_or_create(PanelPermission, panel_name=SALES, role=sales_admin)
db.session.add(perm)
marketer = CustomSysRole.query.filter_by(name='Marketer').first()
perm, _ = get_or_create(PanelPermission, panel_name=SALES, role=marketer)
db.session.add(perm)
def create_user_permissions():
# Publish Event
user_perm, _ = get_or_create(UserPermission, name='publish_event',
description='Publish event (make event live)')
user_perm.verified_user = True
db.session.add(user_perm)
# Create Event
user_perm, _ = get_or_create(UserPermission, name='create_event',
description='Create event')
user_perm.verified_user, user_perm.unverified_user = True, True
db.session.add(user_perm)
def populate():
"""
Create defined Roles, Services and Permissions.
"""
print
print 'Creating roles...'
create_roles()
print 'Creating services...'
create_services()
print 'Creating permissions...'
create_permissions()
print 'Creating custom system roles...'
create_custom_sys_roles()
print 'Creating admin panel permissions...'
create_panel_permissions()
print 'Creating user permissions...'
create_user_permissions()
db.session.commit()
if __name__ == '__main__':
with current_app.app_context():
populate()