Skip to content

models/base.py

models.base

DB base

The base module for connect to MongoDB. In testing mode, we use the mongomock for mock data and need set the MONGO_MOCK to be True in setting.py.

DBBase

Bases: Collection

DBBase class

Parameters:

Name Type Description Default
name str

collection name.

required
Source code in models/base.py
class DBBase(Collection):  # pylint: disable=abstract-method
    ''' DBBase class

    Args:
        name (str): collection name.

    '''

    def __init__(self, name: str) -> None:
        if not setting.MONGO_MOCK:
            client = MongoClient(
                f'mongodb://{setting.MONGO_HOST}:{setting.MONGO_PORT}')[setting.MONGO_DBNAME]
            super_args = {'database': client, 'name': name}
        else:
            client = mongomock.MongoClient()['testing']
            super_args = {'database': client, 'name': name,
                          '_db_store': MOCK_DB_STORE}

        super().__init__(**super_args)

    @staticmethod
    def make_create_at(data: dict[str, Any]) -> None:
        ''' make `create_at` timestamp

        Args:
            data (dict): make the timestamp into `create_at` field

        '''
        data['created_at'] = time()

make_create_at staticmethod

make_create_at(data: dict[str, Any]) -> None

make create_at timestamp

Parameters:

Name Type Description Default
data dict

make the timestamp into create_at field

required
Source code in models/base.py
@staticmethod
def make_create_at(data: dict[str, Any]) -> None:
    ''' make `create_at` timestamp

    Args:
        data (dict): make the timestamp into `create_at` field

    '''
    data['created_at'] = time()