Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pagination #5

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
27 changes: 15 additions & 12 deletions kremlin/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<int: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():
Expand Down Expand Up @@ -188,4 +192,3 @@ def register():
def about():
return "Kremlin Everything System and Boredom Inhibitor v 0.0.0-None"


5 changes: 5 additions & 0 deletions kremlin/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()])
Expand Down Expand Up @@ -70,3 +74,4 @@ class RegisterForm(Form):
confirm = PasswordField(u'Repeat Password')
# accept_tos = BooleanField(u'I accept the TOS',
# validators=[validators.Required()])

42 changes: 42 additions & 0 deletions kremlin/pagination.py
Original file line number Diff line number Diff line change
@@ -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)))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be simplified to 1 + total // 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):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be reimplemented as xrange(self.pages)

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

2 changes: 1 addition & 1 deletion kremlin/static/javascripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@
}, 0);
});
}

})(jQuery, this);
19 changes: 17 additions & 2 deletions kremlin/templates/board.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,23 @@
<input type="submit" value="Upload"><br />
</form>
<div id='ImageboardPageTop'>
{# TODO: Paginate like a motherfucker #}
Hi this is where pagination will go
<div class=pagination>
{% for page in pagination.iter_pages() %}
{% if page %}
{% if page != pagination.page %}
<a href="{{ url_for_other_page(page) }}">{{ page }}</a>
{% else %}
<strong>{{ page }}</strong>
{% endif %}
{% else %}
<span class=ellipsis>…</span>
{% endif %}
{% endfor %}
{% if pagination.has_next %}
<a href="{{ url_for_other_page(pagination.page + 1)
}}">Next &raquo;</a>
{% endif %}
</div>
</div>
<div id="ImageboardContainer">
{% for post in posts %}
Expand Down
4 changes: 2 additions & 2 deletions kremlin/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ <h5>Magical Everything System - Prototype</h5>
<div id="Menu" class="twelve columns">
{% block menu %}
<ul class="nav-bar">
<li><a href="{{ url_for('home_index') }}" class="main">home</a></li>
<li><a href="{{ url_for('home_index') }}" class="main">Home</a></li>
<li><a href="{{ url_for('entries_index') }}" class="main">Imageboard</a></li>
<li><a href="{{ url_for('logs_index') }}" class="main">IRC logs</a></li>
<li><a href="{{ url_for('services_index') }}" class="main">Services Index</a></li>
Expand Down Expand Up @@ -102,7 +102,7 @@ <h5>Magical Everything System - Prototype</h5>
<div class="row">
<div id="Footer" class="four columns centered">
{% block footer %}
<p>Copyright &copy; glasnost.us 2004-2012</p>
<p>Copyright &copy; glasnost.us 2004-2015</p>
{% endblock %}
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion runserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def usage():
def main():
""" Program entry point when ran interactively """
print "Kremlin Magical Everything System v 0.0.0-None"
print "Copyright (c) Glasnost 2010-2011"
print "Copyright (c) Glasnost 2010-2015"
print "-----------------------------------------------"

# If no configuration is specified, use the default.
Expand Down