From 0b82405c091aad39ade4a57978e7997b7f9d5dd3 Mon Sep 17 00:00:00 2001 From: Cédric Bonhomme Date: Tue, 20 Sep 2016 18:05:33 +0200 Subject: Improved the way we count feeds by link. --- src/web/controllers/feed.py | 3 +++ src/web/views/views.py | 12 +++++------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/web/controllers/feed.py b/src/web/controllers/feed.py index a3f5cae7..c67c6bea 100644 --- a/src/web/controllers/feed.py +++ b/src/web/controllers/feed.py @@ -70,6 +70,9 @@ class FeedController(AbstractController): def count_by_category(self, **filters): return self._count_by(Feed.category_id, filters) + def count_by_link(self, **filters): + return self._count_by(Feed.link, filters) + def _ensure_icon(self, attrs): if not attrs.get('icon_url'): return diff --git a/src/web/views/views.py b/src/web/views/views.py index c3425a17..ceb1ab10 100644 --- a/src/web/views/views.py +++ b/src/web/views/views.py @@ -1,5 +1,5 @@ import logging -from collections import Counter +import operator from flask import (request, render_template, flash, url_for, redirect, current_app) from flask_babel import gettext @@ -45,12 +45,10 @@ def handle_sqlalchemy_assertion_error(error): @current_app.route('/popular', methods=['GET']) @etag_match def popular(): - feeds = FeedController().read().all() - counter = Counter() - for feed in feeds: - counter[feed.link] += 1 - print(counter.most_common(50)) - return render_template('popular.html', popular=counter.most_common(50)) + feeds = FeedController().count_by_link() + sorted_feeds = sorted(feeds.items(), key=operator.itemgetter(1), + reverse=True) + return render_template('popular.html', popular=sorted_feeds) @current_app.route('/about', methods=['GET']) -- cgit