from onegov.user.models import UserGroup
from onegov.core.collection import GenericCollection
from typing import overload, Literal, TypeVar, TYPE_CHECKING
if TYPE_CHECKING:
from sqlalchemy.orm import Query, Session
[docs]
_G = TypeVar('_G', bound=UserGroup)
[docs]
class UserGroupCollection(GenericCollection[_G]):
""" Manages a list of user groups.
Use it like this::
from onegov.user import UserGroupCollection
groups = UserGroupCollection(session)
"""
@overload
def __init__(
self: 'UserGroupCollection[UserGroup]',
session: 'Session',
type: Literal['*', 'generic'] = ...
): ...
@overload
def __init__(self, session: 'Session', type: str): ...
def __init__(self, session: 'Session', type: str = '*'):
@property
[docs]
def model_class(self) -> type[_G]:
return UserGroup.get_polymorphic_class( # type:ignore[return-value]
self.type, default=UserGroup) # type:ignore[arg-type]
[docs]
def query(self) -> 'Query[_G]':
query = super().query()
return query.order_by(self.model_class.name)