Merge pull request #19 from touilleMan/add-list-post-page

Add pagination to posts page
This commit is contained in:
Guillaume Ayoub 2018-05-11 17:57:47 +02:00 committed by GitHub
commit 8d92d29f9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 4 deletions

15
afpy.py
View File

@ -20,6 +20,8 @@ cache = Cache(config={'CACHE_TYPE': 'simple', 'CACHE_DEFAULT_TIMEOUT': 600})
app = Flask(__name__)
cache.init_app(app)
PAGINATION = 12
PLANET = {
'Emplois AFPy': 'https://www.afpy.org/feed/emplois/rss.xml',
'Nouvelles AFPy': 'https://www.afpy.org/feed/actualites/rss.xml',
@ -158,20 +160,25 @@ def save_post(name, timestamp=None):
@app.route('/posts/<name>')
def posts(name):
@app.route('/posts/<name>/page/<int:page>')
def posts(name, page=1):
if name not in POSTS:
abort(404)
path = root / name / 'published'
timestamps = sorted(path.iterdir(), reverse=True)[:12]
timestamps = sorted(path.iterdir(), reverse=True)
end = page * PAGINATION
start = end - PAGINATION
total_pages = (len(timestamps) // PAGINATION) + 1
posts = {}
for timestamp in timestamps:
for timestamp in timestamps[start:end]:
tree = ElementTree.parse(timestamp / 'post.xml')
posts[timestamp.name] = {item.tag: item.text for item in tree.iter()}
if (timestamp / 'post.jpg').is_file():
posts[timestamp.name]['image'] = '/'.join((
name, 'published', timestamp.name))
return render_template(
'posts.html', body_id=name, posts=posts, title=POSTS[name], name=name)
'posts.html', body_id=name, posts=posts, title=POSTS[name], name=name,
page=page, total_pages=total_pages)
@app.route('/admin/posts/<name>')

View File

@ -22,4 +22,13 @@
<p><a href="{{ url_for('post', name=name, timestamp=timestamp) }}">Lire la suite…</a></p>
</article>
{% endfor %}
<aside>
{% if page != 1 %}
<a href="{{ url_for('posts', name=name, page=page-1) }}">Précedente</a>
{% endif %}
Page {{ page }}/{{ total_pages }}
{% if page != total_pages %}
<a href="{{ url_for('posts', name=name, page=page+1) }}">Suivante</a>
{% endif %}
</aside>
{% endblock main %}