Anton Shestakov <av6@dwimlabs.net>, Sun, 17 Jul 2016 05:10:35 +0800
utils: a small module for shared functions, the first of them is rel()
candolint/handlers.py
Permissions: -rw-r--r--
from __future__ import absolute_import, division from peewee import DoesNotExist from tornado.escape import json_decode from tornado.web import HTTPError, RequestHandler from tornado.web import ErrorHandler as BaseErrorHandler from candolint.models import database, Project, Change, Check def get_or_404(query, *args, **kwargs): return query.get(*args, **kwargs) def get_project_or_404(domain, user, name): pq = Project.select().where( Project.domain == domain, Project.user == (user if user != '-' else None), class BaseHandler(RequestHandler): super(BaseHandler, self).prepare() if not database.is_closed(): def write_error(self, status_code, **kwargs): if self.settings.get('serve_traceback'): fexc = traceback.format_exception(*kwargs['exc_info']) data['debug_message'] = '\n'.join(fexc) self.render('404.html', **data) self.render('500.html', **data) class IndexHandler(BaseHandler): .select(Project, Change, *Check.get_light_fields()) .group_by(Check.project)) self.render('index.html', checks=checks) class ProjectHandler(BaseHandler): def get(self, domain, user, name): project = get_project_or_404(domain, user, name) .select(Change, *Check.get_light_fields()) .where(Check.project == project) self.render('project.html', project=project, checks=checks) class CheckHandler(BaseHandler): def get(self, domain, user, name, check_num, format_='html'): project = get_project_or_404(domain, user, name) .where(Check.project == project)) if check_num != 'latest': check_query = check_query.where(Check.ordinal == check_num) check = get_or_404(check_query) lines = json_decode(check.lines) self.get_raw(project, check, lines) self.get_html(project, check, lines) def get_raw(self, project, check, lines): self.set_header('Content-Type', 'text/plain; charset=utf-8') def get_html(self, project, check, lines): adapter = project.get_adapter() line['link'] = adapter.get_line_url(check.change, line) self.render('check.html', project=project, check=check, lines=lines, adapter=adapter) class StatusHandler(BaseHandler): def get(self, domain, user, name): project = get_project_or_404(domain, user, name) check = get_or_404(Check, project=project) parts = [('#555', 30, 14.5, 'lint')] parts.append(('#777', 62, 30.5, 'unknown')) elif check.errors or check.warnings: msg = self.locale.translate('{} error', '{} errors', check.errors) text = msg.format(check.errors) width = 7 + 6 * len(text) + 7 parts.append(('#da314b', width, width // 2 - 0.5, text)) msg = self.locale.translate('{} warning', '{} warnings', check.warnings) text = msg.format(check.warnings) width = 7 + 6 * len(text) + 9 parts.append(('#faa732', width, width // 2 + 0.5, text)) parts.append(('#8cc14c', 40, 19.5, 'none')) width = sum(p[1] for p in parts) self.set_header('Content-Type', 'image/svg+xml; charset=utf-8') self.render('status.svg', width=width, parts=parts, height=20) class ErrorHandler(BaseHandler, BaseErrorHandler):