Skip to content

Commit

Permalink
Added flake8 linter and modified code to pass without any errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
bdoms committed Feb 4, 2018
1 parent fc82e7f commit 36b3799
Show file tree
Hide file tree
Showing 13 changed files with 189 additions and 83 deletions.
49 changes: 25 additions & 24 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,31 @@
# URL routes
from controllers import admin, api, dev, error, home, index, job, sitemap, static, user

ROUTES = [('/', index.IndexController),
('/home', home.HomeController),
('/user', user.IndexController),
('/user/auths', user.AuthsController),
('/user/email', user.EmailController),
('/user/password', user.PasswordController),
('/user/signup', user.SignupController),
('/user/login', user.LoginController),
('/user/logout', user.LogoutController),
('/user/forgotpassword', user.ForgotPasswordController),
('/user/resetpassword', user.ResetPasswordController),
('/terms', static.StaticController),
('/privacy', static.StaticController),
('/sitemap.xml', sitemap.SitemapController),
('/admin', admin.AdminController),
('/api/upload', api.UploadController),
('/dev', dev.DevController),
('/job/auths', job.AuthsController),
('/job/email', job.EmailController),
#('/errors/(.*)', static.StaticController), # uncomment to test static error pages
('/logerror', error.LogErrorController),
('/policyviolation', error.PolicyViolationController),
('/(.*)', error.ErrorController)
]
ROUTES = [
('/', index.IndexController),
('/home', home.HomeController),
('/user', user.IndexController),
('/user/auths', user.AuthsController),
('/user/email', user.EmailController),
('/user/password', user.PasswordController),
('/user/signup', user.SignupController),
('/user/login', user.LoginController),
('/user/logout', user.LogoutController),
('/user/forgotpassword', user.ForgotPasswordController),
('/user/resetpassword', user.ResetPasswordController),
('/terms', static.StaticController),
('/privacy', static.StaticController),
('/sitemap.xml', sitemap.SitemapController),
('/admin', admin.AdminController),
('/api/upload', api.UploadController),
('/dev', dev.DevController),
('/job/auths', job.AuthsController),
('/job/email', job.EmailController),
# ('/errors/(.*)', static.StaticController), # uncomment to test static error pages
('/logerror', error.LogErrorController),
('/policyviolation', error.PolicyViolationController),
('/(.*)', error.ErrorController)
]

# any extra config needed when the app starts
cookie_args = {
Expand Down
23 changes: 13 additions & 10 deletions controllers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import json
import logging
import os
import urllib2

# app engine api imports
from google.appengine.api import app_identity, memcache, taskqueue, users
Expand All @@ -21,7 +20,7 @@
from config.constants import VIEWS_PATH, SUPPORT_EMAIL

# lib imports
from gae_html import cacheAndRender
from gae_html import cacheAndRender # NOQA: F401


class BaseController(webapp2.RequestHandler):
Expand Down Expand Up @@ -61,7 +60,7 @@ def dispatch(self):
# only run the regular action if there isn't already an error or redirect
if self.response.status_int == 200:
webapp2.RequestHandler.dispatch(self)

if hasattr(self, "after"):
try:
self.after(*self.request.route_args, **self.request.route_kwargs)
Expand Down Expand Up @@ -200,6 +199,7 @@ def user(self):
user = auth.user
else:
del self.session['auth_key']

return user

def deferEmail(self, to, subject, filename, reply_to=None, attachments=None, **kwargs):
Expand All @@ -209,7 +209,7 @@ def deferEmail(self, to, subject, filename, reply_to=None, attachments=None, **k
params['reply_to'] = reply_to

# this supports passing a template as well as a file
if type(filename) == type(""):
if isinstance(filename, basestring):
filename = "emails/" + filename
template = self.jinja_env.get_template(filename)

Expand Down Expand Up @@ -242,7 +242,7 @@ def validate(self):
form_data[name] = self.request.get(name)
except UnicodeDecodeError:
return self.renderError(400)

valid, data = validator(form_data[name])
if valid:
valid_data[name] = data
Expand All @@ -253,7 +253,7 @@ def validate(self):


def withUser(action):
def decorate(*args, **kwargs):
def decorate(*args, **kwargs):
controller = args[0]
if controller.user:
return action(*args, **kwargs)
Expand All @@ -264,7 +264,7 @@ def decorate(*args, **kwargs):


def withoutUser(action):
def decorate(*args, **kwargs):
def decorate(*args, **kwargs):
controller = args[0]
if not controller.user:
return action(*args, **kwargs)
Expand All @@ -275,7 +275,7 @@ def decorate(*args, **kwargs):


def removeSlash(action):
def decorate(*args, **kwargs):
def decorate(*args, **kwargs):
controller = args[0]
if controller.request.path.endswith("/"):
return controller.redirect(controller.request.path[:-1], permanent=True)
Expand All @@ -284,7 +284,7 @@ def decorate(*args, **kwargs):


def validateReferer(action):
def decorate(*args, **kwargs):
def decorate(*args, **kwargs):
controller = args[0]
referer = controller.request.headers.get("referer")
if not referer.startswith("http://" + controller.request.headers.get("host")):
Expand All @@ -300,9 +300,11 @@ def decorate(*args, **kwargs):
# (whereas the BaseController uses webapp2) and webapp 1 doesn't use the dispatch method
# which is why we have this custom second-level decorator defined to call it manually.
def testDispatch(action):

class MockApp(object):
debug = True
def decorate(*args, **kwargs):

def decorate(*args, **kwargs):
if helpers.testing():
controller = args[0]
# this will go into an infinite loop of dispatching itself if we don't stop it
Expand All @@ -314,4 +316,5 @@ def decorate(*args, **kwargs):
controller.dispatch()
else:
return action(*args, **kwargs)

return decorate
6 changes: 3 additions & 3 deletions controllers/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ def post(self):
modified = []

# do migration work
#q = model.User.query()
#for item in q:
# modified.append(item)
# q = model.User.query()
# for item in q:
# modified.append(item)

if modified:
model.ndb.put_multi(modified)
Expand Down
1 change: 1 addition & 0 deletions controllers/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class StaticPageError(Exception):
def __init__(self, reason):
self.message = "Static Error Page: " + reason


class PolicyViolationError(Exception):
def __init__(self, reason):
self.message = "Content Security Policy Violation: " + reason
3 changes: 2 additions & 1 deletion controllers/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from datetime import datetime, timedelta
import json
import logging
import urllib2

from google.appengine.api import mail

Expand Down Expand Up @@ -79,7 +80,7 @@ def post(self):
# an error here logs the status code but not the message
# which is way more helpful, so we get it manually
try:
response = self.SENDGRID.client.mail.send.post(request_body=message.get())
self.SENDGRID.client.mail.send.post(request_body=message.get())
except urllib2.HTTPError, e:
logging.error(e.read())
else:
Expand Down
28 changes: 15 additions & 13 deletions controllers/user.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timedelta
from datetime import datetime

from google.appengine.api import images
from google.appengine.ext import blobstore
Expand Down Expand Up @@ -63,7 +63,7 @@ def login(self, user, new=False, remember=False):
class IndexController(blobstore_handlers.BlobstoreUploadHandler, FormController):

# TODO: need a serving URL for non-image files and this should work in prod
#self.user.pic_url = 'https://' + self.gcs_bucket + '.storage.googleapis.com/' + rel_path
# self.user.pic_url = 'https://' + self.gcs_bucket + '.storage.googleapis.com/' + rel_path

# this is sometimes called by the blob service, so it won't include the CSRF
SKIP_CSRF = True
Expand Down Expand Up @@ -92,7 +92,7 @@ def post(self):
images.delete_serving_url(self.user.pic_blob)
if self.user.pic_blob:
blobstore.delete(self.user.pic_blob)

self.user.pic_gcs = None
self.user.pic_blob = None
self.user.pic_url = None
Expand All @@ -118,7 +118,7 @@ def post(self):

self.user.pic_gcs = upload.gs_object_name
self.user.pic_blob = upload.key()

if errors:
return self.redisplay({}, errors)

Expand All @@ -144,7 +144,9 @@ def post(self):

try:
auth_key = model.ndb.Key(urlsafe=str_key)
except:
except Exception:
# this is really a ProtocolBufferDecodeError, but we can't catch that
# see https://github.com/googlecloudplatform/datastore-ndb-python/issues/143
self.flash('error', 'Invalid session.')
else:
if auth_key.parent() != self.user.key:
Expand All @@ -168,7 +170,7 @@ def get(self):

@withUser
def post(self):

form_data, errors, valid_data = self.validate()

hashed_password = model.User.hashPassword(valid_data["password"], self.user.password_salt)
Expand Down Expand Up @@ -207,7 +209,7 @@ def get(self):

@withUser
def post(self):

form_data, errors, valid_data = self.validate()

hashed_password = model.User.hashPassword(valid_data["password"], self.user.password_salt)
Expand All @@ -220,7 +222,7 @@ def post(self):
self.redisplay(form_data, errors)
else:
password_salt, hashed_password = model.User.changePassword(valid_data["new_password"])

self.user.populate(password_salt=password_salt, hashed_password=hashed_password)
self.user.put()
self.uncache(self.user.slug)
Expand All @@ -245,7 +247,7 @@ def get(self):

@withoutUser
def post(self):

form_data, errors, valid_data = self.validate()

# extra validation to make sure that email address isn't already in use
Expand Down Expand Up @@ -278,7 +280,7 @@ def get(self):

@withoutUser
def post(self):

form_data, errors, valid_data = self.validate()

# check that the user exists and the password matches
Expand Down Expand Up @@ -308,7 +310,7 @@ def post(self):
str_key = self.session['auth_key']
try:
auth_key = model.ndb.Key(urlsafe=str_key)
except:
except Exception:
pass
else:
self.uncache(str_key)
Expand All @@ -328,7 +330,7 @@ def get(self):

@withoutUser
def post(self):

form_data, errors, valid_data = self.validate()

if errors:
Expand Down Expand Up @@ -372,7 +374,7 @@ def get(self):
self.renderTemplate('user/reset_password.html', key=self.key, token=self.token)

def post(self):

form_data, errors, valid_data = self.validate()

if errors:
Expand Down
Loading

0 comments on commit 36b3799

Please sign in to comment.