From 96b2bd95da11e40c974f2d1cdfe262ad2b36b54b Mon Sep 17 00:00:00 2001 From: cedricbonhomme Date: Sat, 3 Mar 2012 11:57:14 +0100 Subject: Draft of the MongoDB implementation. --- mongodb.py | 129 +++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 87 insertions(+), 42 deletions(-) (limited to 'mongodb.py') diff --git a/mongodb.py b/mongodb.py index 851db945..43ed851e 100644 --- a/mongodb.py +++ b/mongodb.py @@ -4,13 +4,13 @@ __author__ = "Cedric Bonhomme" __version__ = "$Revision: 0.1 $" __date__ = "$Date: 2012/03/03 $" -__date__ = "$Date: 2012/03/03 $" +__revision__ = "$Date: 2012/03/03 $" __copyright__ = "Copyright (c) Cedric Bonhomme" __license__ = "GPLv3" import time -from pymongo.connection import Connection +import pymongo class Articles(object): """ @@ -19,70 +19,57 @@ class Articles(object): """ Instantiates the connection. """ - self.connection = Connection(url, port) + self.connection = pymongo.connection.Connection(url, port) self.db = self.connection.pyaggr3g470r - def add_collection(self, collection): + def add_collection(self, new_collection): """ Creates a new collection for a new feed. """ - collection = self.db.collection.feed_id - collection.insert(collection) + name = str(new_collection["collection_id"]) + pymongo.collection.Collection(self.db, name) + collection = self.db[name] + collection.create_index([("article_link", pymongo.ASCENDING)], {"unique":True, "sparse":True}) + collection.insert(new_collection) - def add_article(self, article, collection_id): + def add_articles(self, articles, collection_id): """ - Add an article in a collection. + Add article(s) in a collection. """ - collection = self.db.collection_id - cursor = collection.find({"article_id":article.article_id}) - if cursor.count() == 0: - collection.insert(user_dic) + collection = self.db[str(collection_id)] + for article in articles: + cursor = collection.find({"article_id":article["article_id"]}) + if cursor.count() == 0: + collection.insert(article) - def get_all_articles(): + def get_all_articles(self): """ Return all articles from all collections. """ articles = [] - collections = self.db.collection_names + collections = self.db.collection_names() for colliection in collections: collection = self.db.collection_id articles.append(collection) return articles - def get_articles_from_collection(): + def get_articles_from_collection(self, collection_id): """ Return all the articles of a collection. """ - collection = self.db.collection_id + collection = self.db[str(collection_id)] return collection - - - # - # Collection: users - # - def register_user(self, sender_uuid, user): - """ - Insert a new user in the collection of users. - """ - user_dic = {"uuid":sender_uuid, "name":user, \ - "time-registration":time.time()} - - collection = self.db.users - cursor = collection.find({"uuid":sender_uuid}) - if cursor.count() == 0: - collection.insert(user_dic) - - - def print_users(self): + def print_articles_from_collection(self, collection_id): """ - List and print the users. + Print the articles of a collection. """ - collection = self.db.users - cursor = collection.find() + collection = self.db[str(collection_id)] + cursor = collection.find({"type":1}) + print "Article for the collection", collection_id for d in cursor: print d - + def nb_users(self): """ Return the number of users. @@ -90,12 +77,70 @@ class Articles(object): collection = self.db.users collection.count() - + def list_collections(self): + """ + List all collections (feed). + """ + collections = self.db.collection_names() + return collections + # Functions on database - def drop_wsc_database(self): + def drop_database(self): + """ + Drop all the database + """ self.connection.drop_database('pyaggr3g470r') if __name__ == "__main__": # Point of entry in execution mode. - articles_database = Articles() \ No newline at end of file + articles = Articles() + + + # Create a collection for a stream + collection_dic = {"type": 0, \ + "collection_id": 42,\ + "feed_image": "Image", \ + "feed_title": "Title", \ + "feed_link": "Link", \ + "site_title": "Site link", \ + "mail": True, \ + } + + articles.add_collection(collection_dic) + + + + # Add an article in the newly created collection + article_dic1 = {"type": 1, \ + "article_id": 51, \ + "article_date": "Today", \ + "article_link": "Link of the article", \ + "article_title": "The title", \ + "article_content": "The content of the article", \ + "article_readed": True, \ + "article_like": True \ + } + + article_dic2 = {"type": 1, \ + "article_id": 52, \ + "article_date": "Yesterday", \ + "article_link": "Link", \ + "article_title": "Hello", \ + "article_content": "The content of the article", \ + "article_readed": True, \ + "article_like": True \ + } + + articles.add_articles([article_dic1, article_dic2], 42) + + + # Print articles of the collection + articles.print_articles_from_collection(42) + + + print + print articles.list_collections() + + # Drop the database + articles.drop_database() \ No newline at end of file -- cgit