-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed.py
More file actions
273 lines (177 loc) · 7.95 KB
/
seed.py
File metadata and controls
273 lines (177 loc) · 7.95 KB
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
"""Utility file to seed Givr database from seed_recipients and seed_recipient_orgs data in seed_data/"""
from sqlalchemy import func
from model import Givr, Giv, Recipient, Recipient_org, Alt_choice, Restaurant, Item
from datetime import datetime, timedelta
import random
import time
from model import connect_to_db, db
from server import app
from dateutil import parser
def load_alt_choice():
"""Load alt_choice from seed_alt_choices into database."""
print "Alternative Choices"
Alt_choice.query.delete()
for row in open("seed/seed_alt_choices.txt"):
row = row.rstrip()
alt_choice_id, description = row.split("|")
recipient_org = Alt_choice(alt_choice_id=alt_choice_id,
description=description)
db.session.add(recipient_org)
db.session.commit()
def load_givs():
"""Load givs from seed_givs.txt into database."""
print "Givs"
Giv.query.delete()
for row in open("seed/seed_givs.txt"):
row = row.rstrip()
(giv_id, givr_id, restaurant_id, date_of_order, date_of_delivery,
requested_destination, actual_destination, total_amount,
successful_delivery, recipient_id, size, tax_exempt) = row.split("|")
recipient_org = Alt_choice(alt_choice_id=alt_choice_id,
description=description)
db.session.add(recipient_org)
db.session.commit()
def get_random_date(start_date, end_date):
""" This will pick a random date for each instantiated giv
"""
start_date_secs = time.mktime(time.strptime(start_date, '%m-%d-%Y %H:%M:%S'))
end_date_secs = time.mktime(time.strptime(end_date, '%m-%d-%Y %H:%M:%S'))
random_date_secs = start_date_secs + random.random() * (end_date_secs - start_date_secs)
#return time.strftime('%m-%d-%Y %H:%M:%S', time.localtime(random_date_secs))
return datetime.fromtimestamp(random_date_secs)
def create_givs(addresses, givr_id, num_givs_per_address, start_date, end_date):
""" 3 Addresses, Time of day/date,
created, delivered, delivery address,
"""
givr = Givr.query.filter_by(givr_id=givr_id).first()
giv = Giv.query.filter_by(givr_id=givr_id).first()
for address in addresses:
num_extra_givs = random.randint(1, 10)
to_add = random.choice([True, False])
if to_add == True:
num_givs_per_address += num_extra_givs
else:
num_givs_per_address -= num_extra_givs
for i in range(num_givs_per_address):
date_of_order = get_random_date(start_date, end_date)
date_of_delivery = date_of_order
date_of_delivery = date_of_delivery + timedelta(hours=1)
total_amount = random.randint(5, givr.smallgiv)
successful_delivery = True
size = "smallgiv"
if (address == "1001 Polk Street, San Francisco, California 94109") or (address == "536 Central Avenue, San Francisco, California 94117"):
tax_exempt = True
else:
tax_exempt = False
giv = Giv(givr_id=givr_id,
restaurant_id=1,
date_of_order=date_of_order,
date_of_delivery=date_of_delivery,
requested_destination=address,
actual_destination=address,
total_amount=total_amount,
successful_delivery=successful_delivery,
size=size,
tax_exempt=tax_exempt)
# add num days to start date
#source https://stackoverflow.com/questions/553303/generate-a-random-date-between-two-other-dates
print "We created a new Giv!"
db.session.add(giv)
db.session.commit()
def load_givrs():
"""Load users from u.user into database."""
print "Givrs"
# Delete all rows in table, so if we need to run this a second time,
# we won't be trying to add duplicate users
Givr.query.delete()
# Read u.user file and insert data
for row in open("seed/seed_givrs"):
row = row.rstrip()
(givr_id, email, password, fname, lname, creditcardtype, creditcardname, creditcardnum,
creditcardexp, creditcardccv, smallgiv, biggiv, alt_choice_id) = row.split("|")
# creditcardexp = datetime.strptime(creditcardexp, "%m/%y")
creditcardexp = datetime.strptime(creditcardexp, "%m/%y").strftime("%Y-%m-%d")
givr = Givr(
email=email,
password=password,
fname=fname,
lname=lname,
creditcardtype=creditcardtype,
creditcardname=creditcardname,
creditcardnum=creditcardnum,
creditcardexp=creditcardexp,
creditcardccv=creditcardccv,
smallgiv=smallgiv,
biggiv=biggiv,
alt_choice_id=alt_choice_id)
# We need to add to the session or it won't ever be stored
db.session.add(givr)
# Once we're done, we should commit our work
db.session.commit()
def load_recipients():
"""Load recipients from seed_recipients into database."""
print "Recipients"
Recipient.query.delete()
for row in open("seed/seed_recipients.txt"):
row = row.rstrip()
recipient_id, address, city, state, zipcode, latitude, longitude, recipient_type = row.split("|")
recipient = Recipient(recipient_id=recipient_id,
address=address,
city=city,
state=state,
zipcode=zipcode,
latitude=latitude,
longitude=longitude,
recipient_type=recipient_type)
db.session.add(recipient)
db.session.commit()
def load_restaurants():
"""Load restaurants from seed_restaurants into database."""
print "Restaurants"
Restaurant.query.delete()
for row in open("seed/seed_restaurants.txt"):
row = row.rstrip()
restaurant_id, name, address, delivery_fee = row.split("|")
restaurant = Restaurant(restaurant_id=restaurant_id,
name=name,
address=address,
delivery_fee=delivery_fee)
db.session.add(restaurant)
db.session.commit()
def load_items():
""" Load Items from seed_items into database"""
print "Items"
Item.query.delete()
for row in open("seed/seed_items.txt"):
row = row.rstrip()
item_id, restaurant_id, name, price = row.split("|")
item = Item(item_id=item_id, restaurant_id=restaurant_id, name=name, price=price)
db.session.add(item)
db.session.commit()
def load_recipient_orgs():
"""Load recipient_orgs from seed_recipient_orgs into database."""
print "Recipient_orgs"
Recipient_org.query.delete()
for row in open("seed/seed_recipient_orgs.txt"):
row = row.rstrip()
org_id, recipient_id, name, phone, url = row.split("|")
recipient_org = Recipient_org(org_id=org_id,
recipient_id=recipient_id,
name=name,
phone=phone,
url=url)
db.session.add(recipient_org)
db.session.commit()
if __name__ == "__main__":
connect_to_db(app)
# In case tables haven't been created, create them
db.create_all()
# Import different types of data
# load_alt_choice()
# load_givrs()
# load_restaurants()
# load_items()
# load_recipients()
# load_recipient_orgs()
create_givs(("600 Townsend Street, San Francisco, CA 94103", "598 Market St, San Francisco, CA 94104", "1001 Polk Street, San Francisco, California 94109", "536 Central Avenue, San Francisco, California 94117"),
000000001, 300, "01-01-2018 01:01:01", "06-18-2018 01:01:01")