Skip to content

module/project.py

module.project

Project

Project

Project module

Source code in module/project.py
class Project:
    ''' Project module '''
    @staticmethod
    def create(pid: str, name: str, owners: list[str], action_date: str) -> dict[str, Any]:
        ''' Create project

        Args:
            pid (str): Project id.
            name (str): Project name.
            owners (list): List of owner's uids.
            action_date: The date at the event first date.

        Returns:
            Return the added data.

        '''
        new_project = ProjectBase.parse_obj({
            '_id': pid, 'name': name, 'owners': owners, 'action_date': action_date,
        })

        return ProjectDB(pid=pid).add(
            data=new_project.dict(by_alias=True, exclude_none=True))

    @staticmethod
    def all() -> Generator[ProjectBase, None, None]:
        ''' List all project

        Returns:
            Return all projects and order by `action_date`(desc).

        '''
        for item in ProjectDB(pid='').find({}, sort=(('action_date', -1), )):
            yield ProjectBase.parse_obj(item)

    @staticmethod
    def get(pid: str) -> ProjectBase | None:
        ''' Get project info

        Args:
            pid (str): Project id.

        Returns:
            Return the project info.

        '''
        for item in ProjectDB(pid).find({'_id': pid}):
            return ProjectBase.parse_obj(item)

        return None

    @staticmethod
    def update(pid: str, data: ProjectBaseUpdate) -> ProjectBaseUpdate:
        ''' update data

        Args:
            pid (str): Project id.
            data (dict): The data to update.
                         Can be updated fields refer to [structs.projects.ProjectBaseUpdate][]

        '''
        _data = data.dict(exclude_none=True)
        result = ProjectDB(pid).find_one_and_update(
            {'_id': pid},
            {'$set': _data},
            return_document=ReturnDocument.AFTER,
        )

        return ProjectBaseUpdate.parse_obj(result)

all staticmethod

all() -> Generator[ProjectBase, None, None]

List all project

Returns:

Type Description
Generator[ProjectBase, None, None]

Return all projects and order by action_date(desc).

Source code in module/project.py
@staticmethod
def all() -> Generator[ProjectBase, None, None]:
    ''' List all project

    Returns:
        Return all projects and order by `action_date`(desc).

    '''
    for item in ProjectDB(pid='').find({}, sort=(('action_date', -1), )):
        yield ProjectBase.parse_obj(item)

create staticmethod

create(
    pid: str, name: str, owners: list[str], action_date: str
) -> dict[str, Any]

Create project

Parameters:

Name Type Description Default
pid str

Project id.

required
name str

Project name.

required
owners list

List of owner's uids.

required
action_date str

The date at the event first date.

required

Returns:

Type Description
dict[str, Any]

Return the added data.

Source code in module/project.py
@staticmethod
def create(pid: str, name: str, owners: list[str], action_date: str) -> dict[str, Any]:
    ''' Create project

    Args:
        pid (str): Project id.
        name (str): Project name.
        owners (list): List of owner's uids.
        action_date: The date at the event first date.

    Returns:
        Return the added data.

    '''
    new_project = ProjectBase.parse_obj({
        '_id': pid, 'name': name, 'owners': owners, 'action_date': action_date,
    })

    return ProjectDB(pid=pid).add(
        data=new_project.dict(by_alias=True, exclude_none=True))

get staticmethod

get(pid: str) -> ProjectBase | None

Get project info

Parameters:

Name Type Description Default
pid str

Project id.

required

Returns:

Type Description
ProjectBase | None

Return the project info.

Source code in module/project.py
@staticmethod
def get(pid: str) -> ProjectBase | None:
    ''' Get project info

    Args:
        pid (str): Project id.

    Returns:
        Return the project info.

    '''
    for item in ProjectDB(pid).find({'_id': pid}):
        return ProjectBase.parse_obj(item)

    return None

update staticmethod

update(
    pid: str, data: ProjectBaseUpdate
) -> ProjectBaseUpdate

update data

Parameters:

Name Type Description Default
pid str

Project id.

required
data dict

The data to update. Can be updated fields refer to structs.projects.ProjectBaseUpdate

required
Source code in module/project.py
@staticmethod
def update(pid: str, data: ProjectBaseUpdate) -> ProjectBaseUpdate:
    ''' update data

    Args:
        pid (str): Project id.
        data (dict): The data to update.
                     Can be updated fields refer to [structs.projects.ProjectBaseUpdate][]

    '''
    _data = data.dict(exclude_none=True)
    result = ProjectDB(pid).find_one_and_update(
        {'_id': pid},
        {'$set': _data},
        return_document=ReturnDocument.AFTER,
    )

    return ProjectBaseUpdate.parse_obj(result)