diff options
Diffstat (limited to 'pyaggr3g470r/views.py')
-rw-r--r-- | pyaggr3g470r/views.py | 88 |
1 files changed, 59 insertions, 29 deletions
diff --git a/pyaggr3g470r/views.py b/pyaggr3g470r/views.py index e4668f54..1b3be75e 100644 --- a/pyaggr3g470r/views.py +++ b/pyaggr3g470r/views.py @@ -28,11 +28,15 @@ __license__ = "AGPLv3" import os import datetime -from flask import render_template, request, flash, session, url_for, redirect, g, current_app, make_response -from flask.ext.login import LoginManager, login_user, logout_user, login_required, current_user, AnonymousUserMixin -from flask.ext.principal import Principal, Identity, AnonymousIdentity, identity_changed, identity_loaded, Permission, RoleNeed, UserNeed +from flask import abort, render_template, request, flash, session, \ + url_for, redirect, g, current_app, make_response +from flask.ext.login import LoginManager, login_user, logout_user, \ + login_required, current_user, AnonymousUserMixin +from flask.ext.principal import Principal, Identity, AnonymousIdentity, \ + identity_changed, identity_loaded, Permission,\ + RoleNeed, UserNeed from flask.ext.babel import gettext -from sqlalchemy import desc +from sqlalchemy import desc, func from sqlalchemy.exc import IntegrityError from werkzeug import generate_password_hash @@ -209,23 +213,41 @@ def signup(): @app.route('/') @login_required def home(): - """ - The home page lists most recent articles of all feeds. - """ - #user = User.query.filter(User.email == g.user.email).first() - result = [] - for feed in g.user.feeds: - new_feed = Feed() - new_feed.id = feed.id - new_feed.title = feed.title - new_feed.enabled = feed.enabled - new_feed.articles = feed.articles[:9] - #new_feed.articles = Article.query.filter(Article.user_id == g.user.id, - #Article.feed_id == feed.id).order_by(desc("Article.date")).limit(9) - new_feed.nb_unread = Article.query.filter(Article.user_id == g.user.id, Article.feed_id == feed.id, Article.readed == False).count() - result.append(new_feed) - unread_articles = len(Article.query.filter(Article.user_id == g.user.id, Article.readed == False).all()) - return render_template('home.html', result=result, head_title=unread_articles) + feeds = {feed.id: feed.title for feed in g.user.feeds if feed.enabled} + articles = Article.query.filter(Article.feed_id.in_(feeds.keys())) + filter_ = request.args.get('filter_', 'unread') + feed_id = int(request.args.get('feed', 0)) + limit = request.args.get('limit', 1000) + if filter_ != 'all': + articles = articles.filter(Article.readed == (filter_ == 'read')) + if feed_id: + articles = articles.filter(Article.feed_id == feed_id) + + articles = articles.order_by(Article.date.desc()) + if limit != 'all': + limit = int(limit) + articles = articles.limit(limit) + unread = db.session.query(Article.feed_id, func.count(Article.id))\ + .filter(Article.readed == False)\ + .group_by(Article.feed_id).all() + def gen_url(filter_=filter_, limit=limit, feed=feed_id): + return '/?filter_=%s&limit=%s&feed=%d' % (filter_, limit, feed) + return render_template('home.html', gen_url=gen_url, feed_id=feed_id, + filter_=filter_, limit=limit, feeds=feeds, + unread=dict(unread), articles=articles.all()) + + +@app.route('/article/redirect/<int:article_id>', methods=['GET']) +@login_required +def redirect_to_article(article_id): + article = Article.query.filter(Article.id == article_id, + Article.user_id == g.user.id).first() + if article is None: + abort(404) + article.readed = True + db.session.commit() + return redirect(article.link) + @app.route('/fetch/', methods=['GET']) @app.route('/fetch/<int:feed_id>', methods=['GET']) @@ -312,22 +334,30 @@ def article(article_id=None): return redirect(redirect_url()) -@app.route('/mark_as_read/', methods=['GET']) -@app.route('/mark_as_read/<int:feed_id>', methods=['GET']) +@app.route('/mark_as/<string:new_value>', methods=['GET']) +@app.route('/mark_as/<string:new_value>/feed/<int:feed_id>', methods=['GET']) +@app.route('/mark_as/<string:new_value>/article/<int:article_id>', methods=['GET']) @login_required @feed_access_required -def mark_as_read(feed_id=None): +def mark_as(new_value='read', feed_id=None, article_id=None): """ Mark all unreaded articles as read. """ + readed = new_value == 'read' + articles = Article.query.filter(Article.user_id == g.user.id) if feed_id is not None: - Article.query.filter(Article.user_id == g.user.id, Article.feed_id == feed_id, - Article.readed == False).update({"readed": True}) - flash(gettext('Articles marked as read.'), 'info') + articles = articles.filter(Article.feed_id == feed_id) + message = 'Feed marked as %s.' + elif article_id is not None: + articles = articles.filter(Article.id == article_id) + message = 'Article marked as %s.' else: - Article.query.filter(Article.user_id == g.user.id, Article.readed == False).update({"readed": True}) - flash(gettext("All articles marked as read"), 'info') + message = 'All article marked as %s.' + articles.filter(Article.readed == (not readed)).update({"readed": readed}) + flash(gettext(message % new_value), 'info') db.session.commit() + if readed: + return redirect(redirect_url()) return redirect(redirect_url()) @app.route('/like/<int:article_id>', methods=['GET']) |