forked from fossasia/open-event-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
populate_db.py
192 lines (154 loc) · 6.34 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from app import current_app
from app.models import db
from app.api.helpers.db import get_or_create # , save_to_db
# Admin message settings
from app.api.helpers.system_mails import MAILS
from app.models.message_setting import MessageSettings
# 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, REGISTRAR
# Admin Panel Permissions
from app.models.panel_permission import PanelPermission
from app.models.custom_system_role import CustomSysRole
# User Permissions
from app.models.user_permission import UserPermission
SALES = 'sales'
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')
get_or_create(Role, name=REGISTRAR, title_name='Registrar')
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()
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 create_admin_message_settings():
default_mails = ["Next Event",
"Session Schedule Change",
"User email",
"Invitation For Papers",
"After Event",
"Ticket(s) Purchased",
"Session Accept or Reject",
"Event Published",
"Event Export Failed",
"Event Exported",
"Event Role Invitation",
"New Session Proposal"]
for mail in MAILS:
if mail in default_mails:
get_or_create(MessageSettings, action=mail, mail_status=1, notification_status=1, user_control_status=1)
else:
get_or_create(MessageSettings, action=mail, mail_status=0, notification_status=0, user_control_status=0)
def populate():
"""
Create defined Roles, Services and Permissions.
"""
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()
print('Creating admin message settings...')
create_admin_message_settings()
def populate_without_print():
"""
Create defined Roles, Services and Permissions.
"""
create_roles()
create_services()
create_permissions()
create_custom_sys_roles()
create_panel_permissions()
create_user_permissions()
create_admin_message_settings()
db.session.commit()
if __name__ == '__main__':
with current_app.app_context():
populate()