/
show_address_site.py
605 lines (458 loc) · 18.5 KB
/
show_address_site.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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
"""
This is the main file for starting up the entire address site.
It wraps everything and glues it all together to grab the proper
views, utilities, models, and static files. The application itself
runs wsgi via gunicorn and serves up the web with bottle. Authentication
is done through cork, templates through jinja, client side manipulation
through AngularJS, css is mostly bootstrap, and the backend via MongoDB.
The rest is custom code to bring it all together.
"""
from bottle import Bottle, hook, HTTPResponse, request, TEMPLATE_PATH
from bottle import jinja2_template as template, static_file, debug
from beaker.middleware import SessionMiddleware
from configobj import ConfigObj
from cork import Cork, AuthException, AAAException
from cork.backends import MongoDBBackend
from gunicorn.app.base import Application
from libraries.utils import JSONHelper, strToId, CSVHelper
from models.address import AddressModel
import logging
import os
import sys
logging.basicConfig(format='localhost - - [%(asctime)s] %(message)s',
level=logging.DEBUG)
log = logging.getLogger(__name__)
debug(True)
MODULEPATH = os.path.dirname(__file__)
TEMPLATE_PATH.append(os.path.join(MODULEPATH, "views"))
LOGIN_PATH = '/login'
CHANGE_PASSWORD_PATH = '/change_password'
VALIDATE_REGISTRATION_PATH = '/validate_registration'
class AddressServer(Application):
"""
This class is the gunicorn wrapper to serve up bottle.
Strongly borrowed from:
http://damianzaremba.co.uk/2012/08/running-a-wsgi-
app-via-gunicorn-from-python/
"""
def __init__(self, options={}):
self.usage = None
self.callable = None
self.prog = None
self.options = options
self.do_load_config()
self.setup_cork()
super(AddressServer, self).__init__()
self.app = Bottle()
self.add_routes()
self.add_middleware()
def setup_cork(self):
"""Set up cork using environment variables."""
EMAIL = os.environ.get('EMAIL_SENDER')
EMAIL_PASS = os.environ.get('EMAIL_PASSWORD')
self.MONGO_DB = os.environ.get('MONGOHQ_DB')
self.MONGO_URL = os.environ.get('MONGOHQ_URL')
mb = MongoDBBackend(self.MONGO_DB, self.MONGO_URL)
self.loginPlugin = Cork(backend=mb, email_sender=EMAIL,
smtp_url='starttls://' + EMAIL + ':' +
EMAIL_PASS + '@smtp.gmail.com:587')
def add_middleware(self):
"""Set up the session middleware."""
ENCRYPT_KEY = os.environ.get('ENCRYPT_KEY')
session_opts = {
'session.cookie_expires': True,
'session.encrypt_key': ENCRYPT_KEY,
'session.httponly': True,
'session.timeout': 3600 * 24, # 1 day
'session.type': 'cookie',
'session.validate_key': True,
}
self.app = SessionMiddleware(self.app, session_opts)
def add_routes(self):
"""Add all the application routes."""
self.app.route(LOGIN_PATH, 'GET', callback=get_login)
self.app.route(LOGIN_PATH, 'POST', callback=post_login,
apply=self.add_login_plugin)
self.app.route('/logout', 'GET', callback=logout,
apply=self.add_login_plugin)
self.app.route('/register', 'GET', callback=get_register,
apply=self.add_login_plugin)
self.app.route('/register', 'POST', callback=post_register,
apply=self.add_login_plugin)
self.app.route(VALIDATE_REGISTRATION_PATH + '/<registration_code>',
'GET', callback=validate_registration,
apply=self.add_login_plugin)
self.app.route(CHANGE_PASSWORD_PATH + '/<reset_code>', 'GET',
callback=get_change_password)
self.app.route(CHANGE_PASSWORD_PATH, 'POST',
callback=post_change_password,
apply=self.add_login_plugin)
self.app.route('/reset_password', 'GET', callback=get_reset_password)
self.app.route('/reset_password', 'POST', callback=post_reset_password,
apply=self.add_login_plugin)
self.app.route('/', 'GET', callback=index, apply=self.check_login)
self.app.route('/addresses', 'GET', callback=get_addresses,
apply=self.check_login)
self.app.route('/addresses', 'POST', callback=post_addresses,
apply=self.check_login)
self.app.route('/addresses', 'PUT', callback=put_addresses,
apply=self.check_login)
self.app.route('/addresses/<deleteId>', 'DELETE',
callback=delete_addresses, apply=self.check_login)
self.app.route('/csv', 'GET', callback=csv_export,
apply=self.check_login)
self.app.route('/christmas_card', 'GET',
callback=christmas_card_csv_export,
apply=self.check_login)
# self.app.route('/import_csv', 'GET', callback=csv_import,
# apply=self.check_login)
self.app.route('/js/<filename>', 'GET', callback=js_static)
self.app.route('/css/<filename>', 'GET', callback=css_static)
def init(self, *args):
"""Add any options passed in to the config.
:param *args: the arguments of the application
:returns: config object
:rtype: dict
"""
cfg = {}
for k, v in self.options.items():
if k.lower() in self.cfg.settings and v is not None:
cfg[k.lower()] = v
return cfg
def load(self):
"""Load and return the bottle app."""
return self.app
@hook('before_request')
def check_login(self, fn):
"""Hook for checking the login before doing logic.
:param fn: the function to call if the user is logged in
:returns: the wrapped function
:rtype: def
"""
def check_uid(**kwargs):
self.loginPlugin.require(fail_redirect=LOGIN_PATH)
kwargs["helper"] = AddressModel(self.MONGO_URL, self.MONGO_DB)
kwargs["userName"] = self.loginPlugin.current_user.username
return fn(**kwargs)
return check_uid
@hook('before_request')
def add_login_plugin(self, fn):
"""Hook for adding the plugin information.
:param fn: the function to pass the plugin
:returns: the wrapped function
:rtype: def
"""
def add_plugin(**kwargs):
kwargs["loginPlugin"] = self.loginPlugin
return fn(**kwargs)
return add_plugin
def post_get(name, default=''):
"""Get posted information.
:param name: the key of the posted information
:type name: str
:param default: the default value if not found
:type default: str
:returns: the value of the posted information
:rtype: str
"""
return request.POST.get(name, default).strip()
def get_login():
"""The login page.
:returns: the html of the login page
:rtype: str
"""
return template('login.html')
def post_login(loginPlugin):
"""Handle the login page's post information.
If successful, it redirects to the home page, but if it fails
it goes back to the login page.
:param loginPlugin: the login plugin to authenticate against
:type loginPlugin: object
:returns: (nothing)
"""
errMessage = ''
success = False
try:
username = post_get('username')
password = post_get('password')
success = loginPlugin.login(username, password, success_redirect='/')
if not success:
errMessage = 'User name and/or password were incorrect.'
# redirects throw an exception, so ignore
except HTTPResponse:
raise
except Exception:
errMessage = 'Login had an unknown error.'
log.exception(errMessage)
return template('login.html', errMessage=errMessage)
def logout(loginPlugin):
"""Log out the current user and redirect to the login page.
:param loginPlugin: the login plugin to log out
:type loginPlugin: object
:returns: (nothing)
"""
loginPlugin.logout(success_redirect=LOGIN_PATH)
def get_register(loginPlugin):
"""Register a new user based on post information.
:param loginPlugin: the login plugin to use to create a user
:type loginPlugin: object
:returns: the html after registering
:rtype: str
"""
return template('registration.html')
def post_register(loginPlugin):
"""Register a new user based on post information.
:param loginPlugin: the login plugin to use to create a user
:type loginPlugin: object
:returns: the html after registering
:rtype: str
"""
errMessage = ''
success = False
try:
loginPlugin.register(post_get('username'), post_get('password'),
post_get('email_address'))
success = True
except AssertionError:
errMessage = 'You must fill out user name, password,'
errMessage += ' and email address to register.'
log.exception(errMessage)
except Exception:
errMessage = 'An unknown error occurred.'
log.exception(errMessage)
return template('registration.html', success=success,
errMessage=errMessage)
def validate_registration(loginPlugin, registration_code):
"""Validate a registration so a new user can log in.
Once registered users receive an email, they will click on a link
to validate before logging in.
:param loginPlugin: the login plugin to validate against
:type loginPlugin: object
:param registration_code: the registration code from the email
:type registration_code: str
:returns: the html after validating
:rtype: str
"""
errMessage = ''
success = False
try:
loginPlugin.validate_registration(registration_code)
success = True
except AuthException:
errMessage = 'This is not a proper registration code.'
log.exception(errMessage)
except Exception:
errMessage = 'An unknown error occurred.'
log.exception(errMessage)
return template('validate_registration.html', success=success,
errMessage=errMessage)
def get_reset_password():
"""Get the user name and email address to reset the password.
:returns: the html form for resetting password
:rtype: str
"""
return template('get_reset_code.html')
def post_reset_password(loginPlugin):
"""Send out the password reset email.
If the email fails, this page will display an error message.
:param loginPlugin: the login plugin to user to reset the password
:type loginPlugin: object
:returns: the html after attempting to reset the password
:rtype: str
"""
errMessage = ''
success = False
try:
userName = post_get('username')
emailAddress = post_get('email_address')
loginPlugin.send_password_reset_email(username=userName,
email_addr=emailAddress)
success = True
except AuthException:
errMessage = 'Your username or email address is invalid.'
log.exception(errMessage)
except AAAException:
errMessage = 'Your username is invalid.'
log.exception(errMessage)
except Exception:
errMessage = 'An unknown error occurred.'
log.exception(errMessage)
return template('get_reset_code.html', success=success,
errMessage=errMessage)
def get_change_password(reset_code):
"""The change password page.
:param reset_code: the reset code to get a changed password
:type reset_code: str
:returns: the html after attempting to change the password
:rtype: str
"""
return template('password_reset_form.html', reset_code=reset_code)
def post_change_password(loginPlugin):
"""Reset the password of a user.
:param loginPlugin: the login plugin to user to change the password
:type loginPlugin: object
:returns: the html after resetting the password
:rtype: str
"""
errMessage = ''
success = False
try:
reset_code = post_get('reset_code')
password = post_get('password')
loginPlugin.reset_password(reset_code, password)
success = True
except AuthException as aue:
errMessage = str(aue)
log.exception(errMessage)
except AAAException as aae:
errMessage = str(aae)
log.exception(errMessage)
except Exception:
errMessage = 'An unknown error occurred.'
log.exception(errMessage)
return template('reset_password.html', success=success,
errMessage=errMessage)
def index(helper, userName):
"""The home page.
:param helper: the helper object to operate on the databaes
:type helper: DataObject
:param userName: the user name of the currently logged in user
:type userName: str
:returns: the html of the home page
:rtype: str
"""
address_fields = helper.getCreationFields()
return template('home.html', address_fields=address_fields)
def get_addresses(helper, userName):
"""The JSON of all addresses for the given user.
:param helper: the helper object to operate on the databaes
:type helper: DataObject
:param userName: the user name of the currently logged in user
:type userName: str
:returns: JSON data of all the addresses
:rtype: str
"""
addresses = helper.getMultiple(userName=userName)
jsonAddresses = JSONHelper().encode(addresses)
return HTTPResponse(jsonAddresses, status=200,
header={'Content-Type': 'application/json'})
def post_addresses(helper, userName):
"""Create a new address.
:param helper: the helper object to operate on the databaes
:type helper: DataObject
:param userName: the user name of the currently logged in user
:type userName: str
:returns: JSON data indicating success or not
:rtype: str
"""
throwAway, newAddress = JSONHelper().decode(request.body.read())
postId = helper.create(newAddress, userName=userName)
if postId != -1:
return HTTPResponse(JSONHelper().encode({'_id': postId}), status=200,
header={'Content-Type': 'application/json'})
else:
return return_error(400, "Create did not work.")
def put_addresses(helper, userName):
"""Save changes to an address.
:param helper: the helper object to operate on the databaes
:type helper: DataObject
:param userName: the user name of the currently logged in user
:type userName: str
:returns: JSON data indicating success or not
:rtype: str
"""
ids, decodeds = JSONHelper().decode(request.body.read())
if helper.updateMultiple(ids, decodeds, userName=userName):
return HTTPResponse(status=200)
else:
return return_error(400, "Update did not work.")
def delete_addresses(deleteId, helper, userName):
"""Delete an address.
:param deleteId: the id of the address to delete
:type deleteId: str
:param helper: the helper object to operate on the databaes
:type helper: DataObject
:param userName: the user name of the currently logged in user
:type userName: str
:returns: JSON data indicating success or not
:rtype: str
"""
if helper.delete(strToId(deleteId), userName=userName):
return HTTPResponse(status=200)
else:
return return_error(400, "Delete did not work")
def csv_export(helper, userName):
"""Export all addresses in the database as a csv file.
:param helper: the helper object to operate on the databaes
:type helper: DataObject
:param userName: the user name of the currently logged in user
:type userName: str
:returns: HTTP response with the csv file download
:rtype: HTTPResponse
"""
addresses = helper.getMultiple(userName=userName)
csvAddresses = CSVHelper().convertToCSV(addresses,
helper.getCreationFields())
disposition = "attachment;filename=addresses.csv"
return HTTPResponse(body=csvAddresses, status=200,
headers={'Content-Type': 'text/csv',
'Content-disposition': disposition})
def christmas_card_csv_export(helper, userName):
"""Export Christmas-approved addresses with applicable fields as a csv.
:param helper: the helper object to operate on the databaes
:type helper: DataObject
:param userName: the user name of the currently logged in user
:type userName: str
:returns: HTTP response with the csv file download
:rtype: HTTPResponse
"""
addresses = helper.getMultiple(userName, {'send_christmas_card': True})
csvAddresses = CSVHelper().convertToCSV(addresses,
helper.getChristmasFields())
disposition = "attachment;filename=christmas_card.csv"
return HTTPResponse(csvAddresses, status=200,
headers={'Content-Type': 'text/csv',
'Content-disposition': disposition})
#This is really for testing purposes and needs updated to use for real.
#def csv_import(helper, userName):
# addresses = CSVHelper().convertFromCSV('contacts.csv')
# atleastOneFailed = False
# for address in addresses:
# if helper.create(address, userName=userName) == -1:
# atleastOneFailed = True
# if not atleastOneFailed:
# return HTTPResponse(status=200)
# else:
# return return_error(400, "Import did not work.")
def js_static(filename):
"""Get static javascript files.
:param filename: the name of the javascript file
:type filename: str
:returns: The given js file
:rtype: file
"""
return static_file(filename, root=os.path.join(MODULEPATH, 'static/js'))
def css_static(filename):
"""Get static css files.
:param filename: the name of the css file
:type filename: str
:returns: The given css file
:rtype: file
"""
return static_file(filename, root=os.path.join(MODULEPATH, 'static/css'))
def return_error(status, msg=''):
"""Return a JSON error message.
:param status: the response code
:type status: int
:param msg: the message to return to the user
:type msg: str
:returns: The JSON data of the error
:rtype: str
"""
json_err = JSONHelper().encode({'error': msg})
return HTTPResponse(json_err, status=status,
header={'Content-Type': 'application/json'})
if __name__ == '__main__':
options = ConfigObj(os.environ["CONFIGFILE"])["web server"]
options['bind'] = options['bind'] + ':' + os.environ["PORT"]
AddressServer(options).run()