from datetime import datetime, timedelta
import pymongo
from pymongo import MongoClient
client = MongoClient("localhost", 27017)
db = client.test_database
db.create_collection("test")
Collection(Database(MongoClient('localhost', 27017), 'test_database'), 'test')
db.collection_names()
['system.indexes', 'test']
db.test.insert({"users": [{"name": "Erik Bjäreholt", "nick": "erb"}]})
db.test.find_one({"users": [{"name": "Erik Bjäreholt", "nick": "erb"}]})
{'_id': ObjectId('5352b3e8a8a823033d84be6a'), 'users': [{'name': 'Erik Bjäreholt', 'nick': 'erb'}]}
db.drop_collection("test")
db.collection_names()
['system.indexes', 'lifelogger-sheet']
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)
{'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}