/
seed.py
313 lines (225 loc) · 9.9 KB
/
seed.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
"""Utility file to seed ratings database from MovieLens data in seed_data/"""
from sqlalchemy import func
from model import Animal, Color, AnimalColor, Species, Size, Breed, Gender, User, Lost_Pet_Submission, lostPetColor, connect_to_db, db #add userAnimal?
from server import app
# ADD LATER: from model import User
def load_colors(color_filename):
"""Load colors from seed_data/generic_colors into database."""
# Delete all rows in table, so if we need to run this a second time,
# we won't be trying to add duplicate users
Color.query.delete()
#Read generic_colors file and insert data
for row in open(color_filename):
row = row.rstrip()
color_id, color = row.split("|")
colors = Color(color=color)
# We need to add to the session or it won't ever be stored
db.session.add(colors)
# Once we're done, we should commit our work
db.session.commit()
#finished the function
print("Colors inserted")
def load_species(species_filename):
"""Load species from seed_data/species into database."""
# Delete all rows in table, so if we need to run this a second time,
# we won't be trying to add duplicate users
Species.query.delete()
#Read species file and insert data
for row in open(species_filename):
row = row.rstrip()
species_id, species = row.split("|")
the_species = Species(species=species)
# We need to add to the session or it won't ever be stored
db.session.add(the_species)
# Once we're done, we should commit our work
db.session.commit()
#finished the function
print("Species inserted")
def load_sizes(sizes_filename):
"""Load sizes from seed_data/sizes into database."""
# Delete all rows in table, so if we need to run this a second time,
# we won't be trying to add duplicate users
# BROKEN?
Size.query.delete()
#Read sizes file and insert data
for row in open(sizes_filename):
row = row.rstrip()
size_id, size = row.split("|")
sizes = Size(size=size)
# We need to add to the session or it won't ever be stored
db.session.add(sizes)
# Once we're done, we should commit our work
db.session.commit()
#finished the function
print("Sizes inserted")
def load_breeds(breeds_filename):
"""Load breeds from seed_data/all_breeds into database."""
# Delete all rows in table, so if we need to run this a second time,
# we won't be trying to add duplicate users
Breed.query.delete()
#Read sizes file and insert data
for row in open(breeds_filename):
row = row.rstrip()
species_id, breed = row.split("|")
breeds = Breed(species_id=species_id,
breed=breed)
# We need to add to the session or it won't ever be stored
db.session.add(breeds)
# Once we're done, we should commit our work
db.session.commit()
#finished the function
print("Breeds inserted")
def load_genders(genders_filename):
"""Load breeds from seed_data/all_breeds into database."""
# Delete all rows in table, so if we need to run this a second time,
# we won't be trying to add duplicate users
Gender.query.delete()
#Read sizes file and insert data
for row in open(genders_filename):
row = row.rstrip()
gender_id, gender = row.split("|")
genders = Gender(gender_id=gender_id,
gender=gender)
# We need to add to the session or it won't ever be stored
db.session.add(genders)
# Once we're done, we should commit our work
db.session.commit()
#finished the function
print("Genders inserted")
def load_users(users_filename):
"""Load load_users from seed_data/load_users into database."""
# Delete all rows in table, so if we need to run this a second time,
# we won't be trying to add duplicate users
User.query.delete()
#Read load_users file and insert data
for row in open(users_filename):
row = row.rstrip()
username, email, password, f_name, l_name = row.split("|")
users = User(username=username,
email=email,
password=password,
f_name=f_name,
l_name=l_name)
# We need to add to the session or it won't ever be stored
db.session.add(users)
# Once we're done, we should commit our work
db.session.commit()
#finished the function
print("Users inserted")
def load_animals(animal_filename):
"""Load colors from seed_data/generic_colors into database."""
# Delete all rows in table, so if we need to run this a second time,
# we won't be trying to add duplicate users
Animal.query.delete()
#Read generic_colors file and insert data
for row in open(animal_filename):
row = row.rstrip()
animal_id, species_id, breed_id, size_id, user_id, latitude, longitude, timestamp, photo, notes, found, found_by_user_id, gender_id = row.split("|")
if found_by_user_id == "None":
found_by_user_id = None
if found == "True":
found = True
else:
found = False
animals = Animal(species_id=species_id,
breed_id=breed_id,
size_id=size_id,
user_id=user_id,
latitude=latitude,
longitude=longitude,
timestamp=timestamp,
photo=photo,
notes=notes,
found=bool(found),
found_by_user_id = found_by_user_id,
gender_id = gender_id)
# We need to add to the session or it won't ever be stored
db.session.add(animals)
# Once we're done, we should commit our work
db.session.commit()
#finished the function
print("Animals inserted")
def load_animalColors(animalColors_filename):
"""Load animalColors from seed_data/animalColors into database."""
# Delete all rows in table, so if we need to run this a second time,
# we won't be trying to add duplicate users
AnimalColor.query.delete()
#Read animalColors file and insert data
for row in open(animalColors_filename):
row = row.rstrip()
animal_color_id, animal_id, color_id = row.split("|")
animalColors = AnimalColor(animal_id=animal_id,
color_id=color_id)
# We need to add to the session or it won't ever be stored
db.session.add(animalColors)
# Once we're done, we should commit our work
db.session.commit()
#finished the function
print("Animal Colors inserted")
def load_lostPetPosters(lostPet_filename):
"""Loads lost animals into lost_pet_posters table."""
# Delete all rows in table, so if we need to run this a second time,
# we won't be trying to add duplicate users
Lost_Pet_Submission.query.delete()
#Read animalColors file and insert data
for row in open(lostPet_filename):
row = row.rstrip()
pet_id, species_id, breed_id, user_id, pet_name, latitude, longitude, date_lost, photo, notes, gender_id = row.split("|")
lost_Pet_Submissions = Lost_Pet_Submission(species_id=species_id,
breed_id=breed_id,
user_id=user_id,
pet_name=pet_name,
latitude=latitude,
longitude=longitude,
date_lost=date_lost,
photo=photo,
notes=notes,
gender_id=gender_id)
# We need to add to the session or it won't ever be stored
db.session.add(lost_Pet_Submissions)
# Once we're done, we should commit our work
db.session.commit()
#finished the function
print("lost pets inserted")
def load_lostPetColors(lostPetColors_filename):
"""Load lostPetColors from seed_data/lostPetColors into database."""
# Delete all rows in table, so if we need to run this a second time,
# we won't be trying to add duplicate users
lostPetColor.query.delete()
#Read lostPetColor file and insert data
for row in open(lostPetColors_filename):
row = row.rstrip()
animal_color_id, pet_id, color_id = row.split("|")
lostPetColors = lostPetColor(pet_id=pet_id,
color_id=color_id)
# We need to add to the session or it won't ever be stored
db.session.add(lostPetColors)
# Once we're done, we should commit our work
db.session.commit()
#finished the function
print("lostPet Colors inserted")
if __name__ == "__main__":
connect_to_db(app)
# In case tables haven't been created, create them
db.create_all()
# Import different types of data
animal_filename = "seed_data/lost_pets_data"
color_filename = "seed_data/generic_colors"
species_filename = "seed_data/species"
sizes_filename = "seed_data/sizes"
breeds_filename = "seed_data/all_breeds"
genders_filename = "seed_data/gender"
animalColors_filename = "seed_data/animalColors"
users_filename = "seed_data/users"
lostPet_filename = "seed_data/lost_pet_posters_data"
lostPetColors_filename = "seed_data/lostPetColors_data"
load_colors(color_filename)
load_species(species_filename)
load_sizes(sizes_filename)
load_breeds(breeds_filename)
load_genders(genders_filename)
load_users(users_filename)
load_animals(animal_filename)
load_animalColors(animalColors_filename)
load_lostPetPosters(lostPet_filename)
load_lostPetColors(lostPetColors_filename)