/
main.py
executable file
·390 lines (311 loc) · 14 KB
/
main.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
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import os
import urllib
import webapp2
import logging
from webapp2_extras import auth
from webapp2_extras import sessions
from google.appengine.ext.webapp import template
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
import models
import utils
def insert_details(context):
try:
current_session = auth.get_auth().get_user_by_session()
new_user_object = auth.get_auth().store.user_model.get_by_auth_token(current_session['user_id'], current_session['token'])[0]
username = new_user_object.auth_ids[0]
old_user_object = models.Account.all().filter('username =', username).fetch(1)[0].__dict__['_entity']
context['username'] = old_user_object['username']
context['is_admin'] = old_user_object['is_admin']
context['is_employee'] = old_user_object['is_employee']
except Exception, e:
logging.warning(e)
# Returns error message to self.response.write in the BaseHandler.dispatcher
# Currently no message is attached to the exceptions
def user_required(handler):
"""
Decorator for checking if there's a user associated with the current session.
Will also fail if there's no session present.
"""
def check_login(self, *args, **kwargs):
auth = self.auth
if not auth.get_user_by_session():
# If handler has no login_url specified invoke a 403 error
try:
self.redirect(self.auth_config['login_url'], abort=True)
except (AttributeError, KeyError), e:
self.abort(403)
else:
return handler(self, *args, **kwargs)
return check_login
class BaseHandler(webapp2.RequestHandler):
"""
BaseHandler for all requests
Holds the auth and session properties so they are reachable for all requests
"""
def dispatch(self):
"""
Save the sessions for preservation across requests
"""
try:
response = super(BaseHandler, self).dispatch()
finally:
self.session_store.save_sessions(self.response)
@webapp2.cached_property
def auth(self):
return auth.get_auth()
@webapp2.cached_property
def session_store(self):
return sessions.get_store(request=self.request)
@webapp2.cached_property
def auth_config(self):
"""
Dict to hold urls for login/logout
"""
return {
'login_url': self.uri_for('login'),
'logout_url': self.uri_for('logout')
}
class MainHandler(webapp2.RequestHandler):
def get(self):
#mike_exists = models.Account.all().filter('username =', 'mike')
#if not mike_exists.count():
# account = models.Account(username="mike", password="test", given_name="Michael", is_admin=True, is_employee=True, ssn='999999999')
# account.save()
context = utils.get_context(self.request)
insert_details(context)
#if 'user' in self.request.GET:
# new_user_object = self.auth.store.user_model.get_by_auth_id(self.request.GET['user'])
# old_user_object = models.Account.all().filter('username =', self.request.GET['user']).fetch(1)[0].__dict__['_entity']
# username = new_user_object.auth_ids[0]
path = os.path.join(os.path.dirname(__file__), 'templates/home.html')
self.response.out.write(template.render(path, context))
# This class is used to easily change employee information so that teams have
# unique employee information. You are more than welcome to delete this class.
# This is the only functionality you are allowed to remove. -- White Team
#class edit_employee(webapp2.RequestHandler):
# def get(self):
# username = self.request.GET['username']
# ssn = self.request.GET['ssn']
#
# account = models.Account.all().filter('username =', username).fetch(1)
# account = account[0]
# account.ssn = ssn
# account.save()
class view_directory(BaseHandler):
def get(self):
context = utils.get_context(self.request)
insert_details(context)
if context['is_admin']:
employee_query = models.Account.all().filter('is_employee =', True)#.filter('username !=', 'mike')
employees = employee_query.fetch(1000)
context['employees'] = employees
path = os.path.join(os.path.dirname(__file__),
'templates/directory.html')
self.response.out.write(template.render(path, context))
else:
path = os.path.join(os.path.dirname(__file__),
'templates/error_no_permission.html')
self.response.out.write(template.render(path, context))
def post(self):
keys = {x : self.request.POST[x] for x in self.request.POST}
employee_key = self.request.get('edit_employee', None)
if employee_key:
account = models.Account.get(str(employee_key))
if 'edit_given_name' in keys:
account.given_name = keys['edit_given_name']
if 'edit_ssn' in keys:
account.ssn = keys['edit_ssn']
if 'edit_username' in keys:
account.username = keys['edit_username']
if 'edit_password' in keys:
account.password = keys['edit_password']
account.save()
self.redirect('/directory')
#else:
#
# newbie = models.Account(given_name=keys['given_name'],
# ssn=keys['ssn'],
# username=keys['username'],
# password=keys['password'],
# is_employee=True,
# is_admin=True)
#
# newbie.save()
#
# user = self.auth.store.user_model.create_user(keys['username'], password_raw=keys['password'])
self.redirect('/directory')
class edit_profile(BaseHandler):
def get(self):
context = utils.get_context(self.request)
insert_details(context)
if context['is_employee']:
employee = models.Account.all().filter('username =', context['username'])[0]
context['employee'] = employee
path = os.path.join(os.path.dirname(__file__), 'templates/profile.html')
self.response.out.write(template.render(path, context))
def post(self):
keys = {x : self.request.POST[x] for x in self.request.POST}
username = self.request.get('username', None)
if username:
account = models.Account.all().filter("username =", username)[0]
if 'given_name' in keys:
account.given_name = keys['given_name']
if 'ssn' in keys:
account.ssn = keys['ssn']
if 'username' in keys:
account.username = keys['username']
if 'password' in keys:
account.password = keys['password']
account.save()
self.redirect('/profile')
class delete_employee(webapp2.RequestHandler):
def get(self, employee_ssn):
ssn = str(urllib.unquote(employee_ssn))
fired = models.Account.all().filter('ssn =', ssn).fetch(1)[0]
fired.delete()
self.redirect('/directory')
class view_customers(webapp2.RequestHandler):
def get(self):
context = utils.get_context(self.request)
insert_details(context)
if context['is_admin']:
customer_query = models.Account.all().filter('is_customer =', True)
customers = customer_query.fetch(1000)
context['customers'] = customers
path = os.path.join(os.path.dirname(__file__),
'templates/customers.html')
self.response.out.write(template.render(path, context))
else:
path = os.path.join(os.path.dirname(__file__),
'templates/error_no_permission.html')
self.response.out.write(template.render(path, context))
class apply(webapp2.RequestHandler):
def get(self):
context = utils.get_context(self.request)
insert_details(context)
upload_url = blobstore.create_upload_url('/upload')
upload_url = upload_url.replace('http://localhost:8080', self.request.get('host'))
context['upload_url'] = upload_url
path = os.path.join(os.path.dirname(__file__), 'templates/apply.html')
self.response.out.write(template.render(path, context))
class resume_upload(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
upload_files = self.get_uploads('file')
blob_info = upload_files[0]
given_name = self.request.POST.get('given_name', None)
surname = self.request.POST.get('surname', None)
resume = models.Resume(blob=str(blob_info.key()),
given_name=given_name,
surname=surname)
resume.save()
self.redirect('/thanks')
class thanks(webapp2.RequestHandler):
def get(self):
context = utils.get_context(self.request)
insert_details(context)
path = os.path.join(os.path.dirname(__file__), 'templates/thanks.html')
self.response.out.write(template.render(path, context))
class resume_download(blobstore_handlers.BlobstoreDownloadHandler):
def get(self, blob_key):
blob_key = str(urllib.unquote(blob_key))
if not blobstore.get(blob_key):
self.error(404)
else:
self.send_blob(blobstore.BlobInfo.get(blob_key), save_as=True)
class resume_delete(webapp2.RequestHandler):
def get(self, blob_key):
key = str(urllib.unquote(blob_key))
remove = models.Resume.all().filter('blob =', key).fetch(1)[0]
if remove:
remove.delete()
self.redirect('/resumes')
class view_resumes(webapp2.RequestHandler):
def get(self):
context = utils.get_context(self.request)
insert_details(context)
resumes = models.Resume.all().fetch(10000)
context['resumes'] = resumes
path = os.path.join(os.path.dirname(__file__),
'templates/view_resumes.html')
self.response.out.write(template.render(path, context))
class login(BaseHandler):
def post(self):
username = self.request.POST.get('username', None)
password = self.request.POST.get('password', None)
try:
auth.get_auth().get_user_by_password(username, password)
except Exception, e:
logging.warning(e)
# Returns error message to self.response.write in the BaseHandler.dispatcher
# Currently no message is attached to the exceptions
if username:
try:
auth.get_auth().get_user_by_password(username, password)
except Exception, e:
logging.warning(e)
self.redirect('/')
class logout(webapp2.RequestHandler):
def post(self):
auth.get_auth().unset_session()
self.redirect('/')
# I am testing a new authentication system that should be better than the
# one I have now. I have it mostly set up, but I need to either figure out
# how to store user info on the new user model, or some other way to securely
# handle user authentication. Remove this later. -- Michael
class test(BaseHandler):
# @user_required
def get(self):
context = utils.get_context(self.request)
insert_details(context)
#
current_session = auth.get_auth().get_user_by_session()
new_user_object = self.auth.store.user_model.get_by_auth_token(current_session['user_id'], current_session['token'])[0]
username = new_user_object.auth_ids[0]
old_user_object = models.Account.all().filter('username =', username).fetch(1)[0].__dict__['_entity']
if 'user' in self.request.GET:
new_user_object = self.auth.store.user_model.get_by_auth_id(self.request.GET['user'])
old_user_object = models.Account.all().filter('username =', self.request.GET['user']).fetch(1)[0].__dict__['_entity']
username = new_user_object.auth_ids[0]
context['current_session'] = current_session
context['new_user_object'] = new_user_object
context['user'] = username
context['old_user_object'] = old_user_object
path = os.path.join(os.path.dirname(__file__),
'templates/auth_test.html')
self.response.out.write(template.render(path, context))
config = {}
config['webapp2_extras.sessions'] = {
'secret_key': 'some-secret-key',
}
app = webapp2.WSGIApplication([('/', MainHandler),
('/directory', view_directory),
('/apply', apply),
('/resumes', view_resumes),
('/resumes/([^/]+)?', resume_download),
('/resume/delete/([^/]+)?', resume_delete),
('/upload', resume_upload),
('/login', login),
('/logout', logout),
('/test', test),
#('/edit', edit_employee),
('/thanks', thanks),
('/profile', edit_profile),
('/delete_employee/([^/]+)?', delete_employee)],
debug=True, config=config)