Skip to content

cattle_grid.manage

cattle_grid.manage

AccountManager dataclass

Access for managing accounts from outside cattle_grid, e.g. by an extension

Parameters:

Name Type Description Default
account Account
required
Source code in cattle_grid/manage/account.py
@dataclass
class AccountManager:
    """Access for managing accounts from outside cattle_grid, e.g.
    by an extension"""

    account: Account

    @staticmethod
    async def for_name_and_password(name: str, password: str) -> "AccountManager":
        """Returns an AccountManager for the given name and password"""
        account = await account_with_name_password(name, password)

        if account is None:
            raise ValueError("Account not found")

        return AccountManager(account=account)

    @staticmethod
    async def for_name(name: str) -> "AccountManager":
        """Returns an AccountManager for a given name"""

        account = await Account.get_or_none(name=name)

        if account is None:
            raise ValueError("Account not found")

        return AccountManager(account=account)

    def account_information(self) -> list[ActorInformation]:
        """Returns the actors belonging to the account"""
        return [
            actor_to_information(x)
            for x in self.account.actors
            if x.status == ActorStatus.active
        ]

    async def allowed_base_urls(self) -> list[str]:
        """Returns the list of base urls allowed for the account"""
        return await allowed_base_urls(self.account)

account_information

account_information() -> list[ActorInformation]

Returns the actors belonging to the account

Source code in cattle_grid/manage/account.py
def account_information(self) -> list[ActorInformation]:
    """Returns the actors belonging to the account"""
    return [
        actor_to_information(x)
        for x in self.account.actors
        if x.status == ActorStatus.active
    ]

allowed_base_urls async

allowed_base_urls() -> list[str]

Returns the list of base urls allowed for the account

Source code in cattle_grid/manage/account.py
async def allowed_base_urls(self) -> list[str]:
    """Returns the list of base urls allowed for the account"""
    return await allowed_base_urls(self.account)

for_name async staticmethod

for_name(name: str) -> AccountManager

Returns an AccountManager for a given name

Source code in cattle_grid/manage/account.py
@staticmethod
async def for_name(name: str) -> "AccountManager":
    """Returns an AccountManager for a given name"""

    account = await Account.get_or_none(name=name)

    if account is None:
        raise ValueError("Account not found")

    return AccountManager(account=account)

for_name_and_password async staticmethod

for_name_and_password(
    name: str, password: str
) -> AccountManager

Returns an AccountManager for the given name and password

Source code in cattle_grid/manage/account.py
@staticmethod
async def for_name_and_password(name: str, password: str) -> "AccountManager":
    """Returns an AccountManager for the given name and password"""
    account = await account_with_name_password(name, password)

    if account is None:
        raise ValueError("Account not found")

    return AccountManager(account=account)

ActorManager dataclass

Access for managing actors from outside cattle_grid, e.g. by an extension

Parameters:

Name Type Description Default
actor_id str

The URI of the actor to manage

required
_actor_for_account ActorForAccount | None
None
_actor Actor | None
None
Source code in cattle_grid/manage/actor.py
@dataclass
class ActorManager:
    """Access for managing actors from outside cattle_grid, e.g.
    by an extension"""

    actor_id: str = field(metadata={"description": "The URI of the actor to manage"})

    _actor_for_account: ActorForAccount | None = field(default=None)
    _actor: Actor | None = field(default=None)

    async def actor_for_account(self) -> ActorForAccount:
        if not self._actor_for_account:
            self._actor_for_account = await actor_for_actor_id(self.actor_id)

        if not self._actor_for_account:
            raise ValueError("Actor not found")

        return self._actor_for_account

    async def actor(self) -> Actor:
        if not self._actor:
            self._actor = await Actor.get_or_none(
                actor_id=self.actor_id
            ).prefetch_related("identifiers")
        if not self._actor:
            raise ValueError("Actor not found")
        return self._actor

    async def add_to_group(self, group: str):
        """Adds the actor to a group"""
        actor = await self.actor_for_account()
        await add_actor_to_group(actor, group)

    async def groups(self) -> list[str]:
        """Returns the list of groups the actor belongs to"""
        actor = await self.actor_for_account()
        return await group_names_for_actor(actor)

    async def profile(self) -> dict:
        """Returns the actor profile"""
        return actor_to_object(await self.actor())

add_to_group async

add_to_group(group: str)

Adds the actor to a group

Source code in cattle_grid/manage/actor.py
async def add_to_group(self, group: str):
    """Adds the actor to a group"""
    actor = await self.actor_for_account()
    await add_actor_to_group(actor, group)

groups async

groups() -> list[str]

Returns the list of groups the actor belongs to

Source code in cattle_grid/manage/actor.py
async def groups(self) -> list[str]:
    """Returns the list of groups the actor belongs to"""
    actor = await self.actor_for_account()
    return await group_names_for_actor(actor)

profile async

profile() -> dict

Returns the actor profile

Source code in cattle_grid/manage/actor.py
async def profile(self) -> dict:
    """Returns the actor profile"""
    return actor_to_object(await self.actor())