コード例 #1
0
    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()
コード例 #2
0
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
コード例 #3
0
ファイル: Experiments.py プロジェクト: joelliusp/SpaceHabit
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
コード例 #4
0
 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 = {}
コード例 #5
0
 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()
コード例 #6
0
ファイル: Test_Daily.py プロジェクト: joelliusp/SpaceHabit
 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()
コード例 #7
0
ファイル: Todo.py プロジェクト: joelliusp/SpaceHabit
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)])
コード例 #8
0
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")
コード例 #9
0
ファイル: Habit.py プロジェクト: joelliusczar/SpaceHabit
 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")
コード例 #10
0
  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 = {}
コード例 #11
0
ファイル: Good.py プロジェクト: joelliusp/SpaceHabit
 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")
コード例 #12
0
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
コード例 #13
0
ファイル: Zone.py プロジェクト: joelliusp/SpaceHabit
 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 = {}
コード例 #14
0
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]
コード例 #15
0
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]
コード例 #16
0
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]
コード例 #17
0
    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 = {}
コード例 #18
0
ファイル: Test_Daily.py プロジェクト: joelliusp/SpaceHabit
 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()
コード例 #19
0
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()})
コード例 #20
0
ファイル: Zone.py プロジェクト: joelliusp/SpaceHabit
  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
コード例 #21
0
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()
コード例 #22
0
    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
コード例 #23
0
  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
コード例 #24
0
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
コード例 #25
0
    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
コード例 #26
0
 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
コード例 #27
0
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
コード例 #28
0
def clean_up():
    """
    remove all the data from the test database
  """
    connection = DatabaseLayer.open_conn()
    connection.drop_database("test")
コード例 #29
0
ファイル: Habit.py プロジェクト: joelliusczar/SpaceHabit
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)])
コード例 #30
0
ファイル: Todo.py プロジェクト: joelliusczar/SpaceHabit
 def save_changes(self):
     DatabaseLayer.update_thing_by_id(self.id, COLLECTION_NAME,
                                      self._changes)
     self._changes = {}
コード例 #31
0
ファイル: Todo.py プロジェクト: joelliusczar/SpaceHabit
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)])
コード例 #32
0
ファイル: Good.py プロジェクト: joelliusp/SpaceHabit
 def save_changes(self):
   DatabaseLayer.update_thing_by_id(self.id,GoodsFields.COLLECTION_NAME,self._changes)
   self._changes = {}
コード例 #33
0
def clean_up():
  """
    remove all the data from the test database
  """
  connection = DatabaseLayer.open_conn()
  connection.drop_database("test")
コード例 #34
0
ファイル: Experiments.py プロジェクト: joelliusp/SpaceHabit
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")
コード例 #35
0
 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)
コード例 #36
0
ファイル: Experiments.py プロジェクト: joelliusp/SpaceHabit
def check_db_works():
        DatabaseLayer.open_conn()
        print(DatabaseLayer.get_open_connection().list_tables())
コード例 #37
0
def add_item_to_db():
    item = {'name': "John", 'lvl': 31}
    heros = DatabaseLayer.get_table("heros")
    id = heros.insert_one(item).inserted_id
    print(id)
コード例 #38
0
def trying_something_with_the_db_layer_and_globals():
    a = DatabaseLayer.get_table("heros")
    b = DatabaseLayer.get_table("dailies")
コード例 #39
0
def check_db_works():
    DatabaseLayer.open_conn()
    print(DatabaseLayer.get_open_connection().list_tables())
コード例 #40
0
ファイル: Experiments.py プロジェクト: joelliusp/SpaceHabit
def trying_something_with_the_db_layer_and_globals():
    a = DatabaseLayer.get_table("heros")
    b = DatabaseLayer.get_table("dailies")
コード例 #41
0
 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)
コード例 #42
0
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
コード例 #43
0
ファイル: Daily.py プロジェクト: joelliusp/SpaceHabit
 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)
コード例 #44
0
ファイル: Experiments.py プロジェクト: joelliusp/SpaceHabit
def add_item_to_db():
    item = {'name':"John",
            'lvl':31}
    heros = DatabaseLayer.get_table("heros")
    id = heros.insert_one(item).inserted_id
    print(id)