-
Notifications
You must be signed in to change notification settings - Fork 1
/
seed.py
321 lines (229 loc) · 8.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
313
314
315
316
317
318
319
320
321
"""Utility file to seed the coffeebuddy database from generated data in seed_data"""
from sqlalchemy import func
from models import connect_to_db, db
from models import *
from random import choice
import datetime
#import pdb; pdb.set_trace()
def load_users():
"""Load users from static/user_data.txt into database."""
print "User"
User.query.delete()
file = open("seed_data/user_data.txt")
for row in file:
row = row.rstrip()
row = row.split("|")
user_id = row[0]
fname = row[1]
lname = row[2]
email = row[3]
user_name = row[4]
password = row[5]
date_of_birth = row[6]
zipcode = row[7]
phone = row[8]
one_word = row[9]
profile_picture = row[10]
#insert user
user = User(user_id=user_id,
fname=fname,
lname=lname,
email=email,
user_name=user_name,
password=password,
date_of_birth=date_of_birth,
zipcode=zipcode,
phone=phone,
one_word=one_word,
profile_picture=profile_picture)
db.session.add(user)
db.session.commit()
def load_books():
"""Load books from book_genre_data into database."""
print "BookGenre"
#read book_genre_data
for row in open("seed_data/book_genre_data.txt"):
row = row.rstrip()
book_genre_id, book_genre_name = row.split("|")
# insert book genre
book = BookGenre(book_genre_id=book_genre_id,
book_genre_name=book_genre_name)
db.session.add(book)
db.session.commit()
def load_movies():
"""Load movies from movie_genre_data into database."""
print "MovieGenre"
for row in open("seed_data/movie_genre_data.txt"):
row = row.rstrip()
movie_genre_id, movie_genre_name = row.split("|")
#insert movie
movie = MovieGenre(movie_genre_id=movie_genre_id,
movie_genre_name=movie_genre_name)
db.session.add(movie)
db.session.commit()
def load_music():
"""Load music from music_genre_data into database."""
print "MusicGenre"
for row in open("seed_data/music_genre_data.txt"):
row = row.rstrip()
music_genre_id, music_genre_name = row.split("|")
#insert music
music = MusicGenre(music_genre_id=music_genre_id,
music_genre_name=music_genre_name)
db.session.add(music)
db.session.commit()
def load_food_habits():
"""Load food_habits from food_habit_data into database."""
print "FoodHabit"
for row in open("seed_data/food_habit_data.txt"):
row = row.rstrip()
food_habit_id, food_habit_name = row.split("|")
#insert habit
habit = FoodHabit(food_habit_id=food_habit_id,
food_habit_name=food_habit_name)
db.session.add(habit)
db.session.commit()
def load_cuisines():
"""Load from fav_cuisine_data into database."""
print "FavCuisine"
for row in open("seed_data/fav_cuisine_data.txt"):
row = row.rstrip()
fav_cuisine_id, fav_cuisine_name = row.split("|")
#insert cuisine
cuisine = FavCuisine(fav_cuisine_id=fav_cuisine_id,
fav_cuisine_name=fav_cuisine_name)
db.session.add(cuisine)
db.session.commit()
def load_hobbies():
"""Load from hobby_data_data into database."""
print "Hobby"
for row in open("seed_data/hobby_data.txt"):
row = row.rstrip()
hobby_id, hobby_name = row.split("|")
#insert hobby
hobby = Hobby(hobby_id=hobby_id,
hobby_name=hobby_name)
db.session.add(hobby)
db.session.commit()
def load_political_views():
"""Load from book_genre_data into database."""
print "PoliticalViews"
for row in open("seed_data/political_view_data.txt"):
row = row.rstrip()
political_view_id, political_view_name = row.split("|")
#insert political view
view = PoliticalView(political_view_id=political_view_id,
political_view_name=political_view_name)
db.session.add(view)
db.session.commit()
def load_religions():
"""Load from book_genre_data into database."""
print "Religions"
for row in open("seed_data/religion_data.txt"):
row = row.rstrip()
religion_id, religion_name = row.split("|")
#insert religion
religion = Religion(religion_id=religion_id,
religion_name=religion_name)
db.session.add(religion)
db.session.commit()
def load_outdoor_activities():
"""Load from book_genre_data into database."""
print "Outdoors"
for row in open("seed_data/outdoor_data.txt"):
row = row.rstrip()
outdoor_id, outdoor_activity = row.split("|")
#insert outdoor
outdoor = Outdoor(outdoor_id=outdoor_id,
outdoor_activity=outdoor_activity)
db.session.add(outdoor)
db.session.commit()
def seed_interests():
""" add data for each user in the interest table"""
books = BookGenre.query.all()
movies = MovieGenre.query.all()
music = MusicGenre.query.all()
food_habits = FoodHabit.query.all()
cuisines = FavCuisine.query.all()
hobbies = Hobby.query.all()
political_views = PoliticalView.query.all()
religions = Religion.query.all()
outdoors = Outdoor.query.all()
users = User.query.all()
for user in users:
new_interest_row = Interest(user_id=user.user_id,
book_genre_id=choice(books).book_genre_id,
movie_genre_id=choice(movies).movie_genre_id,
music_genre_id=choice(music).music_genre_id,
food_habit_id=choice(food_habits).food_habit_id,
fav_cuisine_id=choice(cuisines).fav_cuisine_id,
hobby_id=choice(hobbies).hobby_id,
political_view_id=choice(political_views).political_view_id,
religion_id=choice(religions).religion_id,
outdoor_id=choice(outdoors).outdoor_id)
db.session.add(new_interest_row)
db.session.commit()
def load_user_queries():
""" Load all the queries from pending_match_data.txt to the PendingMatch table"""
print "User Queries"
for row in open("seed_data/pending_match_data.txt"):
row = row.rstrip()
row = row.split("|")
user_id = row[0]
query_pin_code = row[1]
query_time = datetime.datetime.now()
pending = bool(row[2])
#insert pending matches
pending_match = PendingMatch(user_id=user_id,
query_pin_code=query_pin_code,
query_time=query_time,
pending=pending)
db.session.add(pending_match)
db.session.commit()
def set_val_user_id():
"""Set value for the next user_id after seeding database"""
result = db.session.query(func.max(User.user_id)).one()
max_id = int(result[0])
# Set the value for the next user_id to be max_id + 1
query = "SELECT setval('users_user_id_seq', :new_id)"
db.session.execute(query, {'new_id': max_id + 1})
db.session.commit()
def set_val_query_id():
"""Set value for yhe next query_id after seeding database"""
result = db.session.query(func.max(PendingMatch.user_query_id)).one()
max_id = int(result[0])
#set the valuefor the next user_id to be the max_id + 1
query = "SELECT setval('pending_matches_user_query_id_seq', :new_id)"
db.session.execute(query, {'new_id': max_id + 1})
db.session.commit()
def set_val_interest_id():
"""Set value for the next user_id after seeding database"""
result = db.session.query(func.max(Interest.interest_id)).one()
max_id = int(result[0])
# Set the value for the next user_id to be max_id + 1
query = "SELECT setval('interests_interest_id_seq', :new_id)"
db.session.execute(query, {'new_id': max_id + 1})
db.session.commit()
########################################################################################################
if __name__ == "__main__":
from flask import Flask
from server import app
connect_to_db(app)
# In case tables haven't been created, create them
db.create_all()
# Import different types of data
load_users()
load_books()
load_movies()
load_music()
load_food_habits()
load_cuisines()
load_hobbies()
load_political_views()
load_religions()
load_outdoor_activities()
load_user_queries()
seed_interests()
set_val_user_id()
set_val_interest_id()
set_val_query_id()