Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
tdw-catalog / tdw_catalog / team.py
Size: Mime:
from enum import IntEnum
from tdw_catalog.entity import Entity, Property, EntityBase
from tdw_catalog.relations import _OrganizationRelation
from tdw_catalog.utils import LegacyFilter
from tdw_catalog.errors import _convert_error, _raise_error
from datetime import datetime
from typing import TYPE_CHECKING, List, Optional

from tdw_catalog import Catalog
import tdw_catalog.team_member as team_member

if TYPE_CHECKING:
    import tdw_catalog.organization as organization


class TeamMemberPermissionLevel(IntEnum):
    VIEW = 1
    MANAGE = 2


@Entity([
    Property("id", str, serialize=True),
    Property("organization_id",
             str,
             relation="tdw_catalog.organization.Organization",
             serialize=True),
    Property("title", str, writable=True),
    Property("created_at", datetime),
    Property("updated_at", datetime)
])
class Team(EntityBase, _OrganizationRelation):
    """
    Teams are sets of OrganizationMembers, with which
    Datasets can be shared.

    Attributes
    ----------
    id : str
        The unique ID of this Team
    organization : organization.Organization
        The :class:`.Organization`that relates to the `organization_id` on the model
    organization_id : str
        The unique ID of the :class:`.Organization` to which this Team belongs
    title : str
        The name of this Team
    created_at : datetime
        The datetime at which this Team was created
    updated_at :  datetime
        The datetime at which this Team was last updated
    """
    _client: 'Catalog'
    id: str
    organization: 'organization.Organization'
    organization_id: str
    title: str
    created_at: datetime
    updated_at: datetime

    def __str__(self) -> str:
        return f"<Team id={self.id} title={self.title}>"

    @classmethod
    def get(cls, client: 'Catalog', organization_id: str, id: str):
        """
        Retrieve an :class:`.Team` belonging to this :class:`.Organization`

        Parameters
        ----------
        client : Catalog
            A :class:`.Catalog` client
        organization_id : str
            The unique ID of the :class:`.Organization`
        id : str
            The unique ID of the :class:`.Team`

        Returns
        -------
        Team
            The :class:`.Team` associated with the given ID

        Raises
        ------
        CatalogInternalException
            If call to the :class:`.Catalog` server fails
        CatalogNotFoundException
            If no :class:`.Team` is found matching the provided ID
        CatalogPermissionDeniedException
            If the caller is not allowed to retrieve this :class:`.Team`
        """
        try:
            res = client._get_group(organization_id=organization_id, id=id)
            return Team(client, **res)
        except Exception as e:
            err = _raise_error(e, "Unable to fetch Team {}".format(id))

    def save(self) -> None:
        """
        Update this Team, saving any changes to its title

        Parameters
        ----------
        None

        Returns
        -------
        None

        Raises
        ------
        CatalogPermissionDeniedException
            If the caller is not allowed to update this Team
        CatalogException
            If call to the :class:`.Catalog` server fails
        """
        try:
            res = self._client._update_group(group=self.serialize())
            self.deserialize(res)
        except Exception as e:
            raise _convert_error(e)

    def delete(self) -> None:
        """
        Delete this Team. This Team object should not be
        used after `delete()` has successfully returned

        Parameters
        ----------
        None

        Returns
        -------
        None

        Raises
        ------
        CatalogPermissionDeniedException
            If the caller is not allowed to delete this Team
        CatalogException
            If call to the :class:`.Catalog` server fails
        """
        try:
            self._client._delete_group(group=self.serialize())
        except Exception as e:
            raise _convert_error(e)

    def get_member(self, user_id: str) -> 'team_member.TeamMember':
        """
        Retrieve a specific member (User) of this Team

        Parameters
        ----------
        user_id : str
            The unique :class:`.User` ID of the :class:`.TeamMember`

        Returns
        -------
        TeamMember
            The :class:`.TeamMember` with the given :class:`.User` ID

        Raises
        ------
        CatalogPermissionDeniedException
            If the caller is not allowed to fetch Team members
        CatalogNotFoundException
            If the given :class:`.User` ID does not exist or is not a member of this Team
        CatalogInvalidArgumentException
            If the given :class:`.User` ID is malformed
        CatalogException
            If call to the :class:`.Catalog` server fails
        """
        # TeamMember's constructor will fill in permission automatically
        return team_member.TeamMember.get(self._client, self.id, user_id)

    def add_member(
            self, user_id: str,
            permission: TeamMemberPermissionLevel) -> 'team_member.TeamMember':
        """
        Add a :class:`.User` to the :class:`.Team` as a TeamMember. The :class:`.User` in question must
        already be a member of the containing Organization.

        Parameters
        ----------
        user_id : str
            The unique :class:`.User` ID of the invitee

        Returns
        -------
        TeamMember
            The newly created TeamMember

        Raises
        ------
        CatalogPermissionDeniedException
            If the caller is not allowed to invite Team members
        CatalogAlreadyExistsException
            If the caller is inviting a :class:`.User` who is already a TeamMember of this Team
        CatalogInvalidArgumentException
            If the given :class:`.User` ID does not exist
        CatalogException
            If call to the :class:`.Catalog` server fails
        """
        try:
            member = team_member.TeamMember(self._client,
                                            user_id=user_id,
                                            group_id=self.id,
                                            permission=permission)
            res = self._client._add_group_member(member=member.serialize())
            member.deserialize(res)
            return member
        except Exception as e:
            raise _convert_error(e)

    def list_members(
        self,
        filter: Optional[LegacyFilter] = None
    ) -> List['team_member.TeamMember']:
        """
        Retrieve all TeamMembers of this Team

        Parameters
        ----------
            None

        Returns
        -------
        list[TeamMembers]
            The :class:`.TeamMember`\\ s which are a member of this Team

        Raises
        ------
        CatalogPermissionDeniedException
            If the caller is not allowed to list TeamMembers
        CatalogException
            If call to the :class:`.Catalog` server fails
        """
        try:
            team_members = self._client._list_group_members(group_id=self.id,
                                                            filter=filter)
            return [
                team_member.TeamMember(self._client, **m) for m in team_members
            ]
        except Exception as e:
            raise _convert_error(e)

    def _get_organization(self):
        import tdw_catalog.organization as organization
        return organization.Organization.get(self._client,
                                             self.organization_id)