forked from AFPy/afpy.org
Merge pull request #19 from touilleMan/add-list-post-page
Add pagination to posts page
This commit is contained in:
commit
8d92d29f9d
15
afpy.py
15
afpy.py
|
@ -20,6 +20,8 @@ cache = Cache(config={'CACHE_TYPE': 'simple', 'CACHE_DEFAULT_TIMEOUT': 600})
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
cache.init_app(app)
|
cache.init_app(app)
|
||||||
|
|
||||||
|
PAGINATION = 12
|
||||||
|
|
||||||
PLANET = {
|
PLANET = {
|
||||||
'Emplois AFPy': 'https://www.afpy.org/feed/emplois/rss.xml',
|
'Emplois AFPy': 'https://www.afpy.org/feed/emplois/rss.xml',
|
||||||
'Nouvelles AFPy': 'https://www.afpy.org/feed/actualites/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>')
|
@app.route('/posts/<name>')
|
||||||
def posts(name):
|
@app.route('/posts/<name>/page/<int:page>')
|
||||||
|
def posts(name, page=1):
|
||||||
if name not in POSTS:
|
if name not in POSTS:
|
||||||
abort(404)
|
abort(404)
|
||||||
path = root / name / 'published'
|
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 = {}
|
posts = {}
|
||||||
for timestamp in timestamps:
|
for timestamp in timestamps[start:end]:
|
||||||
tree = ElementTree.parse(timestamp / 'post.xml')
|
tree = ElementTree.parse(timestamp / 'post.xml')
|
||||||
posts[timestamp.name] = {item.tag: item.text for item in tree.iter()}
|
posts[timestamp.name] = {item.tag: item.text for item in tree.iter()}
|
||||||
if (timestamp / 'post.jpg').is_file():
|
if (timestamp / 'post.jpg').is_file():
|
||||||
posts[timestamp.name]['image'] = '/'.join((
|
posts[timestamp.name]['image'] = '/'.join((
|
||||||
name, 'published', timestamp.name))
|
name, 'published', timestamp.name))
|
||||||
return render_template(
|
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>')
|
@app.route('/admin/posts/<name>')
|
||||||
|
|
|
@ -22,4 +22,13 @@
|
||||||
<p><a href="{{ url_for('post', name=name, timestamp=timestamp) }}">Lire la suite…</a></p>
|
<p><a href="{{ url_for('post', name=name, timestamp=timestamp) }}">Lire la suite…</a></p>
|
||||||
</article>
|
</article>
|
||||||
{% endfor %}
|
{% 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 %}
|
{% endblock main %}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user