diff options
Diffstat (limited to 'source/mongodb.py')
-rw-r--r-- | source/mongodb.py | 134 |
1 files changed, 62 insertions, 72 deletions
diff --git a/source/mongodb.py b/source/mongodb.py index 68ccf5bc..b9e6686e 100644 --- a/source/mongodb.py +++ b/source/mongodb.py @@ -20,9 +20,9 @@ # along with this program. If not, see <http://www.gnu.org/licenses/> __author__ = "Cedric Bonhomme" -__version__ = "$Revision: 0.3 $" +__version__ = "$Revision: 0.4 $" __date__ = "$Date: 2012/03/03 $" -__revision__ = "$Date: 2012/05/01 $" +__revision__ = "$Date: 2012/12/02 $" __copyright__ = "Copyright (c) Cedric Bonhomme" __license__ = "GPLv3" @@ -82,7 +82,8 @@ class Articles(object): def get_all_feeds(self, condition=None): """ - Return all feeds object. + Return all feeds object. The returned list + is sorted by alphabetically (by feed name). """ feeds = [] collections = self.db.collection_names() @@ -97,35 +98,53 @@ class Articles(object): feeds.sort(key = lambda elem: elem['feed_title'].lower()) return feeds - def get_all_articles(self): - """ - Return articles of all feeds object (articles of all MongoDB articles collections). - All articles collections are of type 1. - """ - articles = [] - collections = self.db.collection_names() - for collection_name in collections: - collection = self.db[collection_name] - articles.extend(collection.find({'type':1})) - return articles + def get_articles(self, feed_id=None, article_id=None, condition=None, limit=1000000000): + """ + Return one or several articles. + The parameter "condition" is an optional requirement, for example: + get_articles(feed_id, condition=("article_readed", False)) will + return all unread articles of the feed 'feed_id'. + """ + if feed_id == None and article_id == None: + # Return all articles. + articles = [] + collections = self.db.collection_names() + for collection_name in collections: + collection = self.db[collection_name] + if condition is None: + articles.extend(collection.find({"type":1}, limit=limit)) + else: + articles.extend(collection.find({"type":1, condition[0]:condition[1]}, limit=limit)) + return articles - def get_article(self, feed_id, article_id): - """ - Get an article of a specified feed. - """ - collection = self.db[str(feed_id)] - return next(collection.find({"article_id":article_id})) + elif feed_id != None and article_id == None: + # Return all the articles of a collection. + collection = self.db[str(feed_id)] + if condition is None: + cursor = collection.find({"type":1}, limit=limit) + else: + cursor = collection.find({"type":1, condition[0]:condition[1]}, limit=limit) + return cursor.sort([("article_date", pymongo.DESCENDING)]) + + elif feed_id != None and article_id != None: + # Return a precise article. + collection = self.db[str(feed_id)] + return next(collection.find({"article_id":article_id})) - def get_articles_from_collection(self, feed_id, condition=None, limit=1000000000): + def get_favorites(self, feed_id=None): """ - Return all the articles of a collection. + Return favorites articles. """ - collection = self.db[str(feed_id)] - if condition is None: - cursor = collection.find({"type":1}, limit=limit) + 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.sort([("article_date", pymongo.DESCENDING)]) else: - cursor = collection.find({"type":1, condition[0]:condition[1]}, limit=limit) - return cursor.sort([("article_date", pymongo.DESCENDING)]) + favorites = [] + for feed_id in self.db.collection_names(): + favorites += self.get_favorites(feed_id) + return favorites def nb_articles(self, feed_id=None): """ @@ -142,32 +161,32 @@ class Articles(object): nb_articles += self.nb_articles(feed_id) return nb_articles - def get_favorites(self, feed_id=None): + def nb_unread_articles(self, feed_id=None): """ - Return favorites articles. + Return the number of unread articles of a feed + or of all the database. """ 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 - + return self.get_articles(feed_id=feed_id, condition=("article_readed", False)).count() + else: + return len(self.get_articles(condition=("article_readed", False))) + + def like_article(self, like, feed_id, article_id): + """ + Like or unlike an article. + """ + collection = self.db[str(feed_id)] + collection.update({"article_id": article_id}, {"$set": {"article_like": like}}) + def nb_favorites(self, feed_id=None): """ Return the number of favorites articles of a feed or of all the database. """ 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.count() + return self.get_favorites(feed_id).count() else: - # for all feeds - nb_favorites = 0 - for feed_id in self.db.collection_names(): - nb_favorites += self.nb_favorites(feed_id) - return nb_favorites + return len(self.get_favorites()) def nb_mail_notifications(self): """ @@ -180,28 +199,6 @@ class Articles(object): nb_mail_notifications += cursor.count() return nb_mail_notifications - def nb_unread_articles(self, feed_id=None): - """ - Return the number of unread articles of a feed - or of all the database. - """ - if feed_id is not None: - collection = self.db[feed_id] - cursor = collection.find({'article_readed':False}) - return cursor.count() - else: - unread_articles = 0 - for feed_id in self.db.collection_names(): - unread_articles += self.nb_unread_articles(feed_id) - return unread_articles - - def like_article(self, like, feed_id, article_id): - """ - Like or unlike an article. - """ - collection = self.db[str(feed_id)] - collection.update({"article_id": article_id}, {"$set": {"article_like": like}}) - def mark_as_read(self, readed, feed_id=None, article_id=None): """ """ @@ -222,13 +219,6 @@ class Articles(object): collection = self.db[str(feed_id)] collection.update({"type": 0, "feed_id":feed_id}, {"$set": changes}, multi=True) - def list_collections(self): - """ - List all collections (feed). - """ - collections = self.db.collection_names() - return collections - # Functions on database def drop_database(self): """ |