-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_sample_data.py
284 lines (237 loc) · 11.9 KB
/
load_sample_data.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
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database_setup import Region, Base, Place, User
import httplib2
import json
import datetime
import time
engine = create_engine('sqlite:///travelbucketlist.db')
# Bind the engine to the metadata of the Base class so that the
# declaratives can be accessed through a DBSession instance
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
# A DBSession() instance establishes all conversations with the database
# and represents a "staging zone" for all the objects loaded into the
# database session object. Any change made against the objects in the
# session won't be persisted into the database until you call
# session.commit(). If you're not happy about the changes, you can
# revert all of them back to the last commit by calling
# session.rollback()
session = DBSession()
print "Generating users..."
# Create dummy user
def GenerateUser(visible):
# Need to wait between requests in order for the random user api to select different users
time.sleep(2)
# Retrieve random user info for dummy user accounts
url = 'http://api.randomuser.me/'
h = httplib2.Http()
# Create dummy user
result = h.request(url, 'GET')[1]
res = json.loads(result)
data = res["results"][0]["user"]
return User(name="%s, %s" % (data["name"]["first"], data["name"]["last"]),
email=data["email"],
picture=data["picture"]["medium"],
allow_public_access=visible,
signup_date=datetime.datetime.fromtimestamp(float(data["registered"]))
)
# Create dummy user 1
user1 = GenerateUser(1)
session.add(user1)
# Create dummy user 2
user2 = GenerateUser(1)
session.add(user2)
# Create dummy user 3
user3 = GenerateUser(1)
session.add(user3)
# Create dummy user 4
user4 = GenerateUser(1)
session.add(user4)
print "Generating regions..."
region11 = Region(name = "Japan",
picture = "tb000004.jpg",
geo_location = "https://www.google.ca/maps/place/Japan/@34.7857324,134.3756902,5z/data=!3m1!4b1!4m2!3m1!1s0x34674e0fd77f192f:0xf54275d47c665244",
rating = 8,
creation_date = datetime.datetime(2014, 03, 15, 9, 23, 03),
modifiy_date = datetime.datetime(2014, 03, 15, 9, 23, 03),
user = user1
)
session.add(region11)
region12 = Region(name = "New Zealand",
picture = "tb000009.jpg",
geo_location = "https://www.google.ca/maps/place/New+Zealand/data=!4m2!3m1!1s0x6d2c200e17779687:0xb1d618e2756a4733?sa=X&ei=AM50Vev3FsH-yQSThIKABw&ved=0CIIBEPIBMBI",
rating = 9,
creation_date = datetime.datetime(2013, 8, 15, 15, 45, 22),
modifiy_date = datetime.datetime(2013, 8, 15, 15, 45, 22),
user = user1
)
session.add(region12)
region13 = Region(name = "Montreal",
picture = "tb000007.jpg",
geo_location = "https://www.google.ca/maps/place/Montreal,+QC/data=!4m2!3m1!1s0x4cc91a541c64b70d:0x654e3138211fefef?sa=X&ei=Z850VfOUBsivyATM1rWQDA&ved=0CIYBEPIBMBI",
rating = 8,
creation_date = datetime.datetime(2015, 01, 22, 11, 9, 22),
modifiy_date = datetime.datetime(2015, 01, 22, 11, 9, 22),
user = user1
)
session.add(region13)
region21 = Region(name = "New Zealand",
picture = "tb000010.jpg",
geo_location = "https://www.google.ca/maps/place/New+Zealand/data=!4m2!3m1!1s0x6d2c200e17779687:0xb1d618e2756a4733?sa=X&ei=AM50Vev3FsH-yQSThIKABw&ved=0CIIBEPIBMBI",
rating = 8,
creation_date = datetime.datetime(2014, 05, 04, 7, 55, 47),
modifiy_date = datetime.datetime(2014, 05, 04, 7, 55, 47),
user = user2
)
session.add(region21)
region22 = Region(name = "Australia",
picture = "tb000001.jpg",
geo_location = "https://www.google.ca/maps/place/Australia/data=!4m2!3m1!1s0x2b2bfd076787c5df:0x538267a1955b1352?sa=X&ei=CdB0VYuoLY73yQSC-IOYBg&ved=0CIIBEPIBMBI",
rating = 7,
creation_date = datetime.datetime(2014, 05, 04, 7, 58, 24),
modifiy_date = datetime.datetime(2014, 05, 04, 7, 58, 24),
user = user2
)
session.add(region22)
region31 = Region(name = "New York City",
picture = "tb000005.jpg",
geo_location = "https://www.google.ca/maps/place/New+York,+NY,+USA/@40.7033127,-73.979681,10z/data=!3m1!4b1!4m2!3m1!1s0x89c24fa5d33f083b:0xc80b8f06e177fe62",
rating = 9,
creation_date = datetime.datetime(2014, 12, 23, 7, 42, 17),
modifiy_date = datetime.datetime(2014, 12, 23, 7, 42, 17),
user = user3
)
session.add(region31)
region32 = Region(name = "Nepal",
picture = "tb000006.jpg",
geo_location = "https://www.google.ca/maps/place/Nepal/data=!4m2!3m1!1s0x3995e8c77d2e68cf:0x34a29abcd0cc86de?sa=X&ei=sNB0VaOmLpKayATnmoOACg&ved=0CIMBEPIBMBI",
rating = 10,
creation_date = datetime.datetime(2012, 6, 15, 14, 33, 38),
modifiy_date = datetime.datetime(2012, 6, 15, 14, 33, 38),
user = user3
)
session.add(region32)
region41 = Region(name = "New York City",
picture = "tb000011.jpg",
geo_location = "https://www.google.ca/maps/place/New+York,+NY,+USA/@40.7033127,-73.979681,10z/data=!3m1!4b1!4m2!3m1!1s0x89c24fa5d33f083b:0xc80b8f06e177fe62",
rating = 9,
creation_date = datetime.datetime(2014, 12, 14, 12, 34, 17),
modifiy_date = datetime.datetime(2014, 12, 14, 12, 34, 17),
user = user4
)
session.add(region41)
region42 = Region(name = "Detroit",
picture = "tb000002.jpg",
geo_location = "https://www.google.ca/maps/place/Detroit,+MI,+USA/@42.352711,-83.099205,11z/data=!3m1!4b1!4m2!3m1!1s0x8824ca0110cb1d75:0x5776864e35b9c4d2",
rating = 5,
creation_date = datetime.datetime(2014, 12, 14, 12, 34, 17),
modifiy_date = datetime.datetime(2014, 12, 14, 12, 34, 17),
user = user4
)
session.add(region42)
region43 = Region(name = "San Francisco",
picture = "tb000008.jpg",
geo_location = "https://www.google.ca/maps/place/San+Francisco,+CA,+USA/data=!4m2!3m1!1s0x80859a6d00690021:0x4a501367f076adff?sa=X&ei=k9F0VeyjCov3yQSp7YGADw&ved=0CJcBEPIBMBI",
rating = 9,
creation_date = datetime.datetime(2014, 12, 14, 12, 34, 17),
modifiy_date = datetime.datetime(2014, 12, 14, 12, 34, 17),
user = user4
)
session.add(region43)
region44 = Region(name = "Germany",
picture = "tb000003.jpg",
geo_location = "https://www.google.ca/maps/place/Germany/data=!4m2!3m1!1s0x479a721ec2b1be6b:0x75e85d6b8e91e55b?sa=X&ved=0CIcBEPIBMBNqFQoTCM_9jMOEjcYCFYoVkgodeaoAtA",
rating = 9,
creation_date = datetime.datetime(2015, 6, 14, 9, 34, 17),
modifiy_date = datetime.datetime(2015, 6, 14, 9, 34, 17),
user = user4
)
session.add(region44)
print "Generating places..."
place111 = Place(name = "Visit Tokyo",
description = "Tokyo, Japan's bustling capital, mixes the ultramodern and the traditional, from neon-lit skyscrapers and anime shops to cherry trees and temples. The opulent Meiji Shinto Shrine is known for its towering gate and surrounding forests. The Imperial Palace sits amid sprawling public gardens. The city is famed for its vibrant food scene, and its Shibuya and Harajuku districts are the heart of its trendy teen fashion scene.",
picture = "tb000012.jpg",
geo_location = "https://www.google.ca/maps/place/Tokyo,+Japan/data=!4m2!3m1!1s0x605d1b87f02e57e7:0x2e01618b22571b89?sa=X&ei=09Z0VaDWE8-WyASgporwCw&ved=0CJcBEPIBMBI",
info_website = "http://www.gotokyo.org/en/",
rating = 7,
creation_date = region11.creation_date,
modifiy_date = region11.creation_date,
region = region11,
user = user1
)
session.add(place111)
place112 = Place(name = "Climb Mount Fuji",
description = "Mount Fuji, located on Honshu Island, is the highest mountain in Japan at 3,776.24 m. An active stratovolcano that last erupted in 1707-08, Mount Fuji lies about 100 kilometres south-west of Tokyo, and can be seen from there on a clear day.",
picture = "tb000013.jpg",
geo_location = "https://www.google.ca/maps/place/Mt+Fuji/data=!4m2!3m1!1s0x60196290556df7cf:0x8d5003885b877511!5m1!1e4?sa=X&ei=PNd0VaeaJZGSyAS7zIKwDg&ved=0CJIBEPIBMA4",
info_website = "http://www.japan-guide.com/e/e2172.html",
rating = 9,
creation_date = region11.creation_date,
modifiy_date = region11.creation_date,
region = region11,
user = user1
)
session.add(place112)
place121 = Place(name = "Go to Queenstown",
description = "Queenstown, New Zealand, sits on the shore of the South Island's Lake Wakatipu, set against the dramatic Southern Alps. The surrounding Central Otago region is known for its Pinot Noir and Chardonnay vineyards, and for adventure sports. In winter, there's backcountry skiing and the country's highest vertical drops. Summer brings paragliding, mountain biking and bungee-jumping (Kawarau Gorge Suspension Bridge is among the sport's original sites).",
picture = "tb000014.jpg",
geo_location = "https://www.google.ca/maps/place/Queenstown,+New+Zealand/data=!4m2!3m1!1s0xa9d51df1d7a8de5f:0x500ef868479a600?sa=X&ei=s9d0VcKlK86YyATh34PwAQ&ved=0CJkBEPIBMBA",
info_website = "http://www.japan-guide.com/e/e2172.html",
rating = 9,
creation_date = region12.creation_date,
modifiy_date = region12.creation_date,
region = region12,
user = user1
)
session.add(place121)
place122 = Place(name = "Milford Sound",
description = "Milford Sound is a fiord in the south west of New Zealand's South Island, within Fiordland National Park, Piopiotahi Marine Reserve, and the Te Wahipounamu World Heritage site.",
picture = "tb000015.jpg",
geo_location = "https://www.google.ca/maps/place/Milford+Sound/data=!4m2!3m1!1s0xa9d5e04dba4b49e1:0x2a00ef86ab64de00?sa=X&ei=E9h0VYWHM8X9yQS1rIDIAw&ved=0CIUBEPIBMA4",
info_website = "http://www.milford-sound.co.nz/",
rating = 9,
creation_date = region12.creation_date,
modifiy_date = region12.creation_date,
region = region12,
user = user1
)
session.add(place122)
place211 = Place(name = "Go to Queenstown",
description = "Queenstown, New Zealand, sits on the shore of the South Island's Lake Wakatipu, set against the dramatic Southern Alps. The surrounding Central Otago region is known for its Pinot Noir and Chardonnay vineyards, and for adventure sports. In winter, there's backcountry skiing and the country's highest vertical drops. Summer brings paragliding, mountain biking and bungee-jumping (Kawarau Gorge Suspension Bridge is among the sport's original sites).",
picture = "tb000016.jpg",
geo_location = "https://www.google.ca/maps/place/Queenstown,+New+Zealand/data=!4m2!3m1!1s0xa9d51df1d7a8de5f:0x500ef868479a600?sa=X&ei=s9d0VcKlK86YyATh34PwAQ&ved=0CJkBEPIBMBA",
info_website = "http://www.japan-guide.com/e/e2172.html",
rating = 8,
creation_date = region21.creation_date,
modifiy_date = region21.creation_date,
region = region21,
user = user1
)
session.add(place211)
place212 = Place(name = "Milford Sound",
description = "Milford Sound is a fiord in the south west of New Zealand's South Island, within Fiordland National Park, Piopiotahi Marine Reserve, and the Te Wahipounamu World Heritage site.",
picture = "tb000017.jpg",
geo_location = "https://www.google.ca/maps/place/Milford+Sound/data=!4m2!3m1!1s0xa9d5e04dba4b49e1:0x2a00ef86ab64de00?sa=X&ei=E9h0VYWHM8X9yQS1rIDIAw&ved=0CIUBEPIBMA4",
info_website = "http://www.milford-sound.co.nz/",
rating = 10,
creation_date = region21.creation_date,
modifiy_date = region21.creation_date,
region = region21,
user = user1
)
session.add(place212)
place212 = Place(name = "Abel Tasman",
description = "Abel Tasman National Park is a New Zealand national park located between Golden Bay and Tasman Bay at the north end of the South Island.",
picture = "tb000018.jpg",
geo_location = "https://www.google.ca/maps/place/abel+tasman+national+park/@-40.934685,172.972155,10z/data=!4m2!3m1!1s0x0:0xf00ef87655bee50?sa=X&ei=Zdl0VY3YIZCZyATp1YCgCA&ved=0CJQBEPwSMBA",
info_website = "https://www.abeltasman.co.nz/",
rating = 9,
creation_date = region21.creation_date,
modifiy_date = region21.creation_date,
region = region21,
user = user1
)
session.add(place212)
session.commit()
print "Sample Data Generation Complete."