Skip to content

Commit

Permalink
Made controller actions async.
Browse files Browse the repository at this point in the history
  • Loading branch information
bdoms committed Mar 23, 2020
1 parent 68c790d commit 7f7cf6e
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 54 deletions.
2 changes: 1 addition & 1 deletion controllers/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def renderJSON(self, data):
self.write(data)
self.saveSession()

def head(self, *args):
async def head(self, *args):
# support HEAD requests in a generic way
if hasattr(self, 'get'):
self.get(*args)
Expand Down
2 changes: 1 addition & 1 deletion controllers/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ def before(self):
if not self.current_user.is_admin:
return self.renderError(403)

def get(self):
async def get(self):

self.renderTemplate('admin/index.html')
2 changes: 1 addition & 1 deletion controllers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class UploadController(BaseController):

def post(self):
async def post(self):

# TODO: update this to provide a signed URL for uploading from the service of your choice
url = ''
Expand Down
6 changes: 3 additions & 3 deletions controllers/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
class SvelteController(BaseController):

@web.authenticated
def get(self):
async def get(self):

self.renderTemplate('svelte.html')
self.renderTemplate('svelte.html', host=self.host)


class VueController(BaseController):

@web.authenticated
def get(self):
async def get(self):

self.renderTemplate('vue.html')
6 changes: 3 additions & 3 deletions controllers/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ def before(self):
if (not self.current_user or not self.current_user.is_dev) and not self.debug:
return self.renderError(403)

def get(self):
async def get(self):
self.renderTemplate('dev.html')

def post(self):
async def post(self):

if self.get_argument('clear_cache', None):
helpers.clear_cache()
Expand Down Expand Up @@ -64,7 +64,7 @@ def post(self):
user.save()

# auto signout since the IDs and keys have all changed
self.clear_all_cookies()
self.clear_all_cookies(domain=self.host)
helpers.clear_cache()
self.flash('Data Reset')

Expand Down
6 changes: 3 additions & 3 deletions controllers/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ErrorController(BaseController):
""" handles any page that falls through the rest of config.ROUTES """

@helpers.cacheAndRender()
def get(self, *args):
async def get(self, *args):

self.renderError(404)

Expand All @@ -19,7 +19,7 @@ class LogErrorController(BaseController):
def check_xsrf_cookie(self):
pass

def post(self):
async def post(self):
reason = self.get_argument('javascript', '')
exception = JavascriptError(reason)

Expand All @@ -39,7 +39,7 @@ class PolicyViolationController(BaseController):
def check_xsrf_cookie(self):
pass

def post(self):
async def post(self):
exception = PolicyViolationError(self.request.body.decode('utf8'))

self.logger.error(exception.message)
Expand Down
4 changes: 2 additions & 2 deletions controllers/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
class HomeController(BaseController):

@web.authenticated
def get(self):
async def get(self):

self.renderTemplate('home.html')

@web.authenticated
def post(self):
async def post(self):

self.renderJSON({'user': self.current_user.toDict()})
2 changes: 1 addition & 1 deletion controllers/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ class IndexController(BaseController):
""" handles request for the main index page of the site """

@helpers.cacheAndRender()
def get(self):
async def get(self):

self.renderTemplate('index.html')
2 changes: 1 addition & 1 deletion controllers/sitemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class SitemapController(BaseController):
""" handles generating a sitemap """

@helpers.cacheAndRender()
def get(self):
async def get(self):
# FYI: sitemaps can only have a max of 50,000 URLs or be 10 MB each
base_url = self.request.protocol + "://" + self.request.host

Expand Down
10 changes: 9 additions & 1 deletion controllers/static.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from tornado import web

from controllers._base import BaseController
import helpers

Expand All @@ -6,10 +8,16 @@ class StaticController(BaseController):
""" handles any page that doesn't need to render with custom variables """

@helpers.cacheAndRender()
def get(self, *args):
async def get(self, *args):

path = self.request.path
filename = "static/" + path + ".html"
page_title = path.replace("/", " ").strip().title()

self.renderTemplate(filename, page_title=page_title)


class StaticFileController(web.StaticFileHandler):

def set_default_headers(self):
self.set_header('X-Content-Type-Options', 'nosniff')
30 changes: 15 additions & 15 deletions controllers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def login(self, user, new=False, remember=False):
class IndexController(BaseController):

@web.authenticated
def get(self):
async def get(self):

self.renderTemplate('user/index.html')

Expand Down Expand Up @@ -91,7 +91,7 @@ class AuthsController(BaseController):
FIELDS = {'auth_key': validateRequiredString}

@web.authenticated
def get(self):
async def get(self):

auths = self.current_user.auths
current_auth_key = self.get_secure_cookie('auth_key').decode()
Expand All @@ -102,7 +102,7 @@ def get(self):
self.renderTemplate('user/auths.html', auths=auths, current_auth_key=current_auth_key)

@web.authenticated
def post(self):
async def post(self):

app = self.get_argument('app', None)
form_data, errors, valid_data = self.validate()
Expand Down Expand Up @@ -136,12 +136,12 @@ class EmailController(BaseController):
FIELDS = {"email": validateRequiredEmail, "password": validateRequiredString}

@web.authenticated
def get(self):
async def get(self):

self.renderTemplate('user/email.html')

@web.authenticated
def post(self):
async def post(self):

app = self.get_argument('app', None)
form_data, errors, valid_data = self.validate()
Expand Down Expand Up @@ -186,12 +186,12 @@ class PasswordController(BaseController):
FIELDS = {"password": validateRequiredString, "new_password": validateRequiredString}

@web.authenticated
def get(self):
async def get(self):

self.renderTemplate('user/password.html')

@web.authenticated
def post(self):
async def post(self):

app = self.get_argument('app', None)
form_data, errors, valid_data = self.validate()
Expand Down Expand Up @@ -236,12 +236,12 @@ class SignupController(BaseLoginController):
}

@withoutUser
def get(self):
async def get(self):

self.renderTemplate('user/signup.html')

@withoutUser
def post(self):
async def post(self):

form_data, errors, valid_data = self.validate()

Expand Down Expand Up @@ -271,12 +271,12 @@ class LoginController(BaseLoginController):
FIELDS = {"email": validateRequiredEmail, "password": validateRequiredString, "remember": validateBool}

@withoutUser
def get(self):
async def get(self):

self.renderTemplate('user/login.html')

@withoutUser
def post(self):
async def post(self):

form_data, errors, valid_data = self.validate()

Expand Down Expand Up @@ -318,12 +318,12 @@ class ForgotPasswordController(BaseController):
FIELDS = {"email": validateRequiredEmail}

@withoutUser
def get(self):
async def get(self):

self.renderTemplate('user/forgot_password.html')

@withoutUser
def post(self):
async def post(self):

form_data, errors, valid_data = self.validate()

Expand Down Expand Up @@ -368,11 +368,11 @@ def before(self):
self.flash("That reset password link has expired.", level="error")
self.redirect("/user/forgotpassword")

def get(self):
async def get(self):

self.renderTemplate('user/reset_password.html', key=self.key, token=self.token)

def post(self):
async def post(self):

form_data, errors, valid_data = self.validate()

Expand Down
16 changes: 12 additions & 4 deletions helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def int_comma(i):
def cacheAndRender(skip_check=None, content_type=None):

def wrap_action(action):
def decorate(*args, **kwargs):
async def decorate(*args, **kwargs):
controller = args[0]

# if we're checking for errors and they're present, then return early to avoid caching
Expand All @@ -114,7 +114,7 @@ def decorate(*args, **kwargs):
controller.set_header('Content-Type', content_type)
controller.write(html)
else:
action(*args, **kwargs)
await action(*args, **kwargs)
html = b"".join(controller._write_buffer).decode()

html = htmlmin.minify(html, remove_comments=True, remove_empty_space=True)
Expand All @@ -127,6 +127,9 @@ def decorate(*args, **kwargs):


def cache(key, function):
# make the key memcache compatible
key = key.replace(' ', '_')[:250]

# simple in memory LRU cache, most recently used key is moved to the end, least is at front
if key in CACHE:
# move the key to the end since it was just used
Expand All @@ -139,12 +142,17 @@ def cache(key, function):
del CACHE[remove_key]

value = function()
CACHE_KEYS.append(key)
CACHE[key] = value
if key not in CACHE:
CACHE[key] = value
CACHE_KEYS.append(key)

return value


def uncache(key):
# make the key memcache compatible
key = key.replace(' ', '_')[:250]

if key in CACHE:
del CACHE[key]
CACHE_KEYS.remove(key)
Expand Down
Loading

0 comments on commit 7f7cf6e

Please sign in to comment.