-
Notifications
You must be signed in to change notification settings - Fork 1
/
populate_db.py
244 lines (198 loc) · 6.14 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
"""
We're going to add hardcoded values into our database so we have something
to experiment with. (This is cheating! But who cares...)
"""
import sys
import os
# This makes the file executable
if os.environ.get('PROJECT_PATH'):
sys.path.append(os.environ.get('PROJECT_PATH'))
import datetime
from database.models import Theatre, Show, Person, Role, ShowsRolesLink
from database import session, DB_URI
to_year = lambda x: datetime.datetime(year=x, month=1, day=1)
# ------------------------------------------------------------------------------
theatre_records = [
dict(
theatre_name='Richard Rodgers Theatre',
street_address='226 West 46th Street, New York, NY 10036',
capacity=1_380,
date_opened=to_year(1925)
),
dict(
theatre_name='Gershwin Theatre',
street_address='222 West 51st Street New York, NY 10019',
capacity=1_933,
date_opened=to_year(1972)
)
]
show_records = [
dict(
title='Hamilton',
preview_date=datetime.datetime(year=2015, month=7, day=13),
opening_date=datetime.datetime(year=2015, month=8, day=6),
show_genre='Musical'
),
dict(
title='Wicked',
preview_date=datetime.datetime(year=2003, month=10, day=8),
opening_date=datetime.datetime(year=2003, month=10, day=30),
show_genre='Musical'
)
]
person_records = [
# Some wicked people
dict(
first_name='Stephen',
middle_name=None,
last_name='Oremus',
show='Wicked',
role='Musical Director'
),
dict(
first_name='Joe',
middle_name=None,
last_name='Mantello',
show='Wicked',
role='Director'
),
dict(
first_name='Wayne',
middle_name=None,
last_name='Cilento',
show='Wicked',
role='Choreographer'
),
dict(
first_name='Charles',
middle_name=None,
last_name='LaPointe',
show='Wicked',
role='Associate Hair Design'
),
# Some hamilton people
dict(
first_name='Alex',
middle_name=None,
last_name='Lacamoire',
show='Hamilton',
role='Musical Director'
),
dict(
first_name='Thomas',
middle_name=None,
last_name='Kail',
show='Wicked',
role='Director'
),
dict(
first_name='Stephanie',
middle_name=None,
last_name='Klemons',
show='Wicked',
role='Choreographer'
),
dict(
first_name='Charles',
middle_name=None,
last_name='LaPointe',
show='Hamilton',
role='Hair and Wig Design'
)
]
# ------------------------------------------------------------------------------
def add_all_theatres(records=theatre_records):
for rec in records:
# look before you leap
my_theatre = session.query(Theatre).filter(Theatre.theatre_name==rec['theatre_name']).first()
if my_theatre:
print(f'Theatre "{my_theatre.theatre_name}" already exists')
else:
my_theatre = Theatre(**rec)
session.add(my_theatre)
"""
Inspect the item you've just created:
>> print(my_theatre.__dict__)
{
'_sa_instance_state': <sqlalchemy.orm.state.InstanceState at 0x7fae80837dc0>,
'theatre_name': 'Gershwin Theatre',
'street_address': '222 West 51st Street New York, NY 10019',
'capacity': 1933,
'date_opened': datetime.datetime(1972, 1, 1, 0, 0)
}
"""
# At the end, commit your session (or roll it back!)
session.commit()
def add_all_shows(records=show_records):
for rec in records:
# look before you leap
my_show = session.query(Show).filter(Show.title==rec['title']).first()
if my_show:
print(f'Show "{my_show.title}" already exists')
else:
my_show = Show(**rec)
session.add(my_show)
# At the end, commit your session (or roll it back!)
session.commit()
def add_all_people(records=person_records):
"""
This is not the best way to add these records to the db in any way, but it works
and is kinda easy to read I hope...
"""
for rec in records:
# look before you leap
my_person = session.query(Person)\
.filter(
Person.f_name==rec['first_name'],
Person.m_name==rec['middle_name'],
Person.l_name==rec['last_name'],
)\
.first()
if my_person:
print(f'Person "{my_person.full_name}" already exists')
else:
my_person = Person(
f_name=rec['first_name'],
m_name=rec['middle_name'],
l_name=rec['last_name'],
)
session.add(my_person)
# Now get the role that they're in
my_role = session.query(Role).filter(Role.name==rec['role'].lower()).first()
if my_role:
print(f'Role "{my_role.name}" already exists')
else:
my_role = Role(name=rec['role'])
session.add(my_role)
# Commit here, since we need to look up by ids...
session.commit()
# Now add the record for the show they're in
# get the show
my_show = session.query(Show).filter_by(title=rec['show']).first()
assert my_show
work_experience = session.query(ShowsRolesLink)\
.filter_by(
person_id=my_person.id,
role_id=my_role.id,
show_id=my_show.id
)\
.first()
if work_experience:
print(f'Already have that work experience: person_id={my_person.id}; role_id={my_role.id}; show_id={my_show.id}')
else:
work_experience = ShowsRolesLink(
person_id=my_person.id,
role_id=my_role.id,
show_id=my_show.id
)
print(work_experience)
session.add(work_experience)
session.commit()
# All done!
#
#
if __name__ == '__main__':
# Add em all!
add_all_theatres()
add_all_shows()
add_all_people()