# Copyright © The Debusine Developers
# See the AUTHORS file at the top-level directory of this distribution
#
# This file is part of Debusine. It is subject to the license terms
# in the LICENSE file found in the top-level directory of this
# distribution. No part of Debusine, including this file, may be copied,
# modified, propagated, or distributed except according to the terms
# contained in the LICENSE file.
"""Interface for models for Debusine resources."""
from typing import Protocol, Self, runtime_checkable
from django.db.models import Model
from django.http import HttpRequest
from debusine.db.permissioncontext import PermissionContext
from debusine.web.views.ui import UI
[docs]
@runtime_checkable
class ResourceQuerySetProtocol[R: Model](Protocol):
"""Expected QuerySet interface for resources."""
[docs]
@runtime_checkable
class ResourceManagerProtocol[R: Model](Protocol):
"""Expected Manager interface for resources."""
[docs]
@runtime_checkable
class ResourceProtocol[R: Model](Protocol):
"""Expected Model interface for resources."""
[docs]
def ui(self, request: HttpRequest) -> UI[R]:
"""Instantiate a UI object."""
[docs]
@runtime_checkable
class ResourceWithRolesQuerySetProtocol[R: Model](
ResourceQuerySetProtocol[R], Protocol
):
"""Expected QuerySet interface for resources."""
[docs]
def can_assign_roles(self, pc: PermissionContext) -> Self:
"""Keep only resources whose roles can be assigned to groups."""
[docs]
@runtime_checkable
class ResourceWithRolesManagerProtocol[R: Model, RA: Model](
ResourceManagerProtocol[R], Protocol
):
"""Expected Manager interface for resources."""
[docs]
def get_roles_model(self) -> type[RA]:
"""Get the model used for role assignment."""
[docs]
@runtime_checkable
class ResourceWithRolesProtocol[R: Model](ResourceProtocol[R], Protocol):
"""Expected Model interface for resources."""
[docs]
def can_assign_roles(self, pc: PermissionContext) -> bool:
"""Check if resource roles can be assigned to groups."""