Download:
child 217:a5139d092b65
parent 215:7716c8ee7c85
216:f679f272fe10
Anton Shestakov <av6@dwimlabs.net>, Tue, 23 Aug 2016 22:17:00 +0800
viewer: basic pagination on project page (to see all previous checks)

4 файлов изменено, 38 вставок(+), 3 удалений(-) [+]
candolint/handlers.py file | annotate | diff | comparison | revisions
candolint/uimodules.py file | annotate | diff | comparison | revisions
templates/project.html file | annotate | diff | comparison | revisions
templates/ui/pagination.html file | annotate | diff | comparison | revisions
--- a/candolint/handlers.py Tue Aug 23 21:37:18 2016 +0800
+++ b/candolint/handlers.py Tue Aug 23 22:17:00 2016 +0800
@@ -1,6 +1,7 @@
from __future__ import absolute_import, division
import traceback
+from math import ceil
from peewee import DoesNotExist
from tornado.escape import json_decode
@@ -35,6 +36,17 @@
return get_or_404(cq)
+class Paginator(object):
+ def __init__(self, query, page, paginate_by=30):
+ self.query = query
+ self.page = page
+ self.paginate_by = paginate_by
+ self.pages = int(ceil(query.count() / paginate_by))
+
+ def iterator(self):
+ return self.query.paginate(self.page, self.paginate_by).iterator()
+
+
class BaseHandler(RequestHandler):
def prepare(self):
database.connect()
@@ -44,6 +56,14 @@
if not database.is_closed():
database.close()
+ def paginate(self, query):
+ page = self.get_query_argument('page', '1')
+ if page and page.isdigit():
+ page = max(1, int(page))
+ else:
+ page = 1
+ return Paginator(query, page)
+
def write_error(self, status_code, **kwargs):
data = {
'code': status_code,
@@ -77,8 +97,7 @@
checks = (Check
.select(Change, *Check.get_light_fields())
.join(Change)
- .where(Check.project == project)
- .limit(30))
+ .where(Check.project == project))
self.render('project.html', project=project, checks=checks)
--- a/candolint/uimodules.py Tue Aug 23 21:37:18 2016 +0800
+++ b/candolint/uimodules.py Tue Aug 23 22:17:00 2016 +0800
@@ -16,3 +16,8 @@
class Badges(UIModule):
def render(self, check):
return self.render_string('ui/badges.html', check=check)
+
+
+class Pagination(UIModule):
+ def render(self, page, pages):
+ return self.render_string('ui/pagination.html', page=page, pages=pages)
--- a/templates/project.html Tue Aug 23 21:37:18 2016 +0800
+++ b/templates/project.html Tue Aug 23 22:17:00 2016 +0800
@@ -18,7 +18,8 @@
</dl>
<table class="uk-table uk-table-middle uk-text-nowrap">
<tbody>
- {% for check in checks %}
+ {% set paged_checks = handler.paginate(checks) %}
+ {% for check in paged_checks.iterator() %}
{% set change = check.change %}
{% set adapter = project.get_adapter() %}
<tr>
@@ -36,5 +37,8 @@
{% end %}
</tbody>
</table>
+ {% if paged_checks.pages > 1 %}
+ {% module Pagination(paged_checks.page, paged_checks.pages) %}
+ {% end %}
</div>
{% end %}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/ui/pagination.html Tue Aug 23 22:17:00 2016 +0800
@@ -0,0 +1,7 @@
+<ul class="uk-pagination">
+ {% for i in range(1, pages + 1) %}
+ <li{% if page == i %} class="uk-active"{% end %}>
+ <a href="?page={{ i }}">{{ i }}</a>
+ </li>
+ {% end %}
+</ul>