Skip to content

module/form.py

module.form

Form

Form

Form Object

Source code in module/form.py
class Form:  # pylint: disable=too-many-public-methods
    ''' Form Object '''
    @staticmethod
    def update_appreciation(pid: str, uid: str, data: dict[str, Any]) -> dict[str, Any]:
        ''' Update appreciation

        Args:
            pid (str): Project id.
            uid (str): User id.
            data (dict): The data to update.

                - `available`: `bool`.
                - `key`: In one of `oauth`, `badge_name`, `real_name`.
                - `value`: As user name.

        Returns:
            Return the added data.

        TODO:
            Need refactor in pydantic.

        '''
        return FormDB().add_by_case(case='appreciation', pid=pid, uid=uid, data=data)

    @staticmethod
    def get_appreciation(pid: str, uid: str) -> Optional[dict[str, Any]]:
        ''' Get appreciation

        Args:
            pid (str): Project id.
            uid (str): User id.

        Returns:
            The data or `None`.

        '''
        return FormDB().find_one({'case': 'appreciation', 'pid': pid, 'uid': uid})

    @staticmethod
    def all_appreciation(pid: str) -> Generator[dict[str, Any], None, None]:
        ''' Get all appreciation

        Args:
            pid (str): Project id.

        Yields:
            Return the data in `pid`.

        '''
        yield from FormDB().find({'case': 'appreciation', 'pid': pid})

    @staticmethod
    def update_volunteer_certificate(pid: str, uid: str, data: dict[str, Any]) -> dict[str, Any]:
        ''' Update volunteer certificate

        Args:
            pid (str): Project id.
            uid (str): User id.
            data (dict): The data to update.

                - `value`: `bool`.

        Returns:
            Return the added data.

        TODO:
            Need refactor in pydantic.

        '''
        return FormDB().add_by_case(case='volunteer_certificate', pid=pid, uid=uid, data=data)

    @staticmethod
    def get_volunteer_certificate(pid: str, uid: str) -> Optional[dict[str, Any]]:
        ''' Get volunteer certificate

        Args:
            pid (str): Project id.
            uid (str): User id.

        Returns:
            The data or `None`.

        '''
        return FormDB().find_one({'case': 'volunteer_certificate', 'pid': pid, 'uid': uid})

    @staticmethod
    def all_volunteer_certificate(pid: str) -> Generator[dict[str, Any], None, None]:
        ''' Get All volunteer certificate

        Args:
            pid (str): Project id.

        Yields:
            Return the data in `pid`.

        '''
        yield from FormDB().find({'case': 'volunteer_certificate', 'pid': pid})

    @staticmethod
    def update_traffic_fee(pid: str, uid: str, data: dict[str, Any]) -> dict[str, Any]:
        ''' Update traffic fee

        Args:
            pid (str): Project id.
            uid (str): User id.
            data (dict): The data to update.

                - `apply`: `bool`.
                - `fee`: `int`.
                - `fromwhere`: `str`.
                - `howto`: `str`.

        Returns:
            Return the added data.

        TODO:
            Need refactor in pydantic.

        '''
        return FormDB().add_by_case(case='traffic_fee', pid=pid, uid=uid, data=data)

    @staticmethod
    def get_traffic_fee(pid: str, uid: str) -> Optional[dict[str, Any]]:
        ''' Get traffic fee

        Args:
            pid (str): Project id.
            uid (str): User id.

        Returns:
            The data or `None`.

        '''
        return FormDB().find_one({'case': 'traffic_fee', 'pid': pid, 'uid': uid})

    @staticmethod
    def all_traffic_fee(pid: str) -> Generator[dict[str, Any], None, None]:
        ''' Get All traffic_fee

        Args:
            pid (str): Project id.

        Yields:
            Return the data in `pid`.

        '''
        yield from FormDB().find({'case': 'traffic_fee', 'pid': pid})

    @staticmethod
    def update_accommodation(pid: str, uid: str, data: dict[str, Any]) -> dict[str, Any]:
        ''' Update accommodation

        Args:
            pid (str): Project id.
            uid (str): User id.
            data (dict): The data to update.

                - `key`: In one of `yes-longtraffic`, `yes`, `no`.
                - `status`: `bool`.
                - `room`: Optional. The room numbers.
                - `room_key`: Optional. The key for exchange the place for
                              some users want live together.
                - `mixed`: (bool) accepted in mixed room.

        Returns:
            Return the added data.

        See Also:
            The more about how to use the `room_key` to exchange the `room`,
                please move to [module.form.FormAccommodation][].

        TODO:
            Need refactor in pydantic.

        '''
        return FormDB().add_by_case(case='accommodation', pid=pid, uid=uid, data=data)

    @staticmethod
    def get_accommodation(pid: str, uid: str) -> Optional[dict[str, Any]]:
        ''' Get accommodation

        Args:
            pid (str): Project id.
            uid (str): User id.

        Returns:
            The data or `None`.

        '''
        return FormDB().find_one({'case': 'accommodation', 'pid': pid, 'uid': uid})

    @staticmethod
    def all_accommodation(pid: str) -> Generator[dict[str, Any], None, None]:
        ''' Get all accommodation

        Args:
            pid (str): Project id.

        Yields:
            Return the data in `pid`.

        '''
        yield from FormDB().find({'case': 'accommodation', 'pid': pid})

    @staticmethod
    def update_clothes(pid: str, uid: str, data: dict[str, Any]) -> dict[str, Any]:
        ''' Update clothes

        Args:
            pid (str): Project id.
            uid (str): User id.
            data (dict): The data to update.

                - `clothes`: `S / 38.5 / 55` ...

        Returns:
            Return the added data.

        TODO:
            Need refactor in pydantic.

        '''
        return FormDB().add_by_case(case='clothes', pid=pid, uid=uid, data=data)

    @staticmethod
    def get_clothes(pid: str, uid: str) -> Optional[dict[str, Any]]:
        ''' Get clothes

        Args:
            pid (str): Project id.
            uid (str): User id.

        Returns:
            The data or `None`.

        '''
        return FormDB().find_one({'case': 'clothes', 'pid': pid, 'uid': uid})

    @staticmethod
    def all_clothes(pid: str) -> Generator[dict[str, Any], None, None]:
        ''' Get all clothes

        Args:
            pid (str): Project id.

        Yields:
            Return the data in `pid`.

        '''
        yield from FormDB().find({'case': 'clothes', 'pid': pid})

    @staticmethod
    def get_clothes_statistics(pid: str) -> dict[str, int]:
        ''' Get clothes statistics by given pid '''
        result = {
            'XS': 0,
            'S': 0,
            'M': 0,
            'L': 0,
            'XL': 0,
            '2XL': 0,
            '3XL': 0,
            '4XL': 0,
            '5XL': 0,
            '6XL': 0,
        }

        for form in Form.all_clothes(pid=pid):
            result[form['data']['clothes']] += 1

        return result

    @staticmethod
    def update_parking_card(pid: str, uid: str, data: dict[str, Any]) -> dict[str, Any]:
        ''' Update parking card

        Args:
            pid (str): Project id.
            uid (str): User id.
            data (dict): The data to update.

                - `carno`: The license plate.
                - `dates`: List of dates. The dates options are from
                           the collection of `db.project.parking_card`.

        Returns:
            Return the added data.

        TODO:
            Need refactor in pydantic.

        '''
        return FormDB().add_by_case(case='parking_card', pid=pid, uid=uid, data=data)

    @staticmethod
    def get_parking_card(pid: str, uid: str) -> Optional[dict[str, Any]]:
        ''' Get parking card

        Args:
            pid (str): Project id.
            uid (str): User id.

        Returns:
            The data or `None`.

        '''
        return FormDB().find_one({'case': 'parking_card', 'pid': pid, 'uid': uid})

    @staticmethod
    def all_parking_card(pid: str) -> Generator[dict[str, Any], None, None]:
        ''' Get all parking card

        Args:
            pid (str): Project id.

        Yields:
            Return the data in `pid`.

        '''
        yield from FormDB().find({'case': 'parking_card', 'pid': pid})

    @staticmethod
    def update_drink(pid: str, uid: str, data: dict[str, Any]) -> dict[str, Any]:
        ''' Update drink

        Args:
            pid (str): Project id.
            uid (str): User id.
            data (dict): The data to update.

                - `y18`: `bool`.

        Returns:
            Return the added data.

        TODO:
            Need refactor in pydantic.

        '''
        return FormDB().add_by_case(case='drink', pid=pid, uid=uid, data=data)

    @staticmethod
    def get_drink(pid: str, uid: str) -> Optional[dict[str, Any]]:
        ''' Get drink

        Args:
            pid (str): Project id.
            uid (str): User id.

        Returns:
            The data or `None`.

        '''
        return FormDB().find_one({'case': 'drink', 'pid': pid, 'uid': uid})

    @staticmethod
    def all_drink(pid: str) -> Generator[dict[str, Any], None, None]:
        ''' Get all drink

        Args:
            pid (str): Project id.

        Yields:
            Return the data in `pid`.

        '''
        yield from FormDB().find({'case': 'drink', 'pid': pid})

all_accommodation staticmethod

all_accommodation(
    pid: str,
) -> Generator[dict[str, Any], None, None]

Get all accommodation

Parameters:

Name Type Description Default
pid str

Project id.

required

Yields:

Type Description
dict[str, Any]

Return the data in pid.

Source code in module/form.py
@staticmethod
def all_accommodation(pid: str) -> Generator[dict[str, Any], None, None]:
    ''' Get all accommodation

    Args:
        pid (str): Project id.

    Yields:
        Return the data in `pid`.

    '''
    yield from FormDB().find({'case': 'accommodation', 'pid': pid})

all_appreciation staticmethod

all_appreciation(
    pid: str,
) -> Generator[dict[str, Any], None, None]

Get all appreciation

Parameters:

Name Type Description Default
pid str

Project id.

required

Yields:

Type Description
dict[str, Any]

Return the data in pid.

Source code in module/form.py
@staticmethod
def all_appreciation(pid: str) -> Generator[dict[str, Any], None, None]:
    ''' Get all appreciation

    Args:
        pid (str): Project id.

    Yields:
        Return the data in `pid`.

    '''
    yield from FormDB().find({'case': 'appreciation', 'pid': pid})

all_clothes staticmethod

all_clothes(
    pid: str,
) -> Generator[dict[str, Any], None, None]

Get all clothes

Parameters:

Name Type Description Default
pid str

Project id.

required

Yields:

Type Description
dict[str, Any]

Return the data in pid.

Source code in module/form.py
@staticmethod
def all_clothes(pid: str) -> Generator[dict[str, Any], None, None]:
    ''' Get all clothes

    Args:
        pid (str): Project id.

    Yields:
        Return the data in `pid`.

    '''
    yield from FormDB().find({'case': 'clothes', 'pid': pid})

all_drink staticmethod

all_drink(
    pid: str,
) -> Generator[dict[str, Any], None, None]

Get all drink

Parameters:

Name Type Description Default
pid str

Project id.

required

Yields:

Type Description
dict[str, Any]

Return the data in pid.

Source code in module/form.py
@staticmethod
def all_drink(pid: str) -> Generator[dict[str, Any], None, None]:
    ''' Get all drink

    Args:
        pid (str): Project id.

    Yields:
        Return the data in `pid`.

    '''
    yield from FormDB().find({'case': 'drink', 'pid': pid})

all_parking_card staticmethod

all_parking_card(
    pid: str,
) -> Generator[dict[str, Any], None, None]

Get all parking card

Parameters:

Name Type Description Default
pid str

Project id.

required

Yields:

Type Description
dict[str, Any]

Return the data in pid.

Source code in module/form.py
@staticmethod
def all_parking_card(pid: str) -> Generator[dict[str, Any], None, None]:
    ''' Get all parking card

    Args:
        pid (str): Project id.

    Yields:
        Return the data in `pid`.

    '''
    yield from FormDB().find({'case': 'parking_card', 'pid': pid})

all_traffic_fee staticmethod

all_traffic_fee(
    pid: str,
) -> Generator[dict[str, Any], None, None]

Get All traffic_fee

Parameters:

Name Type Description Default
pid str

Project id.

required

Yields:

Type Description
dict[str, Any]

Return the data in pid.

Source code in module/form.py
@staticmethod
def all_traffic_fee(pid: str) -> Generator[dict[str, Any], None, None]:
    ''' Get All traffic_fee

    Args:
        pid (str): Project id.

    Yields:
        Return the data in `pid`.

    '''
    yield from FormDB().find({'case': 'traffic_fee', 'pid': pid})

all_volunteer_certificate staticmethod

all_volunteer_certificate(
    pid: str,
) -> Generator[dict[str, Any], None, None]

Get All volunteer certificate

Parameters:

Name Type Description Default
pid str

Project id.

required

Yields:

Type Description
dict[str, Any]

Return the data in pid.

Source code in module/form.py
@staticmethod
def all_volunteer_certificate(pid: str) -> Generator[dict[str, Any], None, None]:
    ''' Get All volunteer certificate

    Args:
        pid (str): Project id.

    Yields:
        Return the data in `pid`.

    '''
    yield from FormDB().find({'case': 'volunteer_certificate', 'pid': pid})

get_accommodation staticmethod

get_accommodation(
    pid: str, uid: str
) -> Optional[dict[str, Any]]

Get accommodation

Parameters:

Name Type Description Default
pid str

Project id.

required
uid str

User id.

required

Returns:

Type Description
Optional[dict[str, Any]]

The data or None.

Source code in module/form.py
@staticmethod
def get_accommodation(pid: str, uid: str) -> Optional[dict[str, Any]]:
    ''' Get accommodation

    Args:
        pid (str): Project id.
        uid (str): User id.

    Returns:
        The data or `None`.

    '''
    return FormDB().find_one({'case': 'accommodation', 'pid': pid, 'uid': uid})

get_appreciation staticmethod

get_appreciation(
    pid: str, uid: str
) -> Optional[dict[str, Any]]

Get appreciation

Parameters:

Name Type Description Default
pid str

Project id.

required
uid str

User id.

required

Returns:

Type Description
Optional[dict[str, Any]]

The data or None.

Source code in module/form.py
@staticmethod
def get_appreciation(pid: str, uid: str) -> Optional[dict[str, Any]]:
    ''' Get appreciation

    Args:
        pid (str): Project id.
        uid (str): User id.

    Returns:
        The data or `None`.

    '''
    return FormDB().find_one({'case': 'appreciation', 'pid': pid, 'uid': uid})

get_clothes staticmethod

get_clothes(pid: str, uid: str) -> Optional[dict[str, Any]]

Get clothes

Parameters:

Name Type Description Default
pid str

Project id.

required
uid str

User id.

required

Returns:

Type Description
Optional[dict[str, Any]]

The data or None.

Source code in module/form.py
@staticmethod
def get_clothes(pid: str, uid: str) -> Optional[dict[str, Any]]:
    ''' Get clothes

    Args:
        pid (str): Project id.
        uid (str): User id.

    Returns:
        The data or `None`.

    '''
    return FormDB().find_one({'case': 'clothes', 'pid': pid, 'uid': uid})

get_clothes_statistics staticmethod

get_clothes_statistics(pid: str) -> dict[str, int]

Get clothes statistics by given pid

Source code in module/form.py
@staticmethod
def get_clothes_statistics(pid: str) -> dict[str, int]:
    ''' Get clothes statistics by given pid '''
    result = {
        'XS': 0,
        'S': 0,
        'M': 0,
        'L': 0,
        'XL': 0,
        '2XL': 0,
        '3XL': 0,
        '4XL': 0,
        '5XL': 0,
        '6XL': 0,
    }

    for form in Form.all_clothes(pid=pid):
        result[form['data']['clothes']] += 1

    return result

get_drink staticmethod

get_drink(pid: str, uid: str) -> Optional[dict[str, Any]]

Get drink

Parameters:

Name Type Description Default
pid str

Project id.

required
uid str

User id.

required

Returns:

Type Description
Optional[dict[str, Any]]

The data or None.

Source code in module/form.py
@staticmethod
def get_drink(pid: str, uid: str) -> Optional[dict[str, Any]]:
    ''' Get drink

    Args:
        pid (str): Project id.
        uid (str): User id.

    Returns:
        The data or `None`.

    '''
    return FormDB().find_one({'case': 'drink', 'pid': pid, 'uid': uid})

get_parking_card staticmethod

get_parking_card(
    pid: str, uid: str
) -> Optional[dict[str, Any]]

Get parking card

Parameters:

Name Type Description Default
pid str

Project id.

required
uid str

User id.

required

Returns:

Type Description
Optional[dict[str, Any]]

The data or None.

Source code in module/form.py
@staticmethod
def get_parking_card(pid: str, uid: str) -> Optional[dict[str, Any]]:
    ''' Get parking card

    Args:
        pid (str): Project id.
        uid (str): User id.

    Returns:
        The data or `None`.

    '''
    return FormDB().find_one({'case': 'parking_card', 'pid': pid, 'uid': uid})

get_traffic_fee staticmethod

get_traffic_fee(
    pid: str, uid: str
) -> Optional[dict[str, Any]]

Get traffic fee

Parameters:

Name Type Description Default
pid str

Project id.

required
uid str

User id.

required

Returns:

Type Description
Optional[dict[str, Any]]

The data or None.

Source code in module/form.py
@staticmethod
def get_traffic_fee(pid: str, uid: str) -> Optional[dict[str, Any]]:
    ''' Get traffic fee

    Args:
        pid (str): Project id.
        uid (str): User id.

    Returns:
        The data or `None`.

    '''
    return FormDB().find_one({'case': 'traffic_fee', 'pid': pid, 'uid': uid})

get_volunteer_certificate staticmethod

get_volunteer_certificate(
    pid: str, uid: str
) -> Optional[dict[str, Any]]

Get volunteer certificate

Parameters:

Name Type Description Default
pid str

Project id.

required
uid str

User id.

required

Returns:

Type Description
Optional[dict[str, Any]]

The data or None.

Source code in module/form.py
@staticmethod
def get_volunteer_certificate(pid: str, uid: str) -> Optional[dict[str, Any]]:
    ''' Get volunteer certificate

    Args:
        pid (str): Project id.
        uid (str): User id.

    Returns:
        The data or `None`.

    '''
    return FormDB().find_one({'case': 'volunteer_certificate', 'pid': pid, 'uid': uid})

update_accommodation staticmethod

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

Update accommodation

Parameters:

Name Type Description Default
pid str

Project id.

required
uid str

User id.

required
data dict

The data to update.

  • key: In one of yes-longtraffic, yes, no.
  • status: bool.
  • room: Optional. The room numbers.
  • room_key: Optional. The key for exchange the place for some users want live together.
  • mixed: (bool) accepted in mixed room.
required

Returns:

Type Description
dict[str, Any]

Return the added data.

See Also

The more about how to use the room_key to exchange the room, please move to module.form.FormAccommodation.

TODO

Need refactor in pydantic.

Source code in module/form.py
@staticmethod
def update_accommodation(pid: str, uid: str, data: dict[str, Any]) -> dict[str, Any]:
    ''' Update accommodation

    Args:
        pid (str): Project id.
        uid (str): User id.
        data (dict): The data to update.

            - `key`: In one of `yes-longtraffic`, `yes`, `no`.
            - `status`: `bool`.
            - `room`: Optional. The room numbers.
            - `room_key`: Optional. The key for exchange the place for
                          some users want live together.
            - `mixed`: (bool) accepted in mixed room.

    Returns:
        Return the added data.

    See Also:
        The more about how to use the `room_key` to exchange the `room`,
            please move to [module.form.FormAccommodation][].

    TODO:
        Need refactor in pydantic.

    '''
    return FormDB().add_by_case(case='accommodation', pid=pid, uid=uid, data=data)

update_appreciation staticmethod

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

Update appreciation

Parameters:

Name Type Description Default
pid str

Project id.

required
uid str

User id.

required
data dict

The data to update.

  • available: bool.
  • key: In one of oauth, badge_name, real_name.
  • value: As user name.
required

Returns:

Type Description
dict[str, Any]

Return the added data.

TODO

Need refactor in pydantic.

Source code in module/form.py
@staticmethod
def update_appreciation(pid: str, uid: str, data: dict[str, Any]) -> dict[str, Any]:
    ''' Update appreciation

    Args:
        pid (str): Project id.
        uid (str): User id.
        data (dict): The data to update.

            - `available`: `bool`.
            - `key`: In one of `oauth`, `badge_name`, `real_name`.
            - `value`: As user name.

    Returns:
        Return the added data.

    TODO:
        Need refactor in pydantic.

    '''
    return FormDB().add_by_case(case='appreciation', pid=pid, uid=uid, data=data)

update_clothes staticmethod

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

Update clothes

Parameters:

Name Type Description Default
pid str

Project id.

required
uid str

User id.

required
data dict

The data to update.

  • clothes: S / 38.5 / 55 ...
required

Returns:

Type Description
dict[str, Any]

Return the added data.

TODO

Need refactor in pydantic.

Source code in module/form.py
@staticmethod
def update_clothes(pid: str, uid: str, data: dict[str, Any]) -> dict[str, Any]:
    ''' Update clothes

    Args:
        pid (str): Project id.
        uid (str): User id.
        data (dict): The data to update.

            - `clothes`: `S / 38.5 / 55` ...

    Returns:
        Return the added data.

    TODO:
        Need refactor in pydantic.

    '''
    return FormDB().add_by_case(case='clothes', pid=pid, uid=uid, data=data)

update_drink staticmethod

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

Update drink

Parameters:

Name Type Description Default
pid str

Project id.

required
uid str

User id.

required
data dict

The data to update.

  • y18: bool.
required

Returns:

Type Description
dict[str, Any]

Return the added data.

TODO

Need refactor in pydantic.

Source code in module/form.py
@staticmethod
def update_drink(pid: str, uid: str, data: dict[str, Any]) -> dict[str, Any]:
    ''' Update drink

    Args:
        pid (str): Project id.
        uid (str): User id.
        data (dict): The data to update.

            - `y18`: `bool`.

    Returns:
        Return the added data.

    TODO:
        Need refactor in pydantic.

    '''
    return FormDB().add_by_case(case='drink', pid=pid, uid=uid, data=data)

update_parking_card staticmethod

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

Update parking card

Parameters:

Name Type Description Default
pid str

Project id.

required
uid str

User id.

required
data dict

The data to update.

  • carno: The license plate.
  • dates: List of dates. The dates options are from the collection of db.project.parking_card.
required

Returns:

Type Description
dict[str, Any]

Return the added data.

TODO

Need refactor in pydantic.

Source code in module/form.py
@staticmethod
def update_parking_card(pid: str, uid: str, data: dict[str, Any]) -> dict[str, Any]:
    ''' Update parking card

    Args:
        pid (str): Project id.
        uid (str): User id.
        data (dict): The data to update.

            - `carno`: The license plate.
            - `dates`: List of dates. The dates options are from
                       the collection of `db.project.parking_card`.

    Returns:
        Return the added data.

    TODO:
        Need refactor in pydantic.

    '''
    return FormDB().add_by_case(case='parking_card', pid=pid, uid=uid, data=data)

update_traffic_fee staticmethod

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

Update traffic fee

Parameters:

Name Type Description Default
pid str

Project id.

required
uid str

User id.

required
data dict

The data to update.

  • apply: bool.
  • fee: int.
  • fromwhere: str.
  • howto: str.
required

Returns:

Type Description
dict[str, Any]

Return the added data.

TODO

Need refactor in pydantic.

Source code in module/form.py
@staticmethod
def update_traffic_fee(pid: str, uid: str, data: dict[str, Any]) -> dict[str, Any]:
    ''' Update traffic fee

    Args:
        pid (str): Project id.
        uid (str): User id.
        data (dict): The data to update.

            - `apply`: `bool`.
            - `fee`: `int`.
            - `fromwhere`: `str`.
            - `howto`: `str`.

    Returns:
        Return the added data.

    TODO:
        Need refactor in pydantic.

    '''
    return FormDB().add_by_case(case='traffic_fee', pid=pid, uid=uid, data=data)

update_volunteer_certificate staticmethod

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

Update volunteer certificate

Parameters:

Name Type Description Default
pid str

Project id.

required
uid str

User id.

required
data dict

The data to update.

  • value: bool.
required

Returns:

Type Description
dict[str, Any]

Return the added data.

TODO

Need refactor in pydantic.

Source code in module/form.py
@staticmethod
def update_volunteer_certificate(pid: str, uid: str, data: dict[str, Any]) -> dict[str, Any]:
    ''' Update volunteer certificate

    Args:
        pid (str): Project id.
        uid (str): User id.
        data (dict): The data to update.

            - `value`: `bool`.

    Returns:
        Return the added data.

    TODO:
        Need refactor in pydantic.

    '''
    return FormDB().add_by_case(case='volunteer_certificate', pid=pid, uid=uid, data=data)

FormAccommodation

FormAccommodation object

Source code in module/form.py
class FormAccommodation:
    ''' FormAccommodation object '''
    @staticmethod
    def get(pid: str) -> Generator[dict[str, Any], None, None]:
        ''' Get data, and in `yes-longtraffic`, `yes`.

        Args:
            pid (str): Project id.

        Yields:
            Return the data in `pid`.

        '''
        yield from FormDB().find({'case': 'accommodation', 'pid': pid, 'data.key': {'$ne': 'no'}})

    @staticmethod
    def get_statistics(pid: str) -> dict[str, int]:
        ''' Get accommodation statistics by given pid '''
        result = {
            'yes': 0,
            'yes-longtraffic': 0,
        }

        for form in FormAccommodation.get(pid=pid):
            result[form['data']['key']] += 1

        return result

    @staticmethod
    def update_room(pid: str, uid: str, room: str, change_key: bool = True) -> dict[str, Any]:
        ''' Update room no

        Update will happend in `room`

        - Not the same with `room`.
        - Not yet have the `room`.

        Args:
            pid (str): Project id.
            uid (str): User id.
            room (str): Room numbers.
            change_key (bool): To generate a new `room_key`.

        '''
        _update = {'data.room': room}
        if change_key:
            _update['data.room_key'] = f'{uuid4().fields[0]:08x}'

        _query: dict[str, Any]
        _query = {'case': 'accommodation', 'pid': pid, 'uid': uid}
        _query['$or'] = [{'data.room': {'$ne': room}},
                         {'data.room': {'$exists': False}}]

        return FormDB().find_one_and_update(
            _query, {'$set': _update},
            return_document=ReturnDocument.AFTER,
        )

    @staticmethod
    def update_room_key(pid: str, uids: Iterable[str]) -> None:
        ''' update / regenerate the room key

        Args:
            pid (str): Project id.
            uids (list): User ids.

        '''
        for uid in uids:
            FormDB().find_one_and_update(
                {'case': 'accommodation', 'pid': pid, 'uid': uid}, {
                    '$set': {'data.room_key': f'{uuid4().fields[0]:08x}'}},
            )

    @staticmethod
    def get_room_mate(pid: str, uid: str) -> \
            tuple[Optional[dict[str, Any]], Optional[dict[str, Any]]]:
        ''' Get room mate

        Args:
            pid (str): Project id.
            uid (str): User id.

        Returns:
            (`user_room`, `mate`)

                - `user_room`: User's room info.
                - `mate`: User's room mate info.

        '''
        user_room = FormDB().find_one(
            {'case': 'accommodation', 'pid': pid, 'uid': uid})
        mate = None
        if user_room and 'room' in user_room['data'] and user_room['data']['room']:
            mate = FormDB().find_one(
                {'case': 'accommodation', 'pid': pid,
                 'uid': {'$ne': uid}, 'data.room': user_room['data']['room']})

        return (user_room, mate)

    @classmethod
    def make_exchange(cls, pid: str, uid: str, exkey: str) -> str:
        ''' make exchange

        Args:
            pid (str): Project id.
            uid (str): User id.
            exkey (str): Exchange key.

        '''
        data = FormDB().find_one_and_update(
            {'case': 'accommodation', 'pid': pid, 'uid': uid},
            {'$set': {'data.room_exkey': exkey}},
            return_document=ReturnDocument.AFTER,
        )

        ex_mate = FormDB().find_one(
            {'case': 'accommodation', 'pid': pid, 'data.room_exkey': data['data']['room_key']})
        if not ex_mate:
            logging.info('等待交換中 `%s`', exkey)
            return '等待交換中'

        # do exchange
        new_room = 'R-'+f'{uuid4().fields[0]:04x}'

        FormDB().update_many(
            {'case': 'accommodation', 'pid': pid,
                'uid': {'$in': (uid, ex_mate['uid'])}},
            {'$set': {'data.room': new_room}, '$unset': {'data.room_exkey': 1}},
        )

        cls.update_room_key(pid=pid, uids=(uid, ex_mate['uid']))

        logging.info('已交換 %s %s %s', new_room, uid, ex_mate['uid'])

        old_mates = list(FormDB().find(
            {'case': 'accommodation', 'pid': pid,
             'data.room': {'$in': (data['data']['room'], ex_mate['data']['room'])}}))

        logging.info('old_mates: %s', ', '.join(
            [mate['uid'] for mate in old_mates]))

        if len(old_mates) < 2:
            return '交換完畢'

        uids = [mate['uid'] for mate in old_mates]
        user_infos = User.get_info(uids=uids, need_sensitive=True)
        if user_infos[uids[0]]['profile_real']['roc_id'][1] != user_infos[
                uids[1]]['profile_real']['roc_id'][1]:
            logging.info('old_mates 性別不同,不交換。')
            return '交換完畢'

        new_room = 'R-'+f'{uuid4().fields[0]:04x}'

        FormDB().update_many(
            {'case': 'accommodation', 'pid': pid, 'uid': {'$in': uids}},
            {'$set': {'data.room': new_room}, '$unset': {'data.room_exkey': 1}},
        )

        cls.update_room_key(pid=pid, uids=uids)

        logging.info('old_mates new rooms: %s, %s', new_room, ', '.join(uids))

        return '交換完畢'

get staticmethod

get(pid: str) -> Generator[dict[str, Any], None, None]

Get data, and in yes-longtraffic, yes.

Parameters:

Name Type Description Default
pid str

Project id.

required

Yields:

Type Description
dict[str, Any]

Return the data in pid.

Source code in module/form.py
@staticmethod
def get(pid: str) -> Generator[dict[str, Any], None, None]:
    ''' Get data, and in `yes-longtraffic`, `yes`.

    Args:
        pid (str): Project id.

    Yields:
        Return the data in `pid`.

    '''
    yield from FormDB().find({'case': 'accommodation', 'pid': pid, 'data.key': {'$ne': 'no'}})

get_room_mate staticmethod

get_room_mate(
    pid: str, uid: str
) -> tuple[
    Optional[dict[str, Any]], Optional[dict[str, Any]]
]

Get room mate

Parameters:

Name Type Description Default
pid str

Project id.

required
uid str

User id.

required

Returns:

Type Description
tuple[Optional[dict[str, Any]], Optional[dict[str, Any]]]

(user_room, mate)

  • user_room: User's room info.
  • mate: User's room mate info.
Source code in module/form.py
@staticmethod
def get_room_mate(pid: str, uid: str) -> \
        tuple[Optional[dict[str, Any]], Optional[dict[str, Any]]]:
    ''' Get room mate

    Args:
        pid (str): Project id.
        uid (str): User id.

    Returns:
        (`user_room`, `mate`)

            - `user_room`: User's room info.
            - `mate`: User's room mate info.

    '''
    user_room = FormDB().find_one(
        {'case': 'accommodation', 'pid': pid, 'uid': uid})
    mate = None
    if user_room and 'room' in user_room['data'] and user_room['data']['room']:
        mate = FormDB().find_one(
            {'case': 'accommodation', 'pid': pid,
             'uid': {'$ne': uid}, 'data.room': user_room['data']['room']})

    return (user_room, mate)

get_statistics staticmethod

get_statistics(pid: str) -> dict[str, int]

Get accommodation statistics by given pid

Source code in module/form.py
@staticmethod
def get_statistics(pid: str) -> dict[str, int]:
    ''' Get accommodation statistics by given pid '''
    result = {
        'yes': 0,
        'yes-longtraffic': 0,
    }

    for form in FormAccommodation.get(pid=pid):
        result[form['data']['key']] += 1

    return result

make_exchange classmethod

make_exchange(pid: str, uid: str, exkey: str) -> str

make exchange

Parameters:

Name Type Description Default
pid str

Project id.

required
uid str

User id.

required
exkey str

Exchange key.

required
Source code in module/form.py
@classmethod
def make_exchange(cls, pid: str, uid: str, exkey: str) -> str:
    ''' make exchange

    Args:
        pid (str): Project id.
        uid (str): User id.
        exkey (str): Exchange key.

    '''
    data = FormDB().find_one_and_update(
        {'case': 'accommodation', 'pid': pid, 'uid': uid},
        {'$set': {'data.room_exkey': exkey}},
        return_document=ReturnDocument.AFTER,
    )

    ex_mate = FormDB().find_one(
        {'case': 'accommodation', 'pid': pid, 'data.room_exkey': data['data']['room_key']})
    if not ex_mate:
        logging.info('等待交換中 `%s`', exkey)
        return '等待交換中'

    # do exchange
    new_room = 'R-'+f'{uuid4().fields[0]:04x}'

    FormDB().update_many(
        {'case': 'accommodation', 'pid': pid,
            'uid': {'$in': (uid, ex_mate['uid'])}},
        {'$set': {'data.room': new_room}, '$unset': {'data.room_exkey': 1}},
    )

    cls.update_room_key(pid=pid, uids=(uid, ex_mate['uid']))

    logging.info('已交換 %s %s %s', new_room, uid, ex_mate['uid'])

    old_mates = list(FormDB().find(
        {'case': 'accommodation', 'pid': pid,
         'data.room': {'$in': (data['data']['room'], ex_mate['data']['room'])}}))

    logging.info('old_mates: %s', ', '.join(
        [mate['uid'] for mate in old_mates]))

    if len(old_mates) < 2:
        return '交換完畢'

    uids = [mate['uid'] for mate in old_mates]
    user_infos = User.get_info(uids=uids, need_sensitive=True)
    if user_infos[uids[0]]['profile_real']['roc_id'][1] != user_infos[
            uids[1]]['profile_real']['roc_id'][1]:
        logging.info('old_mates 性別不同,不交換。')
        return '交換完畢'

    new_room = 'R-'+f'{uuid4().fields[0]:04x}'

    FormDB().update_many(
        {'case': 'accommodation', 'pid': pid, 'uid': {'$in': uids}},
        {'$set': {'data.room': new_room}, '$unset': {'data.room_exkey': 1}},
    )

    cls.update_room_key(pid=pid, uids=uids)

    logging.info('old_mates new rooms: %s, %s', new_room, ', '.join(uids))

    return '交換完畢'

update_room staticmethod

update_room(
    pid: str, uid: str, room: str, change_key: bool = True
) -> dict[str, Any]

Update room no

Update will happend in room

  • Not the same with room.
  • Not yet have the room.

Parameters:

Name Type Description Default
pid str

Project id.

required
uid str

User id.

required
room str

Room numbers.

required
change_key bool

To generate a new room_key.

True
Source code in module/form.py
@staticmethod
def update_room(pid: str, uid: str, room: str, change_key: bool = True) -> dict[str, Any]:
    ''' Update room no

    Update will happend in `room`

    - Not the same with `room`.
    - Not yet have the `room`.

    Args:
        pid (str): Project id.
        uid (str): User id.
        room (str): Room numbers.
        change_key (bool): To generate a new `room_key`.

    '''
    _update = {'data.room': room}
    if change_key:
        _update['data.room_key'] = f'{uuid4().fields[0]:08x}'

    _query: dict[str, Any]
    _query = {'case': 'accommodation', 'pid': pid, 'uid': uid}
    _query['$or'] = [{'data.room': {'$ne': room}},
                     {'data.room': {'$exists': False}}]

    return FormDB().find_one_and_update(
        _query, {'$set': _update},
        return_document=ReturnDocument.AFTER,
    )

update_room_key staticmethod

update_room_key(pid: str, uids: Iterable[str]) -> None

update / regenerate the room key

Parameters:

Name Type Description Default
pid str

Project id.

required
uids list

User ids.

required
Source code in module/form.py
@staticmethod
def update_room_key(pid: str, uids: Iterable[str]) -> None:
    ''' update / regenerate the room key

    Args:
        pid (str): Project id.
        uids (list): User ids.

    '''
    for uid in uids:
        FormDB().find_one_and_update(
            {'case': 'accommodation', 'pid': pid, 'uid': uid}, {
                '$set': {'data.room_key': f'{uuid4().fields[0]:08x}'}},
        )

FormTrafficFeeMapping

FormTrafficFeeMapping object

Source code in module/form.py
class FormTrafficFeeMapping:
    ''' FormTrafficFeeMapping object '''

    @staticmethod
    def save(pid: str, datas: list[ProjectTrafficLocationFeeItem]) -> list[
            ProjectTrafficLocationFeeItem]:
        ''' Save mapping data

        :param str pid: pid
        :param dict data: location/fee mapping

        Args:
            pid (str): Project id.
            datas (list): location/fee mapping datas.

        Returns:
            Return the saved data.

        '''
        _data = {}
        for item in datas:
            _data[item.location] = item.fee

        result = []
        saved = FormTrafficFeeMappingDB().save(pid=pid, data=_data)
        for location, fee in saved['data'].items():
            result.append(ProjectTrafficLocationFeeItem.parse_obj({
                'location': location, 'fee': fee}))

        return result

    @staticmethod
    def get(pid: str) -> list[ProjectTrafficLocationFeeItem]:
        ''' Get

        :param str pid: pid

        Args:
            pid (str): Project id.

        Returns:
            The data or `None`.

        '''
        result = []
        datas = FormTrafficFeeMappingDB().find_one({'_id': pid})
        if datas and 'data' in datas:
            for location, fee in datas['data'].items():
                result.append(ProjectTrafficLocationFeeItem.parse_obj({
                    'location': location,
                    'fee': fee,
                }))
        return result

get staticmethod

get(pid: str) -> list[ProjectTrafficLocationFeeItem]

Get

:param str pid: pid

Parameters:

Name Type Description Default
pid str

Project id.

required

Returns:

Type Description
list[ProjectTrafficLocationFeeItem]

The data or None.

Source code in module/form.py
@staticmethod
def get(pid: str) -> list[ProjectTrafficLocationFeeItem]:
    ''' Get

    :param str pid: pid

    Args:
        pid (str): Project id.

    Returns:
        The data or `None`.

    '''
    result = []
    datas = FormTrafficFeeMappingDB().find_one({'_id': pid})
    if datas and 'data' in datas:
        for location, fee in datas['data'].items():
            result.append(ProjectTrafficLocationFeeItem.parse_obj({
                'location': location,
                'fee': fee,
            }))
    return result

save staticmethod

save(
    pid: str, datas: list[ProjectTrafficLocationFeeItem]
) -> list[ProjectTrafficLocationFeeItem]

Save mapping data

:param str pid: pid :param dict data: location/fee mapping

Parameters:

Name Type Description Default
pid str

Project id.

required
datas list

location/fee mapping datas.

required

Returns:

Type Description
list[ProjectTrafficLocationFeeItem]

Return the saved data.

Source code in module/form.py
@staticmethod
def save(pid: str, datas: list[ProjectTrafficLocationFeeItem]) -> list[
        ProjectTrafficLocationFeeItem]:
    ''' Save mapping data

    :param str pid: pid
    :param dict data: location/fee mapping

    Args:
        pid (str): Project id.
        datas (list): location/fee mapping datas.

    Returns:
        Return the saved data.

    '''
    _data = {}
    for item in datas:
        _data[item.location] = item.fee

    result = []
    saved = FormTrafficFeeMappingDB().save(pid=pid, data=_data)
    for location, fee in saved['data'].items():
        result.append(ProjectTrafficLocationFeeItem.parse_obj({
            'location': location, 'fee': fee}))

    return result