-
Notifications
You must be signed in to change notification settings - Fork 3
/
seed.py
227 lines (171 loc) · 6.32 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
"""Utility file to seed volunteerdb.
This is 'faker' data for test and demo purposes."""
import csv
from model import User, Relational, Trip, Wishlist
from model import connect_to_db, db
from server import app
################################################################################
# functions
def clear_tables():
"""Delete data from all tables in the database.
Delete in dependancy reverse-order to avoid errors related to foreign key relationships."""
# 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()
Relational.query.delete()
Trip.query.delete()
Wishlist.query.delete()
print('ALL TABLES HAVE BEEN CLEARED OF THEIR DATA')
def inform_tables_loaded():
"""indicate that all functions ran"""
print('ALL TABLES HAVE BEEN LOADED WITH DATA')
def add_users():
"""add users to the user table."""
# c1 = User(
# user_full_name = "Ada Lovelace",
# email = "[email protected]",
# password = "123",
# uzipcode = "94805",
# is_asker= True,
# is_vol= False
# )
# db.session.add(c1)
# # print(f'{c1.user_full_name, c1.is_vol}')
# c2 = User(
# user_full_name= "Grace Hopper",
# email = "[email protected]",
# password = "1235",
# uzipcode = "94121",
# is_asker= False,
# is_vol= True
# )
"""Load user information from seed file into the database.
working code.
"""
print("users")
# opening seed file with the csv library and csv reader.
with open('seed_files/User_seed_v1.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
# print(f'Column names are {", ".join(row)}')
line_count += 1
else:
asker_seed = row[5]
if asker_seed == "TRUE":
asker_seed = True
elif asker_seed == "":
asker_seed = False
vol_seed = row[6]
if vol_seed == "TRUE":
vol_seed = True
elif vol_seed == "":
vol_seed = False
user = User(
user_full_name = row[1],
email = row[2],
# password_hash = row[3],
uzipcode = row[4],
is_asker= asker_seed,
is_vol= vol_seed,
trust_score = row[7]
)
user.set_password(row[3])
db.session.add(user)
line_count += 1
db.session.commit()
print(f'created {line_count} users')
def add_relationals():
"""add relationship data to the relational table."""
# r1 = Relational(
# r_asker_id= 1,
# r_vol_id = 2,
# r_trip_id = 1
# # r_vol_id= 2,
# # r_trip_id = 1
# )
# # print(f'{r1.r_trip_id}')
# db.session.add(r1)
# db.session.commit()
# print(f'created relationals {r1}')
"""Load relationship information from seed file into the database.
working code.
"""
print("trips")
# opening seed file with the csv library and csv reader.
with open('seed_files/Relational_seed_v1.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
# print(f'Column names are {", ".join(row)}')
line_count += 1
else:
relation = Relational(
r_asker_id= int(row[1]),
r_vol_id = int(row[2]),
r_trip_id = int(row[3])
)
db.session.add(relation)
line_count += 1
db.session.commit()
print(f'created {line_count} relationals')
def add_trips():
"""Add trip data to the trips table."""
# t1 = Trip(
# trip_zipcode= "94805",
# user_id= 1,
# wishlist = "Apples, berries, sweet potatoes",
# item_progress= "En Route"
# )
# db.session.add(t1)
# db.session.commit()
# print(f'created trips {t1}')
"""Load trip information from seed file into the database.
working code.
"""
print("trips")
# opening seed file with the csv library and csv reader.
with open('seed_files/Trip_seed_v1.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
# print(f'Column names are {", ".join(row)}')
line_count += 1
else:
trip = Trip(
trip_zipcode= row[1],
user_id= int(row[2]),
wishlist = row[3],
item_progress= row[4]
)
db.session.add(trip)
line_count += 1
db.session.commit()
print(f'created {line_count} trips')
def add_wishlist():
"""Add wishlist data to the wishlist class."""
w1 = Wishlist(
)
db.session.add(w1)
db.session.commit()
print(f'created trips {w1}')
################################################################################
# helper functions for seeding the model
if __name__ == "__main__":
connect_to_db(app)
# In case tables haven't been created, create them
db.create_all()
# Run functions
# clear_tables()
#load functions
add_users()
add_trips()
add_relationals()
inform_tables_loaded()
print(f'\n\nusers table: {User.query.all()}')
print(f'\n\ntrips table: {Trip.query.all()}')
print(f'\n\nrelationals table: {Relational.query.all()}')
print(f'\n\nwishlist table: {Wishlist.query.all()}')