In [48]:
from datetime import datetime, timedelta

import pymongo
from pymongo import MongoClient
In [4]:
client = MongoClient("localhost", 27017)
db = client.test_database
In [9]:
db.create_collection("test")
Out[9]:
Collection(Database(MongoClient('localhost', 27017), 'test_database'), 'test')
In [10]:
db.collection_names()
Out[10]:
['system.indexes', 'test']
In [29]:
db.test.insert({"users": [{"name": "Erik Bjäreholt", "nick": "erb"}]})
db.test.find_one({"users": [{"name": "Erik Bjäreholt", "nick": "erb"}]})
Out[29]:
{'_id': ObjectId('5352b3e8a8a823033d84be6a'),
 'users': [{'name': 'Erik Bjäreholt', 'nick': 'erb'}]}
In [39]:
db.drop_collection("test")
db.collection_names()
Out[39]:
['system.indexes', 'lifelogger-sheet']
In [40]:
db.drop_collection("lifelogger-users")
db.drop_collection("lifelogger-sheets")
In [41]:
users = db.create_collection("lifelogger-users")
sheets = db.create_collection("lifelogger-sheets")
In [45]:
user_erb = users.insert({"username": "erb", "name": "Erik Bjäreholt", "email": "[email protected]"})
user_tester = users.insert({"username": "tester", "name": "Test McTest", "email": "[email protected]"})
In [65]:
user_erb = users.find_one({"username": "erb"})
In [104]:
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)
In [105]:
sheet_erb = Sheet(user_erb)
sheet_erb_2013_1 = Sheet(user_erb, year=2013, month=1)
In [106]:
for sheet in sheets.find():
    print(sheet)
{'columns': {'productivity': {'08:00': [], '20:00': [], '12:00': []}, 'exercise': {'running': [], 'gym': [], 'home': []}, 'mood': {'08:00': [], '20:00': [], '12:00': []}}, 'year': 2014, 'username': 'erb', '_id': ObjectId('5352bb47a8a823033d84be73'), 'month': 4}
{'columns': {'productivity': {'08:00': [], '20:00': [], '12:00': []}, 'exercise': {'running': [], 'gym': [], 'home': []}, 'mood': {'08:00': [], '20:00': [], '12:00': []}}, 'year': 2013, 'username': 'erb', '_id': ObjectId('5352bcbca8a823033d84be74'), 'month': 1}
In [ ]: