206 lines
6.0 KiB
Python
206 lines
6.0 KiB
Python
# app/models/post.py
|
|
from typing import TYPE_CHECKING
|
|
|
|
from sqlmodel import Field, Relationship, Session, select
|
|
|
|
from . import mixin
|
|
from .base import BaseSQLModel, RowId, DocumentedStrEnum, DocumentedIntFlag, auto_enum
|
|
from .user import PermissionRight, User
|
|
|
|
if TYPE_CHECKING:
|
|
from .team import Team
|
|
|
|
# region # Post ##############################################################
|
|
|
|
class PostType(DocumentedIntFlag):
|
|
POINTS = auto_enum() #allows for the ability to get points
|
|
OPTION = auto_enum() #allows groups to enter waiting list
|
|
POINTS_OPTIONS = POINTS | POINTS
|
|
|
|
VISITED = auto_enum() #only mark if a team has visited this post
|
|
CORRECT = auto_enum() #possible to fill in if correct (get max points)
|
|
|
|
|
|
class ReplayType(DocumentedStrEnum):
|
|
NOT_POSSIBLE = auto_enum()
|
|
ONLY_WHEN_NO_POINTS = auto_enum()
|
|
YES_BUT_FIRST_POINTS_COUNT = auto_enum()
|
|
YES_BUT_LAST_POINTS_COUNT = auto_enum()
|
|
YES_BUT_HIGHEST_POINTS_COUNT = auto_enum()
|
|
ALWAYS_AS_NEW = auto_enum() # add points together
|
|
|
|
# ##############################################################################
|
|
|
|
# Shared properties
|
|
class PostUserLinkBase(BaseSQLModel):
|
|
rights: PermissionRight = Field(default=PermissionRight.READ, nullable=False)
|
|
|
|
# Properties to receive via API on creation
|
|
class PostUserLinkCreate(PostUserLinkBase):
|
|
user_id: RowId = Field(nullable=False)
|
|
|
|
# Properties to receive via API on update, all are optional
|
|
class PostUserLinkUpdate(PostUserLinkBase):
|
|
pass
|
|
|
|
# Database model (link tussen post en user)
|
|
class PostUserLink(PostUserLinkBase, table=True):
|
|
post_id: RowId = Field(
|
|
foreign_key="post.id",
|
|
primary_key=True,
|
|
nullable=False,
|
|
ondelete="CASCADE",
|
|
)
|
|
user_id: RowId = Field(
|
|
foreign_key="user.id",
|
|
primary_key=True,
|
|
nullable=False,
|
|
ondelete="CASCADE",
|
|
)
|
|
|
|
post: "Post" = Relationship(back_populates="user_links")
|
|
user: "User" = Relationship(back_populates="post_links")
|
|
|
|
# Properties to return via API
|
|
class PostUserLinkPublic(PostUserLinkBase):
|
|
user_id: RowId
|
|
post_id: RowId
|
|
|
|
class PostUserLinksPublic(BaseSQLModel):
|
|
data: list[PostUserLinkPublic]
|
|
count: int
|
|
|
|
# Shared properties
|
|
class PostBase(
|
|
#common
|
|
mixin.Name, #post name
|
|
mixin.Contact, #name contact person
|
|
mixin.IsActive, #scouting active (unused)
|
|
|
|
#post specific
|
|
mixin.ShortName, #shortname for post
|
|
|
|
mixin.MaxPoints, #max obtainable points
|
|
mixin.VisitedPoints, #points obtained for visiting post
|
|
|
|
mixin.TeamAmmounts, #teams, min&max teams required
|
|
|
|
mixin.QuestionInfo, #field for questions
|
|
|
|
BaseSQLModel,
|
|
):
|
|
post_type: PostType = Field(default=PostType.POINTS, nullable=False)
|
|
replay: ReplayType = Field(default=ReplayType.NOT_POSSIBLE, nullable=False)
|
|
|
|
# Properties to receive via API on creation
|
|
class PostCreate(PostBase):
|
|
pass
|
|
|
|
# Properties to receive via API on update, all are optional
|
|
class PostUpdate(mixin.ShortNameUpdate, PostBase):
|
|
post_type: PostType | None = Field(default=None, nullable=True) # type: ignore
|
|
replay: ReplayType | None = Field(default=None, nullable=True) # type: ignore
|
|
|
|
# Database model
|
|
class Post(mixin.RowId, PostBase, table=True):
|
|
|
|
user_links: list["PostUserLink"] = Relationship(back_populates="post", cascade_delete=True)
|
|
#teams: list["Team"] = Relationship(back_populates="post", cascade_delete=True)
|
|
|
|
@classmethod
|
|
def create(cls, *, session: Session, create_obj: PostCreate) -> "Post":
|
|
data_obj = create_obj.model_dump(exclude_unset=True)
|
|
db_obj = cls.model_validate(data_obj)
|
|
session.add(db_obj)
|
|
session.commit()
|
|
session.refresh(db_obj)
|
|
return db_obj
|
|
|
|
@classmethod
|
|
def update(cls, *, session: Session, db_obj: "Post", in_obj: PostUpdate) -> "Post":
|
|
data_obj = in_obj.model_dump(exclude_unset=True)
|
|
db_obj.sqlmodel_update(data_obj)
|
|
session.add(db_obj)
|
|
session.commit()
|
|
session.refresh(db_obj)
|
|
return db_obj
|
|
|
|
def get_user_link(self, user: User) -> "PostUserLink | None":
|
|
return next((link for link in self.user_links if link.user == user), None)
|
|
|
|
def add_user(
|
|
self,
|
|
user: User,
|
|
rights: PermissionRight = PermissionRight.READ,
|
|
*,
|
|
session: Session,
|
|
) -> "PostUserLink | None":
|
|
existing = self.get_user_link(user=user)
|
|
if existing is None:
|
|
new_link = PostUserLink(post=self, user=user, rights=rights)
|
|
self.user_links.append(new_link)
|
|
session.add(new_link)
|
|
session.commit()
|
|
return new_link
|
|
return None
|
|
|
|
def update_user(
|
|
self,
|
|
user: User,
|
|
rights: PermissionRight = PermissionRight.READ,
|
|
*,
|
|
session: Session,
|
|
) -> "PostUserLink | None":
|
|
link = self.get_user_link(user=user)
|
|
if link:
|
|
link.rights = rights
|
|
session.add(link)
|
|
session.commit()
|
|
return link
|
|
return None
|
|
|
|
def remove_user(self, user: User, *, session: Session) -> None:
|
|
link = self.get_user_link(user=user)
|
|
if link:
|
|
session.delete(link)
|
|
session.commit()
|
|
|
|
def user_has_rights(
|
|
self,
|
|
user: User,
|
|
rights: PermissionRight | None = None,
|
|
) -> bool:
|
|
return any(
|
|
(
|
|
link.user == user
|
|
and link.rights
|
|
and (not rights or (link.rights & rights) == rights)
|
|
)
|
|
for link in self.user_links
|
|
)
|
|
|
|
def user_has_right(
|
|
self,
|
|
user: User,
|
|
rights: PermissionRight | None = None,
|
|
) -> bool:
|
|
return any(
|
|
(
|
|
link.user == user
|
|
and link.rights
|
|
and (not rights or (link.rights & rights))
|
|
)
|
|
for link in self.user_links
|
|
)
|
|
|
|
|
|
# API output models
|
|
class PostPublic(mixin.RowIdPublic, PostBase):
|
|
pass
|
|
|
|
|
|
class PostsPublic(BaseSQLModel):
|
|
data: list[PostPublic]
|
|
count: int
|
|
|
|
# endregion |