from datetime import datetime, timedelta import pymongo from pymongo import MongoClient client = MongoClient("localhost", 27017) db = client.test_database db.create_collection("test") db.collection_names() db.test.insert({"users": [{"name": "Erik Bjäreholt", "nick": "erb"}]}) db.test.find_one({"users": [{"name": "Erik Bjäreholt", "nick": "erb"}]}) db.drop_collection("test") db.collection_names() db.drop_collection("lifelogger-users") db.drop_collection("lifelogger-sheets") users = db.create_collection("lifelogger-users") sheets = db.create_collection("lifelogger-sheets") user_erb = users.insert({"username": "erb", "name": "Erik Bjäreholt", "email": "erik.bjareholt@gmail.com"}) user_tester = users.insert({"username": "tester", "name": "Test McTest", "email": "tester@example.com"}) user_erb = users.find_one({"username": "erb"}) class Sheet(): """Baseclass for all logging-sheets""" def __init__(self, user, year=None, month=None): documents = sheets.find({"username": user["username"]}) if year and month: d = datetime(year, month, 1) elif year or month: raise ValueError("You must specify both year and month, or neither.") else: d = datetime.now() found = False for document in documents: if document and document["year"] == d.year and document["month"] == d.month: found = True break if not found: document = {"username": user["username"], "columns": {"mood": {"08:00": [], "12:00": [], "20:00": []}, "productivity": {"08:00": [], "12:00": [], "20:00": []}, "exercise": {"running": [], "gym": [], "home": []}}, "year": d.year, "month": d.month} sheets.insert(document) self.data = document def __repr__(self): return str(self.data) sheet_erb = Sheet(user_erb) sheet_erb_2013_1 = Sheet(user_erb, year=2013, month=1) for sheet in sheets.find(): print(sheet)