From 71e5ba35f069c09ef9e5faba92ed35095fbf5f86 Mon Sep 17 00:00:00 2001 From: cedricbonhomme Date: Thu, 17 May 2012 08:54:23 +0200 Subject: Improvement: get_articles_from_collection() function can now return only the last 10 articles. --- source/mongodb.py | 16 +++++++++++++--- source/pyAggr3g470r.py | 35 +++++++++++++++++------------------ 2 files changed, 30 insertions(+), 21 deletions(-) (limited to 'source') diff --git a/source/mongodb.py b/source/mongodb.py index d2f87042..9915de75 100644 --- a/source/mongodb.py +++ b/source/mongodb.py @@ -98,15 +98,15 @@ class Articles(object): collection = self.db[str(feed_id)] return collection.find({"article_id":article_id}).next() - def get_articles_from_collection(self, feed_id, condition=None): + def get_articles_from_collection(self, feed_id, condition=None, limit=1000000000): """ Return all the articles of a collection. """ collection = self.db[str(feed_id)] if condition is None: - cursor = collection.find({"type":1}) + cursor = collection.find({"type":1}, limit=limit) else: - cursor = collection.find({"type":1, condition[0]:condition[1]}) + cursor = collection.find({"type":1, condition[0]:condition[1]}, limit=limit) return cursor.sort([("article_date", pymongo.DESCENDING)]) def nb_articles(self, feed_id=None): @@ -124,6 +124,16 @@ class Articles(object): nb_articles += self.nb_articles(feed_id) return nb_articles + def get_favorites(self, feed_id=None): + """ + Return favorites articles. + """ + if feed_id is not None: + # only for a feed + collection = self.db[feed_id] + cursor = collection.find({'type':1, 'article_like':True}) + return cursor + def nb_favorites(self, feed_id=None): """ Return the number of favorites articles of a feed diff --git a/source/pyAggr3g470r.py b/source/pyAggr3g470r.py index 6d873ccd..17cfb549 100755 --- a/source/pyAggr3g470r.py +++ b/source/pyAggr3g470r.py @@ -158,7 +158,7 @@ class Root: feed["feed_link"], feed["feed_image"]) # The main page display only 10 articles by feeds. - for article in self.mongo.get_articles_from_collection(feed["feed_id"])[:10]: + for article in self.mongo.get_articles_from_collection(feed["feed_id"], limit=10): if article["article_readed"] == False: # not readed articles are in bold not_read_begin, not_read_end = "", "" @@ -563,7 +563,7 @@ class Root: """ try: feed = self.mongo.get_feed(feed_id) - articles = self.mongo.get_articles_from_collection(feed_id) + articles = self.mongo.get_articles_from_collection(feed_id, limit=10) nb_articles_feed = self.mongo.nb_articles(feed_id) nb_articles_total = self.mongo.nb_articles() nb_unread_articles_feed = self.mongo.nb_unread_articles(feed_id) @@ -596,7 +596,7 @@ class Root: (str(articles[nb_articles_feed-2]["article_date"])[:10], str(articles[0]["article_date"])[:10]) html += "

Recent articles

" - for article in articles[:10]: + for article in articles: if article["article_readed"] == False: # not readed articles are in bold not_read_begin, not_read_end = "", "" @@ -629,23 +629,22 @@ class Root: html += "
\n" html += """All articles   """ % (feed["feed_id"],) - favs = [article for article in articles if article["article_like"] == True] - if len(favs) != 0: + + if self.mongo.nb_favorites(feed_id) != 0: html += "

Your favorites articles for this feed

" - for article in favs: - if article["like"] == True: - # descrition for the CSS ToolTips - article_content = utils.clear_string(article["article_content"]) - if article_content: - description = " ".join(article_content[:500].split(' ')[:-1]) - else: - description = "No description." + for article in self.mongo.get_favorites(feed_id): + #descrition for the CSS ToolTips + article_content = utils.clear_string(article["article_content"]) + if article_content: + description = " ".join(article_content[:500].split(' ')[:-1]) + else: + description = "No description." - # a description line per article (date, title of the article and - # CSS description tooltips on mouse over) - html += article["article_date"].ctime() + " - " + \ - """%s%s
\n""" % \ - (feed["feed_id"], article["article_id"], article["article_title"][:150], description) + # a description line per article (date, title of the article and + # CSS description tooltips on mouse over) + html += article["article_date"].ctime() + " - " + \ + """%s%s
\n""" % \ + (feed["feed_id"], article["article_id"], article["article_title"][:150], description) # This section enables the user to edit informations about -- cgit