Skip to content

Commit

Permalink
Add basic pagination to news
Browse files Browse the repository at this point in the history
Add:
- Pagination buttons to the template
- Custom IntegerConverter to Flask to support negative numbers
- Ability to select a certain range of newsentries (start~end)

Refs #2
  • Loading branch information
lukasjuhrich committed Apr 22, 2015
1 parent 6fc51ec commit ea9bbc2
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 6 deletions.
12 changes: 12 additions & 0 deletions sipa/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,20 @@
from sipa.utils.graph_utils import render_traffic_chart
from sipa.utils.ldap_utils import User, authenticate

from werkzeug.routing import IntegerConverter as BaseIntegerConverter


class IntegerConverter(BaseIntegerConverter):
"""Modification of the standard IntegerConverter which does not support
negative values. See
http://werkzeug.pocoo.org/docs/0.10/routing/#werkzeug.routing.IntegerConverter
"""
regex = r'-?\d+'


app = Flask('sipa')

app.url_map.converters['int'] = IntegerConverter
login_manager = LoginManager()


Expand Down
58 changes: 52 additions & 6 deletions sipa/blueprints/news.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,67 @@
Blueprint providing features regarding the news entries.
"""

from flask import Blueprint, render_template
from flask import Blueprint, render_template, url_for, redirect
from sipa.flatpages import cf_pages

bp_news = Blueprint('news', __name__, url_prefix='/news')


@bp_news.route("/")
def display():
@bp_news.route("/start/<int:start>")
@bp_news.route("/end/<int:end>")
# todo implement (gui) feature to select range
@bp_news.route("/range/<int:start>:<int:end>")
def display(start=None, end=None):
"""Get all markdown files from 'content/news/', parse them and put
them in a list for the template.
The formatting of these files is described in the readme.
"""
cf_pages.reload()
latest = cf_pages.get_articles_of_category('news')
latest = sorted(latest, key=lambda a: a.date, reverse=True)
latest = latest[0:10]
return render_template("index.html", articles=latest)
news = cf_pages.get_articles_of_category('news')
news = sorted(news, key=lambda a: a.date, reverse=True)

default_step = 10
# calculating mod len() allows things like `end=-1` for the last article(s).
# this may lead to confusing behaviour because this allows values out of the
# range (|val|≥len(latest)), but this will only result in displaying
# articles instead of throwing an error.
# Apart from that, such values would just appear if edited manually.
if start is None:
if end is None:
start, end = 0, default_step
else:
end %= len(news)
start = max(end - default_step + 1, 0)
else:
start %= len(news)
if end is None:
end = min(start + default_step - 1, len(news) - 1)
else:
end %= len(news)

delta = end - start + 1
prev_range, next_range = None, None

if start > 0:
prev_range = {'start': max(start - delta, 0), 'end': start - 1}
if end < len(news) - 1:
next_range = {'start': end + 1, 'end': min(end + delta, len(news) - 1)}

return render_template("index.html", articles=news[start:end],
previous_range=prev_range, next_range=next_range)


@bp_news.route("/newest")
def newest():
return redirect(url_for(".display"))


@bp_news.route("/oldest")
def oldest():
return redirect(url_for(".display", end=-1))


@bp_news.route("/all")
def display_all():
return redirect(url_for(".display", start=0, end=-1))
25 changes: 25 additions & 0 deletions sipa/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,29 @@
<div class="panel-footer">{{ _("geschrieben von") }} {{ art.author }}, {{ art.date }}</div>
</div>
{% endfor %}
<nav>
<ul class="pager">
{% if previous_range %}
<li class="previous">
<a href="{{ url_for(".display", start=previous_range.start, end=previous_range.end) }}">
<span aria-hidden="true">&larr;</span> {{ _("Neuer") }}</a>
</li>
{% else %}
<li class="previous disabled">
<a href="#">{{ _("Keine neueren Beiträge") }}</a>
</li>
{% endif %}

{% if next_range %}
<li class="next">
<a href="{{ url_for(".display", start=next_range.start, end=next_range.end) }}">
{{ _("Älter") }} <span aria-hidden="true">&rarr;</span></a>
</li>
{% else %}
<li class="next disabled">
<a href="#">{{ _("Keine älteren Beiträge") }}</a>
</li>
{% endif %}
</ul>
</nav>
{% endblock %}

0 comments on commit ea9bbc2

Please sign in to comment.