-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanage.py
More file actions
410 lines (356 loc) · 15.7 KB
/
manage.py
File metadata and controls
410 lines (356 loc) · 15.7 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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
from app import create_app, db
from app.constants import status
import os
import googlemaps
import datetime
import random
from flask import Flask
from flask_script import Manager, Shell
from flask_migrate import Migrate, MigrateCommand
from flask_sqlalchemy import SQLAlchemy
from flask_googlemaps import GoogleMaps
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DATABASE_URL']
app.config['GOOGLEMAPS_KEY'] = os.environ['GOOGLEMAPS_KEY']
app.config['MAPS_API'] = os.environ['MAPS_API']
app.config['ALLOWED_EXTENSIONS'] = set(['png', 'jpg', 'jpeg'])
app.config['UPLOAD_FOLDER'] = './app/static/img/userpics/'
db = SQLAlchemy(app)
GoogleMaps(app)
with app.app_context():
from app.models import *
manager = Manager(app)
migrate = Migrate(app, db)
def make_shell_context():
return dict(app=app, db=db, User=User)
manager.add_command("shell", Shell(make_context=make_shell_context))
manager.add_command('db', MigrateCommand)
@manager.command
def test():
"""Run the unit tests."""
import unittest
tests = unittest.TestLoader().discover('tests')
unittest.TextTestRunner(verbosity=2).run(tests)
@manager.command
def fixdb():
"""Fixes spelling errors in the db from before constants are implemented."""
jobs = Job.query.all()
for job in jobs:
job.status = status.PENDING
job.price = 3.50
job.review = None
job.rating = None
job.accepted_id = None
job.date_accepted = None
job.date_completed = None
users = User.query.all()
for user in users:
user.picture_path = "/static/img/userpics/default_pic.png"
job_requestors = JobRequestor.query.all()
for job_requestor in job_requestors:
job_requestor.price = 3.48
db.session.commit()
@manager.command
def emptydb():
"""Deletes all entries in the database."""
try:
# Clear all requestors
jobRequestors = JobRequestor.query.all()
for jobRequestor in jobRequestors:
db.session.delete(jobRequestor)
# Clear all jobs
jobs = Job.query.all()
for job in jobs:
db.session.delete(job)
# Clear all users
users = User.query.all()
for user in users:
db.session.delete(user)
db.session.commit()
print("Emptied database! -- SUCCESS")
except Exception as e:
print("Error emptying database! -- FAILED")
print("Error: %s" % e)
@manager.command
def populatedb():
"""Fills database with fake data."""
default_password = 'Password1'
# Create users
user1 = User(email='sahir.karani@gmail.com',
password=default_password,
first_name='Sahir',
last_name='Karani',
picture_path="/static/img/userpics/default_pic.png",
validated=True)
user2 = User(email='brandon.tang@gmail.com',
password=default_password,
first_name='Brandon',
last_name='Tang',
picture_path="/static/img/userpics/default_pic.png",
validated=True)
user3 = User(email='matthew.laikhram@gmail.com',
password=default_password,
first_name='Matthew',
last_name='Laikhram',
picture_path="/static/img/userpics/default_pic.png",
validated=True)
user4 = User(email='vincent.wong@gmail.com',
password=default_password,
first_name='Vincent',
last_name='Wong',
picture_path="/static/img/userpics/default_pic.png",
validated=True)
# Add users to database
try:
db.session.add(user1)
db.session.add(user2)
db.session.add(user3)
db.session.add(user4)
db.session.commit()
print("Added users! -- SUCCESS")
except Exception as e:
print("Error adding users! -- FAILED")
print("Error: %s" % e)
sys.exit(1)
gmaps = googlemaps.Client(key=app.config['MAPS_API'])
geocode_result = gmaps.geocode('1600 Pennsylvania Ave' + ", " + str(20500))
job1 = Job(name='Insulation Replacement',
description='Need to replace the insulation in my basement.',
price=150,
status=status.PENDING,
location='1600 Pennsylvania Ave',
longitude=round(geocode_result[0]['geometry']['location']['lng'], 6),
latitude=round(geocode_result[0]['geometry']['location']['lat'], 6),
zipcode=20500,
creator_id=user1.id,
date_created=datetime.now())
geocode_result = gmaps.geocode('11 Wall Street' + ", " + str(10005))
job2 = Job(name='Bathroom Renovation',
description='Need to redecorate my upstairs bathroom.',
price=500,
status=status.PENDING,
location='11 Wall Street',
longitude=round(geocode_result[0]['geometry']['location']['lng'], 6),
latitude=round(geocode_result[0]['geometry']['location']['lat'], 6),
zipcode=10005,
creator_id=user1.id,
date_created=datetime.now())
geocode_result = gmaps.geocode('N 6th St & Market St' + ", " + str(19106))
job3 = Job(name='Kitchen Sink Leaking',
description='Need someone to snake my pipes.',
price=150,
status=status.PENDING,
location='N 6th St & Market St',
longitude=round(geocode_result[0]['geometry']['location']['lng'], 6),
latitude=round(geocode_result[0]['geometry']['location']['lat'], 6),
zipcode=19106,
creator_id=user1.id,
date_created=datetime.now())
geocode_result = gmaps.geocode('50 W 10th St' + ", " + str(10011))
job4 = Job(name='Raking Leaves',
description='Need to rake leaves in backyard.',
price=90,
status=status.PENDING,
location='50 W 10th St',
longitude=round(geocode_result[0]['geometry']['location']['lng'], 6),
latitude=round(geocode_result[0]['geometry']['location']['lat'], 6),
zipcode=10011,
creator_id=user2.id,
date_created=datetime.now())
geocode_result = gmaps.geocode('77 Saint Marks Place' + ", " + str(10003))
job5 = Job(name='Build a swing set',
description='Need help building swing set.',
price=500,
status=status.PENDING,
location='77 Saint Marks Place',
longitude=round(geocode_result[0]['geometry']['location']['lng'], 6),
latitude=round(geocode_result[0]['geometry']['location']['lat'], 6),
zipcode=10003,
creator_id=user2.id,
date_created=datetime.now())
geocode_result = gmaps.geocode('778 Park Avenue' + ", " + str(10021))
job6 = Job(name='Painting',
description='Need to paint my walls bright pink.',
price=100,
status=status.PENDING,
location='778 Park Avenue',
longitude=round(geocode_result[0]['geometry']['location']['lng'], 6),
latitude=round(geocode_result[0]['geometry']['location']['lat'], 6),
zipcode=10021,
creator_id=user2.id,
date_created=datetime.now())
geocode_result = gmaps.geocode('419 West 115th Street' + ", " + str(10025))
job7 = Job(name='Moving Furniture',
description='Need help packing furniture onto moving truck.',
price=300,
status=status.PENDING,
location='419 West 115th Street',
longitude=round(geocode_result[0]['geometry']['location']['lng'], 6),
latitude=round(geocode_result[0]['geometry']['location']['lat'], 6),
zipcode=10025,
creator_id=user3.id,
date_created=datetime.now())
geocode_result = gmaps.geocode('441 East 9th Street' + ", " + str(10009))
job8 = Job(name='Demolition',
description='Need to tear down wall.',
price=200,
status=status.PENDING,
location='441 East 9th Street',
longitude=round(geocode_result[0]['geometry']['location']['lng'], 6),
latitude=round(geocode_result[0]['geometry']['location']['lat'], 6),
zipcode=10009,
creator_id=user3.id,
date_created=datetime.now())
geocode_result = gmaps.geocode('45 West 10th Street' + ", " + str(10011))
job9 = Job(name='Replacing wood flooring',
description='Need someone to replace wood flooring.',
price=1000,
status=status.PENDING,
location='45 West 10th Street',
longitude=round(geocode_result[0]['geometry']['location']['lng'], 6),
latitude=round(geocode_result[0]['geometry']['location']['lat'], 6),
zipcode=10011,
creator_id=user3.id,
date_created=datetime.now())
# Add jobs to database
try:
db.session.add(job1)
db.session.add(job2)
db.session.add(job3)
db.session.add(job4)
db.session.add(job5)
db.session.add(job6)
db.session.add(job7)
db.session.add(job8)
db.session.add(job9)
print("Added jobs! -- SUCCESS")
except Exception as e:
print("Error adding jobs! -- FAILED")
print("Error: %s" % e)
sys.exit(1)
# Create job_requestors
users = User.query.all()
jobs = Job.query.all()
for user in users:
for job in jobs:
x = random.randint(1, 101)
if x < 50 and user.id != job.creator_id:
job_request = JobRequestor(requestor_id=user.id,
job_id=job.id,
price=job.price)
db.session.add(job_request)
# Create hard coded jobs for demo
geocode_result = gmaps.geocode('185 Montague Street' + ", " + str(11201))
job10 = Job(name='Fix Heating',
description='I need someone who can fix my heater. It is broken and it is getting cold out!',
price=3945,
status=status.PENDING,
location='185 Montague Street',
longitude=round(geocode_result[0]['geometry']['location']['lng'], 6),
latitude=round(geocode_result[0]['geometry']['location']['lat'], 6),
zipcode=11201,
creator_id=user1.id,
date_created=datetime.now())
geocode_result = gmaps.geocode('100 Willoughby St' + ", " + str(11201))
job11 = Job(name='Redo Sidewalk',
description='The sidewalk is broken due to some construction going on. I would like someone to fix it '
'ASAP, price is negotiable.',
price=3945,
status=status.ACCEPTED,
location='100 Willoughby St',
longitude=round(geocode_result[0]['geometry']['location']['lng'], 6),
latitude=round(geocode_result[0]['geometry']['location']['lat'], 6),
zipcode=11201,
creator_id=user2.id,
date_created=datetime.now(),
accepted_id=user1.id,
date_accepted=datetime.now())
geocode_result = gmaps.geocode('49 Flatbush Ave Ext' + ", " + str(11201))
job12 = Job(name='Clean The Roof',
description='The roof has many leaves and garbage everywhere. I would like someone to clean it.',
price=70,
status=status.COMPLETED,
location='49 Flatbush Ave Ext',
longitude=round(geocode_result[0]['geometry']['location']['lng'], 6),
latitude=round(geocode_result[0]['geometry']['location']['lat'], 6),
zipcode=11201,
creator_id=user3.id,
date_created=datetime.now(),
accepted_id=user1.id,
rating=4,
review='Cleaned the roof to expectations but arrived late. Overall I would recommend.',
date_accepted=datetime.now(),
date_completed=datetime.now())
geocode_result = gmaps.geocode('240 Jay St' + ", " + str(11201))
job13 = Job(name='Repaint Walls',
description='I need someone to repaint the walls of my home. Paint will be provided.',
price=500,
status=status.COMPLETED,
location='240 Jay St',
longitude=round(geocode_result[0]['geometry']['location']['lng'], 6),
latitude=round(geocode_result[0]['geometry']['location']['lat'], 6),
zipcode=11201,
creator_id=user4.id,
date_created=datetime.now(),
accepted_id=user1.id,
rating=5,
review='Painted everything perfectly and super fast too! 11/10 would recommend.',
date_accepted=datetime.now(),
date_completed=datetime.now())
geocode_result = gmaps.geocode('287 Myrtle Ave' + ", " + str(11205))
job14 = Job(name='Building Demolition',
description='Building must be demolished. Licensed personnel only!',
price=10000,
status=status.COMPLETED,
location='287 Myrtle Ave',
longitude=round(geocode_result[0]['geometry']['location']['lng'], 6),
latitude=round(geocode_result[0]['geometry']['location']['lat'], 6),
zipcode=11205,
creator_id=user3.id,
date_created=datetime.now(),
accepted_id=user2.id,
rating=1,
review='Did a bad job, the building next door fell too.',
date_accepted=datetime.now(),
date_completed=datetime.now())
geocode_result = gmaps.geocode('29 Fort Greene Pl' + ", " + str(11217))
job15 = Job(name='Boiler Replacement',
description='The boiler must be replaced, the old one keeps breaking down.',
price=1500,
status=status.ACCEPTED,
location='29 Fort Greene Pl',
longitude=round(geocode_result[0]['geometry']['location']['lng'], 6),
latitude=round(geocode_result[0]['geometry']['location']['lat'], 6),
zipcode=11217,
creator_id=user3.id,
date_created=datetime.now(),
accepted_id=user4.id,
date_accepted=datetime.now())
geocode_result = gmaps.geocode('336 State St' + ", " + str(11217))
job16 = Job(name='Hang Photo',
description='Need a handy person to hang up a photo. Bring a drill please!',
price=30,
status=status.PENDING,
location='185 Montague Street',
longitude=round(geocode_result[0]['geometry']['location']['lng'], 6),
latitude=round(geocode_result[0]['geometry']['location']['lat'], 6),
zipcode=11217,
creator_id=user2.id,
date_created=datetime.now())
# Add hard coded jobs to database
try:
db.session.add(job10)
db.session.add(job11)
db.session.add(job12)
db.session.add(job13)
db.session.add(job14)
db.session.add(job15)
db.session.add(job16)
db.session.commit()
print("Added hard coded jobs! -- SUCCESS")
except Exception as e:
print("Error adding hard coded jobs! -- FAILED")
print("Error: %s" % e)
sys.exit(1)
if __name__ == '__main__':
manager.run()