From f03a9825fc047e2371356d7b1784c65835756c55 Mon Sep 17 00:00:00 2001 From: Cédric Bonhomme Date: Wed, 30 Apr 2014 10:58:54 +0200 Subject: Link between next and previous articles. --- pyaggr3g470r/models.py | 14 +++++++++++++- pyaggr3g470r/templates/article.html | 10 ++++++++++ pyaggr3g470r/views.py | 12 +++++++++++- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/pyaggr3g470r/models.py b/pyaggr3g470r/models.py index f4aa0521..6c6df180 100644 --- a/pyaggr3g470r/models.py +++ b/pyaggr3g470r/models.py @@ -28,7 +28,7 @@ __license__ = "GPLv3" import json from datetime import datetime -from sqlalchemy import desc +from sqlalchemy import asc, desc from werkzeug import generate_password_hash, check_password_hash from flask.ext.login import UserMixin @@ -123,6 +123,18 @@ class Article(db.Model): user_id = db.Column(db.Integer, db.ForeignKey('user.id')) feed_id = db.Column(db.Integer, db.ForeignKey('feed.id')) + def previous_article(self): + """ + Returns the previous article (older). + """ + return Article.query.filter(Article.date < self.date, Article.feed_id == self.feed_id).order_by(desc("Article.date")).first() + + def next_article(self): + """ + Returns the next article (newer). + """ + return Article.query.filter(Article.date > self.date, Article.feed_id == self.feed_id).order_by(asc("Article.date")).first() + def __repr__(self): return json.dumps({ "title": self.title, diff --git a/pyaggr3g470r/templates/article.html b/pyaggr3g470r/templates/article.html index 7cbee48a..1ca1ce49 100644 --- a/pyaggr3g470r/templates/article.html +++ b/pyaggr3g470r/templates/article.html @@ -19,6 +19,16 @@
{{ article.content|safe }}
+
+
+
+ Next post: {{ next_article.title }} +
+
+ Previous post: {{ previous_article.title }} +
+
+
diff --git a/pyaggr3g470r/views.py b/pyaggr3g470r/views.py index 491d44a2..6d2a3b99 100644 --- a/pyaggr3g470r/views.py +++ b/pyaggr3g470r/views.py @@ -243,7 +243,17 @@ def article(article_id=None): if not article.readed: article.readed = True db.session.commit() - return render_template('article.html', head_title=utils.clear_string(article.title), article=article) + + previous_article = article.previous_article() + if previous_article is None: + previous_article = article.source.articles[0] + next_article = article.next_article() + if next_article is None: + next_article = article.source.articles[-1] + + return render_template('article.html', head_title=utils.clear_string(article.title), + article=article, + previous_article=previous_article, next_article=next_article) flash("This article do not exist.", 'warning') return redirect(redirect_url()) -- cgit