Skip to content

models/formdb.py

models.formdb

FormDB

FormDB

Bases: DBBase

Form Collection

Source code in models/formdb.py
class FormDB(DBBase):
    ''' Form Collection '''

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

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

        Indexs:
            - `case`
            - `pid`

        '''
        self.create_index([('case', 1), ])
        self.create_index([('pid', 1), ])

    def add_by_case(self, case: str, pid: str, uid: str, data: dict[str, Any]) -> dict[str, Any]:
        ''' Add data by case

        Args:
            case (str): Case name.
            pid (str): Project id.
            uid (str): User id.
            data (dict): The data to insert / update.

        Returns:
            Return the inserted / updated data.

        '''
        _data = {}
        for k in data:
            _data[f'data.{k}'] = data[k]

        return self.find_one_and_update(
            {'case': case, 'pid': pid, 'uid': uid},
            {'$set': _data},
            upsert=True,
            return_document=ReturnDocument.AFTER,
        )

add_by_case

add_by_case(
    case: str, pid: str, uid: str, data: dict[str, Any]
) -> dict[str, Any]

Add data by case

Parameters:

Name Type Description Default
case str

Case name.

required
pid str

Project id.

required
uid str

User id.

required
data dict

The data to insert / update.

required

Returns:

Type Description
dict[str, Any]

Return the inserted / updated data.

Source code in models/formdb.py
def add_by_case(self, case: str, pid: str, uid: str, data: dict[str, Any]) -> dict[str, Any]:
    ''' Add data by case

    Args:
        case (str): Case name.
        pid (str): Project id.
        uid (str): User id.
        data (dict): The data to insert / update.

    Returns:
        Return the inserted / updated data.

    '''
    _data = {}
    for k in data:
        _data[f'data.{k}'] = data[k]

    return self.find_one_and_update(
        {'case': case, 'pid': pid, 'uid': uid},
        {'$set': _data},
        upsert=True,
        return_document=ReturnDocument.AFTER,
    )

index

index() -> None

To make collection's index

Indexs
  • case
  • pid
Source code in models/formdb.py
def index(self) -> None:
    ''' To make collection's index

    Indexs:
        - `case`
        - `pid`

    '''
    self.create_index([('case', 1), ])
    self.create_index([('pid', 1), ])

FormTrafficFeeMappingDB

Bases: DBBase

Form traffic fee mapping Collection

Source code in models/formdb.py
class FormTrafficFeeMappingDB(DBBase):
    ''' Form traffic fee mapping Collection '''

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

    def save(self, pid: str, data: dict[str, Any]) -> dict[str, Any]:  # pylint: disable=arguments-differ
        ''' Save location / fee data

        Args:
            pid (str): Project id.
            data (dict): The data to insert / update.

                Format: `{'{location}': {fee}, ...}`

        Returns:
            Return the inserted / updated data.

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

save

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

Save location / fee data

Parameters:

Name Type Description Default
pid str

Project id.

required
data dict

The data to insert / update.

Format: {'{location}': {fee}, ...}

required

Returns:

Type Description
dict[str, Any]

Return the inserted / updated data.

Source code in models/formdb.py
def save(self, pid: str, data: dict[str, Any]) -> dict[str, Any]:  # pylint: disable=arguments-differ
    ''' Save location / fee data

    Args:
        pid (str): Project id.
        data (dict): The data to insert / update.

            Format: `{'{location}': {fee}, ...}`

    Returns:
        Return the inserted / updated data.

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