diff --git a/kremlin/core.py b/kremlin/core.py index 0aa5693..01f5875 100644 --- a/kremlin/core.py +++ b/kremlin/core.py @@ -13,27 +13,31 @@ from flask import request, session, render_template, flash, url_for, \ - redirect, send_from_directory - + redirect, send_from_directory, abort from werkzeug import secure_filename - - from kremlin import app, db, dbmodel, forms, imgutils, uploaded_images - - +from pagination import Pagination @app.route('/') def home_index(): """ Display the glasnost logo, attempt to replicate old behavior """ return render_template('home.html') -@app.route('/images') -def entries_index(): +@app.route('/images', defaults={'page': 1}) +@app.route('/images/page/') +def entries_index(page): """ Show an index of image thumbnails """ - #FIXME: limit with pagination - posts = dbmodel.Post.query.all() + posts = dbmodel.Post.query.paginate(page, 25) + posts_count = posts.total + pagination = Pagination(page, 25, posts_count) return render_template('board.html', form=forms.NewPostForm(), - posts=posts) + posts=posts.items, pagination=pagination) + +def url_for_other_page(page): + args = request.view_args.copy() + args['page'] = page + return url_for(request.endpoint, **args) +app.jinja_env.globals['url_for_other_page'] = url_for_other_page @app.route('/logs') def logs_index(): @@ -188,4 +192,3 @@ def register(): def about(): return "Kremlin Everything System and Boredom Inhibitor v 0.0.0-None" - diff --git a/kremlin/forms.py b/kremlin/forms.py index dad8aef..796976b 100644 --- a/kremlin/forms.py +++ b/kremlin/forms.py @@ -17,6 +17,10 @@ from kremlin import uploaded_images +from math import ceil + +from pagination import Pagination + class NewPostForm(Form): """ Post Form """ name = TextField(u'Name', validators=[validators.required()]) @@ -70,3 +74,4 @@ class RegisterForm(Form): confirm = PasswordField(u'Repeat Password') # accept_tos = BooleanField(u'I accept the TOS', # validators=[validators.Required()]) + diff --git a/kremlin/pagination.py b/kremlin/pagination.py new file mode 100644 index 0000000..3a800d8 --- /dev/null +++ b/kremlin/pagination.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python +""" +KREMLIN +""" + +import os +import sys + +from math import ceil + +class Pagination(object): + + def __init__(self, page, per_page, total_count): + self.page = page + self.per_page = per_page + self.total_count = total_count + + @property + def pages(self): + return int(ceil(self.total_count / float(self.per_page))) + + @property + def has_prev(self): + return self.page > 1 + + @property + def has_next(self): + return self.page < self.pages + + def iter_pages(self, left_edge=2, left_current=2, right_current=5, + right_edge=2): + last = 0 + for num in xrange(1, self.pages + 1): + if num <= left_edge or \ + (num > self.page - left_current - 1 and \ + num < self.page + right_current) or \ + num > self.pages - right_edge: + if last + 1 != num: + yield None + yield num + last = num + diff --git a/kremlin/static/javascripts/app.js b/kremlin/static/javascripts/app.js index ea87d67..6b49054 100644 --- a/kremlin/static/javascripts/app.js +++ b/kremlin/static/javascripts/app.js @@ -44,5 +44,5 @@ }, 0); }); } - + })(jQuery, this); diff --git a/kremlin/templates/board.html b/kremlin/templates/board.html index 6026c3e..16782d8 100644 --- a/kremlin/templates/board.html +++ b/kremlin/templates/board.html @@ -13,8 +13,23 @@
- {# TODO: Paginate like a motherfucker #} - Hi this is where pagination will go +
{% for post in posts %} diff --git a/kremlin/templates/layout.html b/kremlin/templates/layout.html index 46c263e..acc3fde 100644 --- a/kremlin/templates/layout.html +++ b/kremlin/templates/layout.html @@ -72,7 +72,7 @@
Magical Everything System - Prototype