diff options
-rw-r--r-- | pyaggr3g470r/controllers/article.py | 9 | ||||
-rw-r--r-- | pyaggr3g470r/templates/home.html | 2 | ||||
-rw-r--r-- | pyaggr3g470r/views/views.py | 48 |
3 files changed, 35 insertions, 24 deletions
diff --git a/pyaggr3g470r/controllers/article.py b/pyaggr3g470r/controllers/article.py index 46ca0988..0ec53a2f 100644 --- a/pyaggr3g470r/controllers/article.py +++ b/pyaggr3g470r/controllers/article.py @@ -1,3 +1,6 @@ +from sqlalchemy import func + +from bootstrap import db import conf from .abstract import AbstractController from pyaggr3g470r.models import Article @@ -25,3 +28,9 @@ class ArticleController(AbstractController): if self.read(**id_).first(): continue yield id_ + + def get_unread(self): + return dict(db.session.query(Article.feed_id, func.count(Article.id)) + .filter(Article.readed == False, + Article.user_id == self.user_id) + .group_by(Article.feed_id).all()) diff --git a/pyaggr3g470r/templates/home.html b/pyaggr3g470r/templates/home.html index 390ac5f6..9872c903 100644 --- a/pyaggr3g470r/templates/home.html +++ b/pyaggr3g470r/templates/home.html @@ -42,7 +42,7 @@ {% if feed_id == fid %}</b>{% endif %} </a></li> <li class="feed-commands"><span> - <a href="/feed/{{ fid }}"><i class="glyphicon glyphicon-info-sign" title="{{ _('Details') }}"></i></a> + <a href="{{ url_for("feed.feed", feed_id=fid) }}"><i class="glyphicon glyphicon-info-sign" title="{{ _('Details') }}"></i></a> <a href="/articles/{{ fid }}/100"><i class="glyphicon glyphicon-th-list" title="{{ _('Articles') }}"></i></a> <a href="{{ url_for("feed.form", feed_id=fid) }}"><i class="glyphicon glyphicon-edit" title="{{ _('Edit this feed') }}"></i></a> <a href="{{ url_for("feed.delete", feed_id=fid) }}"><i class="glyphicon glyphicon-remove" title="{{ _('Delete this feed') }}" onclick="return confirm('{{ _('You are going to delete this feed.') }}');"></i></a> diff --git a/pyaggr3g470r/views/views.py b/pyaggr3g470r/views/views.py index b329afae..27dce8a9 100644 --- a/pyaggr3g470r/views/views.py +++ b/pyaggr3g470r/views/views.py @@ -34,25 +34,25 @@ import datetime from collections import namedtuple from bootstrap import application as app, db from flask import render_template, request, flash, session, \ - url_for, redirect, g, current_app, make_response, jsonify + 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 func, or_ +from sqlalchemy import or_ from sqlalchemy.exc import IntegrityError from werkzeug import generate_password_hash import conf from pyaggr3g470r import utils, notifications, export -from pyaggr3g470r import controllers from pyaggr3g470r.models import User, Feed, Article, Role from pyaggr3g470r.decorators import feed_access_required from pyaggr3g470r.forms import SignupForm, SigninForm, AddFeedForm, \ ProfileForm, InformationMessageForm, RecoverPasswordForm -from pyaggr3g470r.controllers import FeedController +from pyaggr3g470r.controllers import UserController, FeedController, \ + ArticleController if not conf.ON_HEROKU: import pyaggr3g470r.search as fastsearch @@ -93,7 +93,7 @@ def before_request(): @login_manager.user_loader def load_user(email): # Return an instance of the User model - return controllers.UserController().get(email=email) + return UserController().get(email=email) # @@ -153,7 +153,7 @@ def login(): form = SigninForm() if form.validate_on_submit(): - user = controllers.UserController().get(email=form.email.data) + user = UserController().get(email=form.email.data) login_user(user) g.user = user session['email'] = form.email.data @@ -229,32 +229,34 @@ def home(): """ Home page for connected users. Displays by default unread articles. """ - feeds = {feed.id: feed.title for feed in g.user.feeds} - articles = Article.query.filter(Article.feed_id.in_(feeds.keys()), - Article.user_id == g.user.id) + feed_contr = FeedController(g.user.id) + arti_contr = ArticleController(g.user.id) + feeds = {feed.id: feed.title for feed in feed_contr.read()} + + unread = arti_contr.get_unread() + in_error = {feed.id: feed.error_count for feed in + feed_contr.read(error_count__gt=2)} + filter_ = request.args.get('filter_', 'unread') feed_id = int(request.args.get('feed', 0)) limit = request.args.get('limit', 1000) + + filters = {} if filter_ != 'all': - articles = articles.filter(Article.readed == (filter_ == 'read')) + filters['readed'] = filter_ == 'read' if feed_id: - articles = articles.filter(Article.feed_id == feed_id) + filters['feed_id'] = feed_id - articles = articles.order_by(Article.date.desc()) + articles = arti_contr.read(**filters).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, Article.user_id == g.user.id)\ - .group_by(Article.feed_id).all() - in_error = {feed.id: feed.error_count for feed in - FeedController(g.user.id).read(error_count__gt=2).all()} def gen_url(filter_=filter_, limit=limit, feed=feed_id): return url_for('home', filter_=filter_, limit=limit, feed=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(), + unread=unread, articles=list(articles), in_error=in_error, default_max_error = conf.DEFAULT_MAX_ERROR) @@ -383,7 +385,7 @@ def inactives(): List of inactive feeds. """ nb_days = int(request.args.get('nb_days', 365)) - user = controllers.UserController(g.user.id).get(email=g.user.email) + user = UserController(g.user.id).get(email=g.user.email) today = datetime.datetime.now() inactives = [] for feed in user.feeds: @@ -430,7 +432,7 @@ def export_articles(): """ Export all articles to HTML or JSON. """ - user = controllers.UserController(g.user.id).get(id=g.user.id) + user = UserController(g.user.id).get(id=g.user.id) if request.args.get('format') == "HTML": # Export to HTML try: @@ -463,7 +465,7 @@ def export_opml(): """ Export all feeds to OPML. """ - user = controllers.UserController(g.user.id).get(id=g.user.id) + user = UserController(g.user.id).get(id=g.user.id) response = make_response(render_template('opml.xml', user=user, now=datetime.datetime.now())) response.headers['Content-Type'] = 'application/xml' @@ -564,7 +566,7 @@ def profile(): """ Edit the profile of the currently logged user. """ - user = controllers.UserController(g.user.id).get(id=g.user.id) + user = UserController(g.user.id).get(id=g.user.id) form = ProfileForm() if request.method == 'POST': @@ -590,7 +592,7 @@ def delete_account(): """ Delete the account of the user (with all its data). """ - user = controllers.UserController(g.user.id).get(id=g.user.id) + user = UserController(g.user.id).get(id=g.user.id) if user is not None: db.session.delete(user) db.session.commit() |