Aller au contenu

Models

ApiKey

Bases: Model

ApiClient

Bases: Model

has_perm(perm)

Return True if the client has the specified permission.

Source code in api/models.py
def has_perm(self, perm: str):
    """Return True if the client has the specified permission."""

    if self._perm_cache is None:
        group_permissions = (
            Permission.objects.filter(group__group__in=self.groups.all())
            .values_list("content_type__app_label", "codename")
            .order_by()
        )
        client_permissions = self.client_permissions.values_list(
            "content_type__app_label", "codename"
        ).order_by()
        self._perm_cache = {
            f"{content_type}.{name}"
            for content_type, name in (*group_permissions, *client_permissions)
        }
    return perm in self._perm_cache

has_perms(perm_list)

Return True if the client has each of the specified permissions. If object is passed, check if the client has all required perms for it.

Source code in api/models.py
def has_perms(self, perm_list):
    """
    Return True if the client has each of the specified permissions. If
    object is passed, check if the client has all required perms for it.
    """
    if not isinstance(perm_list, Iterable) or isinstance(perm_list, str):
        raise ValueError("perm_list must be an iterable of permissions.")
    return all(self.has_perm(perm) for perm in perm_list)