Download:
child 245:19950999005a
parent 243:0a5072b593dc
244:62252030324f
Anton Shestakov <av6@dwimlabs.net>, Wed, 26 Oct 2016 07:33:36 +0800
viewer: add Atom feeds to projects

4 файлов изменено, 65 вставок(+), 1 удалений(-) [+]
candolint/handlers.py file | annotate | diff | comparison | revisions
templates/atom.xml file | annotate | diff | comparison | revisions
tests/test_viewer.py file | annotate | diff | comparison | revisions
viewer.py file | annotate | diff | comparison | revisions
--- a/candolint/handlers.py Wed Oct 26 06:52:26 2016 +0800
+++ b/candolint/handlers.py Wed Oct 26 07:33:36 2016 +0800
@@ -101,6 +101,33 @@
self.render('project.html', project=project, checks=checks)
+class AtomHandler(BaseHandler):
+ def get(self, domain, user, name):
+ project = get_project_or_404(domain, user, name)
+ checks = (Check
+ .select(Change, *Check.get_light_fields())
+ .join(Change)
+ .where(Check.project == project))
+ def status(check):
+ if not check.success:
+ return 'unknown'
+ elif check.errors or check.warnings:
+ status = []
+ if check.errors:
+ msg = self.locale.translate(
+ '{} error', '{} errors', check.errors)
+ status.append(msg.format(check.errors))
+ if check.warnings:
+ msg = self.locale.translate(
+ '{} warning', '{} warnings', check.warnings)
+ status.append(msg.format(check.warnings))
+ return ', '.join(status)
+ else:
+ return 'all clean'
+ self.set_header('Content-Type', 'application/atom+xml; charset=utf-8')
+ self.render('atom.xml', project=project, checks=checks, status=status)
+
+
class CheckHandler(BaseHandler):
def get(self, domain, user, name, check_num, format_='html'):
project = get_project_or_404(domain, user, name)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/atom.xml Wed Oct 26 07:33:36 2016 +0800
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<feed xmlns="http://www.w3.org/2005/Atom">
+ <icon>{{ request.protocol }}://{{ request.host }}/static/favicon-cat.png</icon>
+ <title>{{ project.name }} feed</title>
+ <id>{{ project.get_url(request) }}/atom</id>
+ <link href="{{ project.get_url(request) }}/atom" rel="self"/>
+ <link href="{{ project.get_url(request) }}"/>
+ {% set last = checks.first() %}
+ {% if last %}
+ <updated>{{ last.finished.isoformat() }}Z</updated>
+ {% else %}
+ <updated>1970-01-01T08:00:00</updated>
+ {% end %}
+
+ {% for check in checks.limit(20) %}
+ <entry>
+ <title>Check #{{ check.ordinal }}: {{ status(check) }}</title>
+ <id>{{ project.get_url(request) }}/{{ check.ordinal }}</id>
+ <link href="{{ project.get_url(request) }}/{{ check.ordinal }}"/>
+ <updated>{{ check.finished.isoformat() }}Z</updated>
+ </entry>
+ {% end %}
+</feed>
--- a/tests/test_viewer.py Wed Oct 26 06:52:26 2016 +0800
+++ b/tests/test_viewer.py Wed Oct 26 07:33:36 2016 +0800
@@ -108,6 +108,19 @@
assert '1 warning' in response.body
assert '<a href="#">default</a>' in response.body
+ def test_atom(self):
+ response = self.fetch('/example.com/alice/test-viewer/atom')
+ assert response.code == 200
+ assert 'atom+xml' in response.headers['Content-Type']
+ root = ElementTree.fromstring(response.body)
+ ns = {'atom': 'http://www.w3.org/2005/Atom'}
+ title = root.find('./atom:entry/atom:title', ns)
+ assert title is not None
+ assert title.text == 'Check #1: 1 error, 1 warning'
+ updates = [el.text for el in root.findall('.//atom:updated', ns)]
+ assert len(updates) == 2
+ assert updates[0] == updates[1] == '2016-08-20T02:38:53Z'
+
def test_check(self):
response = self.fetch('/butt.cloud/cyber/wizard-attack/latest')
assert response.code == 404
--- a/viewer.py Wed Oct 26 06:52:26 2016 +0800
+++ b/viewer.py Wed Oct 26 07:33:36 2016 +0800
@@ -8,7 +8,7 @@
from tornado.web import Application
from candolint import uimodules
-from candolint.handlers import IndexHandler, ProjectHandler, CheckHandler, StatusHandler, ErrorHandler
+from candolint.handlers import IndexHandler, ProjectHandler, AtomHandler, CheckHandler, StatusHandler, ErrorHandler
from candolint.models import database
from candolint.utils import rel
@@ -19,6 +19,7 @@
handlers = [
(r'/', IndexHandler),
(project_re, ProjectHandler),
+ (project_re + r'/atom', AtomHandler),
(project_re + r'/(\d+|latest)(?:/(raw))?', CheckHandler),
(project_re + r'/status\.svg', StatusHandler),
(r'.*', ErrorHandler, {'status_code': 404})