def setUp(self): accountSet = DatabaseLayer.get_table(TABLE_ACCOUNT) dailySet = DatabaseLayer.get_table(TABLE_DAILIES) self.accountCount0 = accountSet.find({}).count() self.dailiesCount0 = dailySet.find({}).count() self.accountId = accountSet.insert_one({'test': 0}).inserted_id testList = [] self.controlList = [] for i in range(0, 500): u = i // 100 r = (500 - i) // 25 f = i tObj = { 'ownerAccountId': self.accountId, 'daysUntilTrigger': u, 'urgency': r, 'difficulty': f, 'isCompleted': False } testList.append(tObj) self.controlList.append(tObj) random.shuffle(testList) for d in testList: id = dailySet.insert_one(d).inserted_id print(id) print("done") return super().setUp()
def find_out_what_im_getting_back_from_db(): for i in range(0, 1000): obj = {'a': i, 'b': i % 10, 'c': "test"} DatabaseLayer.insert_thing(obj, "test") s = DatabaseLayer.get_sorted_stuff_by_search({}, "test") pass
def find_out_what_im_getting_back_from_db(): for i in range(0,1000): obj = {'a':i,'b':i % 10, 'c': "test"} DatabaseLayer.insert_thing(obj,"test") s = DatabaseLayer.get_sorted_stuff_by_search({},"test") pass
def save_changes(self, heroId): """ args: heroId: this needs to be an pymongo objectId. It is used as an owner relationship to a hero model """ from AllDBFields import HeroDbFields ownerCollection = DatabaseLayer.get_table( self.get_dbFields().OWNER_COLLECTION) if self.get_pk(): if self._changes: ownerCollection.update_one( {self.get_dbFields().PK_KEY: heroId}, {'$set': self._changes}) else: collection = DatabaseLayer.get_table( self.get_dbFields().COLLECTION_NAME) pk = collection.insert_one(self.dict).inserted_id self.dict[self.get_dbFields().PK_KEY] = pk nestedZone = {HeroDbFields.ZONE: self.dict} ownerCollection.update_one({self.get_dbFields().PK_KEY: heroId}, {'$set': nestedZone}) self._changes = {}
def tearDown(self): accountSet = DatabaseLayer.get_table(TABLE_ACCOUNT) dailySet = DatabaseLayer.get_table(TABLE_DAILIES) result = dailySet.delete_many({'ownerAccountId': self.accountId}) accountSet.delete_one({'_id': self.accountId}) accountCount1 = accountSet.find({}).count() dailiesCount1 = dailySet.find({}).count() self.assertEqual(accountCount1, self.accountCount0) self.assertEqual(dailiesCount1, self.dailiesCount0) return super().tearDown()
def tearDown(self): accountSet = DatabaseLayer.get_table(TABLE_ACCOUNT) dailySet = DatabaseLayer.get_table(TABLE_DAILIES) result = dailySet.delete_many( {'ownerAccountId':self.accountId}) accountSet.delete_one({'_id':self.accountId}) accountCount1 = accountSet.find({}).count() dailiesCount1 = dailySet.find({}).count() self.assertEqual(accountCount1,self.accountCount0) self.assertEqual(dailiesCount1,self.dailiesCount0) return super().tearDown()
def get_todos_by_account(id): import pymongo return DatabaseLayer.get_sorted_stuff_by_search({ID_KEY:id},COLLECTION_NAME,[ (DUE_DATE,pymongo.ASCENDING), (URGENCY,pymongo.DESCENDING), (DIFFICULTY,pymongo.ASCENDING) (EFFECTIVE_DATE,pymongo.ASCENDING)])
def get_non_existent_item_from_db(): a = DatabaseLayer.get_stuff_by_id(ObjectId("5733fa75ec00000000000000"), "heros") if not a: print("not A") if a is None: print("A is none")
def __init__(self,dict=None,id = None): """Priority is given to dictionary object over id """ self._changes = {} if dict: self._dict = dict return if id: self._dict = DatabaseLayer.get_thing_by_id(id,COLLECTION_NAME) return raise ValueError("Either a reference to a dictionary or an id is required")
def save_changes(self): collection = DatabaseLayer.get_table(self.get_dbFields().COLLECTION_NAME) if self.get_pk(): if self._changes: collection.update_one({self.get_dbFields().PK_KEY:self.get_pk()},{'$set':self._changes}) else: pk = collection.insert_one(self.dict).inserted_id self.dict[self.get_dbFields().PK_KEY] = pk self._changes = {}
def __init__(self,dict=None,id = None): """Priority is given to dictionary object over id """ self._changes = {} if dict: self._dict = dict return if id: self._dict = DatabaseLayer.get_thing_by_id(id,GoodsFields.COLLECTION_NAME) return raise ValueError("Either a reference to a dictionary or an id is required")
def create_login_using_test_values(): collection = DatabaseLayer.get_table(authFields.COLLECTION_NAME) pk = collection.insert_one({ authFields.USER_LOGIN: "******", authFields.USER_DESC: "[email protected]", authFields.USER_PASSWORD: CryptKeeper.encrypt_str("123456") }).inserted_id return pk
def save_changes(self,heroId): """ args: heroId: this needs to be an pymongo objectId. It is used as an owner relationship to a hero model """ from AllDBFields import HeroDbFields ownerCollection = DatabaseLayer.get_table(self.get_dbFields().OWNER_COLLECTION) if self.get_pk(): if self._changes: ownerCollection.update_one({self.get_dbFields().PK_KEY:heroId},{'$set':self._changes}) else: collection = DatabaseLayer.get_table(self.get_dbFields().COLLECTION_NAME) pk = collection.insert_one(self.dict).inserted_id self.dict[self.get_dbFields().PK_KEY] = pk nestedZone = {HeroDbFields.ZONE:self.dict} ownerCollection.update_one({self.get_dbFields().PK_KEY:heroId},{'$set':nestedZone}) self._changes = {}
def get_accountPk_by_loginPk(loginPk): """ args: loginPk: an fk to the user collection return: an objectId to the account collection """ from AllDBFields import AccountDbFields collection = DatabaseLayer.get_table(AccountDbFields.COLLECTION_NAME) account = collection.find_one({AccountDbFields.LOGIN_PK_KEY:loginPk}) return account[AccountDbFields.PK_KEY]
def get_heroPk_by_accountPk(accountPk): """ args: userId: an fk to the account collection return: an objectId to the hero collection """ from AllDBFields import HeroDbFields collection = DatabaseLayer.get_table(HeroDbFields.COLLECTION_NAME) hero = collection.find_one({HeroDbFields.ACCOUNT_PK_KEY:accountPk}) return hero[HeroDbFields.PK_KEY]
def get_loginPk_by_login(validLogin): """ args: validLogin: I'm gonna assume that this login has already been vetted earlier in the program. return: an objectId to the users collection """ collection = DatabaseLayer.get_table(AuthenticationFields.COLLECTION_NAME) login = collection.find_one({AuthenticationFields.USER_LOGIN: validLogin}) return login[BaseFields.PK_KEY]
def save_changes(self): collection = DatabaseLayer.get_table( self.get_dbFields().COLLECTION_NAME) if self.get_pk(): if self._changes: collection.update_one( {self.get_dbFields().PK_KEY: self.get_pk()}, {'$set': self._changes}) else: pk = collection.insert_one(self.dict).inserted_id self.dict[self.get_dbFields().PK_KEY] = pk self._changes = {}
def setUp(self): accountSet = DatabaseLayer.get_table(TABLE_ACCOUNT) dailySet = DatabaseLayer.get_table(TABLE_DAILIES) self.accountCount0 = accountSet.find({}).count() self.dailiesCount0 = dailySet.find({}).count() self.accountId = accountSet.insert_one({'test':0}).inserted_id testList = [] self.controlList = [] for i in range(0,500): u = i // 100 r = (500 -i) // 25 f = i tObj = {'ownerAccountId':self.accountId,'daysUntilTrigger':u,'urgency':r,'difficulty':f,'isCompleted':False} testList.append(tObj) self.controlList.append(tObj) random.shuffle(testList) for d in testList: id = dailySet.insert_one(d).inserted_id print(id) print("done") return super().setUp()
def get_user(login): """ gets the user data from the database args: this should be an email address returns: a dict containing, the user email address in forced lower case, the users encrypted password, and the user's email address in whatever case they saved it as. """ collection = DatabaseLayer.get_table(AuthenticationFields.COLLECTION_NAME) return collection.find_one({AuthenticationFields.USER_LOGIN:login.lower()})
def construct_model_from_pk(cls,pk): """ args: id: uses the id to load this model from the database. return: an instance of the model on which this is called """ collection = DatabaseLayer.get_table(cls.get_dbFields().COLLECTION_NAME) obj = cls(None) obj.dict = collection.find_one({cls.get_dbFields().PK_KEY:pk}) return obj
def get_record_count_from_table(tableName): """ this will usually be used to count how many records were inserted into the database args: tableName: the name of the table that we want to count records from returns: the record count in our collection of interest """ collection = DatabaseLayer.get_table(tableName) return collection.find({}).count()
def construct_model_from_pk(cls, pk): """ args: id: uses the id to load this model from the database. return: an instance of the model on which this is called """ collection = DatabaseLayer.get_table( cls.get_dbFields().COLLECTION_NAME) obj = cls(None) obj.dict = collection.find_one({cls.get_dbFields().PK_KEY: pk}) return obj
def construct_model_from_pk(cls,pk): """ args: id: uses the id to load this model from the database. return: an instance of the model on which this is called """ if not cls.get_dbFields().COLLECTION_NAME: raise NotImplementedError("This needs a collection name to work") collection = DatabaseLayer.get_table(cls.get_dbFields().COLLECTION_NAME) obj = cls() obj.dict = collection.find_one({cls.get_dbFields().PK_KEY:pk}) return obj
def is_login_taken(login): """ checks the database to determine if an email address has already been used. args: login: this should be an email address returns: a boolean. true if email is already used. false if not. """ collection = DatabaseLayer.get_table(AuthenticationFields.COLLECTION_NAME) if collection.find({AuthenticationFields.USER_LOGIN:login.lower()})\ .count() > 0: return True else: return False
def construct_model_from_pk(cls, pk): """ args: id: uses the id to load this model from the database. return: an instance of the model on which this is called """ if not cls.get_dbFields().COLLECTION_NAME: raise NotImplementedError("This needs a collection name to work") collection = DatabaseLayer.get_table( cls.get_dbFields().COLLECTION_NAME) obj = cls() obj.dict = collection.find_one({cls.get_dbFields().PK_KEY: pk}) return obj
def create_new_account_in_db(cls,loginPk=None): account = { AccountDbFields.LOGIN_PK_KEY: loginPk, AccountDbFields.REMINDER_TIME: -1, AccountDbFields.DAY_START:0, AccountDbFields.DEATH_GOLD_PENALTY: .25, AccountDbFields.HERO_LVL_PENALTY: 0, AccountDbFields.ZONE_LVL_PENALTY: "LVLRESTART", AccountDbFields.PERMA_DEATH: False, AccountDbFields.PUBLIC_ACCOUNT: False, AccountDbFields.PUBLIC_KEY: uuid.uuid4().hex, AccountDbFields.CREATE_DATE: datetime.utcnow().timestamp(), AccountDbFields.PREVENT_POPUPS: False } collection = DatabaseLayer.get_table(cls.get_dbFields().COLLECTION_NAME) pk = collection.insert_one(account).inserted_id return pk
def safe_insert_new_user(login,pw): """ This used during the create new user process. this should be called when doing the actual inserting of a new user. This encrypts the password before saving. args: login: unique email address supplied by the user pw: the unencrypted password supplied by the user. returns: the primary key returned from the Database upon insertion of the new user """ safePw = CryptKeeper.encrypt_str(pw) collection = DatabaseLayer.get_table(AuthenticationFields.COLLECTION_NAME) id = collection.insert_one({AuthenticationFields.USER_LOGIN:login.lower(), AuthenticationFields.USER_PASSWORD:safePw, AuthenticationFields.USER_DESC: login}).inserted_id return id
def clean_up(): """ remove all the data from the test database """ connection = DatabaseLayer.open_conn() connection.drop_database("test")
def get_habits_by_account(id): import pymongo return DatabaseLayer.get_sorted_stuff_by_search({ID_KEY:id},COLLECTION_NAME,[(TRIGGER_FREQUENCY,pymongo.DESCENDING), (URGENCY,pymongo.DESCENDING), (DIFFICULTY,pymongo.ASCENDING)])
def save_changes(self): DatabaseLayer.update_thing_by_id(self.id, COLLECTION_NAME, self._changes) self._changes = {}
def get_todos_by_account(id): import pymongo return DatabaseLayer.get_sorted_stuff_by_search( {ID_KEY: id}, COLLECTION_NAME, [(DUE_DATE, pymongo.ASCENDING), (URGENCY, pymongo.DESCENDING), (DIFFICULTY, pymongo.ASCENDING)(EFFECTIVE_DATE, pymongo.ASCENDING)])
def save_changes(self): DatabaseLayer.update_thing_by_id(self.id,GoodsFields.COLLECTION_NAME,self._changes) self._changes = {}
def clean_up(): """ remove all the data from the test database """ connection = DatabaseLayer.open_conn() connection.drop_database("test")
def get_non_existent_item_from_db(): a = DatabaseLayer.get_stuff_by_id(ObjectId("5733fa75ec00000000000000"),"heros") if not a: print("not A") if a is None: print("A is none")
def get_dailies_by_account(cls, accountPk, isCompleted=False): collection = DatabaseLayer.get_table( cls.get_dbFields().COLLECTION_NAME) return collection.find({cls.get_dbFields().ACCOUNT_PK_KEY:accountPk,'isCompleted':isCompleted})\ .sort(DailyDbFields.SORT_KEY,1)
def check_db_works(): DatabaseLayer.open_conn() print(DatabaseLayer.get_open_connection().list_tables())
def add_item_to_db(): item = {'name': "John", 'lvl': 31} heros = DatabaseLayer.get_table("heros") id = heros.insert_one(item).inserted_id print(id)
def trying_something_with_the_db_layer_and_globals(): a = DatabaseLayer.get_table("heros") b = DatabaseLayer.get_table("dailies")
def check_db_works(): DatabaseLayer.open_conn() print(DatabaseLayer.get_open_connection().list_tables())
def trying_something_with_the_db_layer_and_globals(): a = DatabaseLayer.get_table("heros") b = DatabaseLayer.get_table("dailies")
def test_safe_insert_new_user(self): pk = auth.safe_insert_new_user("a","123") users = DatabaseLayer.get_table(fields.COLLECTION_NAME) user = users.find_one({BaseFields.PK_KEY:pk}) self.assertIsNotNone(user)
def create_login_using_test_values(): collection = DatabaseLayer.get_table(authFields.COLLECTION_NAME) pk = collection.insert_one({authFields.USER_LOGIN:"******",authFields.USER_DESC: "[email protected]",authFields.USER_PASSWORD:CryptKeeper.encrypt_str("123456")}).inserted_id return pk
def get_dailies_by_account(cls,accountPk,isCompleted=False): collection = DatabaseLayer.get_table(cls.get_dbFields().COLLECTION_NAME) return collection.find({cls.get_dbFields().ACCOUNT_PK_KEY:accountPk,'isCompleted':isCompleted})\ .sort(DailyDbFields.SORT_KEY,1)
def add_item_to_db(): item = {'name':"John", 'lvl':31} heros = DatabaseLayer.get_table("heros") id = heros.insert_one(item).inserted_id print(id)