33 lines
932 B
Python
33 lines
932 B
Python
import uuid
|
|
|
|
from typing import TYPE_CHECKING, List
|
|
|
|
from sqlalchemy import Unicode, text
|
|
from sqlalchemy.dialects.postgresql import UUID
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from ..db.base_class import reg
|
|
from .role_permission import RolePermission
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
from .role import Role
|
|
|
|
|
|
@reg.mapped_as_dataclass(unsafe_hash=True)
|
|
class Permission:
|
|
__tablename__ = "permissions"
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
"id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), insert_default=uuid.uuid4
|
|
)
|
|
name: Mapped[str] = mapped_column("name", Unicode(255), unique=True, nullable=False)
|
|
|
|
roles: Mapped[List["Role"]] = relationship(
|
|
secondary=RolePermission.__table__, back_populates="permissions" # type: ignore[attr-defined]
|
|
)
|
|
|
|
def __init__(self, name=None, id_=None):
|
|
self.name = name
|
|
self.id = id_
|