Files
core/orm/invite.py

45 lines
1.4 KiB
Python
Raw Normal View History

2024-10-21 12:15:44 +03:00
import enum
2023-12-17 23:30:20 +03:00
2025-07-31 18:55:59 +03:00
from sqlalchemy import ForeignKey, Index, Integer, String, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column, relationship
2023-12-17 23:30:20 +03:00
2025-07-25 01:04:15 +03:00
from orm.base import BaseModel as Base
2023-11-28 13:46:06 +03:00
2024-10-21 12:15:44 +03:00
class InviteStatus(enum.Enum):
2024-04-17 18:32:23 +03:00
PENDING = "PENDING"
ACCEPTED = "ACCEPTED"
REJECTED = "REJECTED"
2023-11-28 13:46:06 +03:00
2024-10-21 12:15:44 +03:00
@classmethod
2025-07-31 18:55:59 +03:00
def from_string(cls, value: str) -> "InviteStatus":
2024-10-21 12:15:44 +03:00
return cls(value)
2023-11-28 13:46:06 +03:00
class Invite(Base):
2024-04-17 18:32:23 +03:00
__tablename__ = "invite"
2023-11-28 13:46:06 +03:00
2025-07-31 18:55:59 +03:00
id: Mapped[int] = mapped_column(Integer, primary_key=True)
inviter_id: Mapped[int] = mapped_column(ForeignKey("author.id"))
author_id: Mapped[int] = mapped_column(ForeignKey("author.id"))
shout_id: Mapped[int] = mapped_column(ForeignKey("shout.id"))
status: Mapped[str] = mapped_column(String, default=InviteStatus.PENDING.value)
2023-11-28 13:46:06 +03:00
2024-10-21 12:15:44 +03:00
inviter = relationship("Author", foreign_keys=[inviter_id])
author = relationship("Author", foreign_keys=[author_id])
shout = relationship("Shout")
2025-07-31 18:55:59 +03:00
__table_args__ = (
UniqueConstraint(inviter_id, author_id, shout_id),
Index("idx_invite_inviter_id", "inviter_id"),
Index("idx_invite_author_id", "author_id"),
Index("idx_invite_shout_id", "shout_id"),
{"extend_existing": True},
)
def set_status(self, status: InviteStatus) -> None:
self.status = status.value
2024-10-21 12:15:44 +03:00
def get_status(self) -> InviteStatus:
2025-07-31 18:55:59 +03:00
return InviteStatus.from_string(str(self.status))