跳轉到

models/users_db.py

models.users_db

User database

PolicySignedDB

Bases: DBBase

PolicySigned Collection

Source code in models/users_db.py
class PolicySignedDB(DBBase):
    ''' PolicySigned Collection '''

    def __init__(self) -> None:
        super().__init__('policy_signed')

    def index(self) -> None:
        ''' To make collection's index

        Indexs:
            - `type`, `sign_at`

        '''
        self.create_index([('type', 1), ('sign_at', -1), ])

    def save(self, uid: str, _type: PolicyType) -> InsertOneResult:
        ''' Save the signed data

        Args:
            uid (str): user id
            _type (PolicyType): Ploicy type

        '''
        return self.insert_one(PolicySigned(uid=uid, type=_type).dict())

index

index() -> None

To make collection's index

Indexs
  • type, sign_at
Source code in models/users_db.py
def index(self) -> None:
    ''' To make collection's index

    Indexs:
        - `type`, `sign_at`

    '''
    self.create_index([('type', 1), ('sign_at', -1), ])

save

save(uid: str, _type: PolicyType) -> InsertOneResult

Save the signed data

Parameters:

Name Type Description Default
uid str

user id

required
_type PolicyType

Ploicy type

required
Source code in models/users_db.py
def save(self, uid: str, _type: PolicyType) -> InsertOneResult:
    ''' Save the signed data

    Args:
        uid (str): user id
        _type (PolicyType): Ploicy type

    '''
    return self.insert_one(PolicySigned(uid=uid, type=_type).dict())

TobeVolunteerDB

Bases: DBBase

TobeVolunteer Collection

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

    def __init__(self) -> None:
        super().__init__('tobe_volunteer')

    def add(self, data: dict[str, Any]) -> None:
        ''' add

        Args:
            data (dict): The data to insert / update.

        '''
        self.find_one_and_update(
            {'_id': data['uid']},
            {'$set': data},
            upsert=True,
            return_document=ReturnDocument.AFTER,
        )

add

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

add

Parameters:

Name Type Description Default
data dict

The data to insert / update.

required
Source code in models/users_db.py
def add(self, data: dict[str, Any]) -> None:
    ''' add

    Args:
        data (dict): The data to insert / update.

    '''
    self.find_one_and_update(
        {'_id': data['uid']},
        {'$set': data},
        upsert=True,
        return_document=ReturnDocument.AFTER,
    )

UsersDB

Bases: DBBase

UsersDB Collection

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

    def __init__(self) -> None:
        super().__init__('users')

    def index(self) -> None:
        ''' To make collection's index

        Indexs:
            - `mail`
            - `property.suspend`

        '''
        self.create_index([('mail', 1), ])
        self.create_index([('property.suspend', 1), ])

    @staticmethod
    def new(mail: str) -> dict[str, Any]:
        ''' New user account

        Args:
            mail (str): Mail address.

        Returns:
            Return the user base object and the `_id` it will be the user id.

        TODO:
            ``mail`` bind to login oauth account. Maybe need ``alias`` for
            some case like one user have more mail account to register.

        '''
        return {
            '_id': f'{uuid4().fields[0]:08x}',
            'mail': mail,
            'created_at': int(time()),
        }

    def add(self, data: dict[str, Any]) -> dict[str, Any]:
        ''' Add data

        Args:
            data (dict): The data to insert / update.

        Returns:
            Return the inserted / updated data.

        '''
        return self.find_one_and_update(
            {'_id': data['_id']},
            {'$set': data},
            upsert=True,
            return_document=ReturnDocument.AFTER,
        )

add

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

Add data

Parameters:

Name Type Description Default
data dict

The data to insert / update.

required

Returns:

Type Description
dict[str, Any]

Return the inserted / updated data.

Source code in models/users_db.py
def add(self, data: dict[str, Any]) -> dict[str, Any]:
    ''' Add data

    Args:
        data (dict): The data to insert / update.

    Returns:
        Return the inserted / updated data.

    '''
    return self.find_one_and_update(
        {'_id': data['_id']},
        {'$set': data},
        upsert=True,
        return_document=ReturnDocument.AFTER,
    )

index

index() -> None

To make collection's index

Indexs
  • mail
  • property.suspend
Source code in models/users_db.py
def index(self) -> None:
    ''' To make collection's index

    Indexs:
        - `mail`
        - `property.suspend`

    '''
    self.create_index([('mail', 1), ])
    self.create_index([('property.suspend', 1), ])

new staticmethod

new(mail: str) -> dict[str, Any]

New user account

Parameters:

Name Type Description Default
mail str

Mail address.

required

Returns:

Type Description
dict[str, Any]

Return the user base object and the _id it will be the user id.

TODO

mail bind to login oauth account. Maybe need alias for some case like one user have more mail account to register.

Source code in models/users_db.py
@staticmethod
def new(mail: str) -> dict[str, Any]:
    ''' New user account

    Args:
        mail (str): Mail address.

    Returns:
        Return the user base object and the `_id` it will be the user id.

    TODO:
        ``mail`` bind to login oauth account. Maybe need ``alias`` for
        some case like one user have more mail account to register.

    '''
    return {
        '_id': f'{uuid4().fields[0]:08x}',
        'mail': mail,
        'created_at': int(time()),
    }