Download:
child 39:81920cb8753b
parent 37:bbce1e45da1e
38:4326af080387
Anton Shestakov <av6@dwimlabs.net>, Sat, 18 Jun 2016 22:30:42 +0800
viewer: get_or_404() to raise 404 (instead of 500) when model doesn't exist

1 файлов изменено, 21 вставок(+), 10 удалений(-) [+]
viewer.py file | annotate | diff | comparison | revisions
--- a/viewer.py Sat Jun 18 18:56:40 2016 +0800
+++ b/viewer.py Sat Jun 18 22:30:42 2016 +0800
@@ -4,10 +4,11 @@
import logging
import os
+from peewee import DoesNotExist
from tornado.escape import json_decode
from tornado.ioloop import IOLoop
from tornado.options import define, options
-from tornado.web import Application, RequestHandler, URLSpec
+from tornado.web import Application, HTTPError, RequestHandler, URLSpec
from candolint import uimodules
from candolint.models import database, Project, Change, Check
@@ -22,6 +23,13 @@
define('debug', metavar='True|False', default=False, type=bool)
+def get_or_404(query, *args, **kwargs):
+ try:
+ return query.get(*args, **kwargs)
+ except DoesNotExist:
+ raise HTTPError(404)
+
+
class BaseHandler(RequestHandler):
def prepare(self):
database.connect()
@@ -45,10 +53,11 @@
class ProjectHandler(BaseHandler):
def get(self, domain, user, name):
- project = Project.select().where(
+ pq = Project.select().where(
Project.domain == domain,
Project.user == (user if user != '-' else None),
- Project.name == name).get()
+ Project.name == name)
+ project = get_or_404(pq)
checks = (Check
.select(Check, Change)
.join(Change)
@@ -59,25 +68,27 @@
class CheckHandler(BaseHandler):
def get(self, domain, user, name, check_num):
- project = Project.select().where(
+ pq = Project.select().where(
Project.domain == domain,
Project.user == (user if user != '-' else None),
- Project.name == name).get()
+ Project.name == name)
+ project = get_or_404(pq)
if check_num == 'latest':
- check = Check.get(project=project)
+ check = get_or_404(Check, project=project)
else:
- check = Check.get(project=project, ordinal=check_num)
+ check = get_or_404(Check, project=project, ordinal=check_num)
lines = json_decode(check.lines)
self.render('check.html', project=project, check=check, lines=lines)
class StatusHandler(BaseHandler):
def get(self, domain, user, name):
- project = Project.select().where(
+ pq = Project.select().where(
Project.domain == domain,
Project.user == (user if user != '-' else None),
- Project.name == name).get()
- check = Check.get(project=project)
+ Project.name == name)
+ project = get_or_404(pq)
+ check = get_or_404(Check, project=project)
parts = [('#555', 30, 14.5, 'lint')]
if not check.success:
parts.append(('#777', 62, 30.5, 'unknown'))