import time from typing import TYPE_CHECKING from sqlalchemy import ForeignKey from sqlalchemy.orm import Mapped, mapped_column, relationship from sqlalchemy.sql.sqltypes import * from flask import g from files.classes import Base from files.helpers.config.const import * from files.helpers.lazy import lazy from files.helpers.slurs_and_profanities import censor_slurs_profanities from files.helpers.sorting_and_time import make_age_string if TYPE_CHECKING: from files.classes import Comment, Post, User class HoleAction(Base): __tablename__ = "hole_actions" id: Mapped[int] = mapped_column(primary_key=True) hole: Mapped[str] = mapped_column(ForeignKey("holes.name")) user_id: Mapped[int] = mapped_column(ForeignKey("users.id")) kind: Mapped[str] target_user_id: Mapped[int] = mapped_column(ForeignKey("users.id")) target_post_id: Mapped[int] = mapped_column(ForeignKey("posts.id")) target_comment_id: Mapped[int] = mapped_column(ForeignKey("comments.id")) _note: Mapped[str] created_utc: Mapped[int] user: Mapped["User"] = relationship(primaryjoin="User.id==HoleAction.user_id") target_user: Mapped["User"] = relationship(primaryjoin="User.id==HoleAction.target_user_id") target_post: Mapped["Post"] = relationship() target_comment: Mapped["Comment"] = relationship() def __init__(self, *args, **kwargs): if "created_utc" not in kwargs: kwargs["created_utc"] = int(time.time()) super().__init__(*args, **kwargs) def __repr__(self): return f"<{self.__class__.__name__}(id={self.id})>" @property @lazy def age_string(self): return make_age_string(self.created_utc) @property @lazy def string(self): output = HOLEACTION_TYPES[self.kind]["str"].format(self=self) if self._note: output += f" ({self._note})" return output @property @lazy def target_link(self): if self.target_user_id: return f'@{self.target_user.username}' elif self.target_post_id: return censor_slurs_profanities(f'{self.target_post.title_html}', g.v) elif self.target_comment_id: return f'comment' @property @lazy def icon(self): return HOLEACTION_TYPES[self.kind]['icon'] @property @lazy def color(self): return HOLEACTION_TYPES[self.kind]['color'] @property @lazy def permalink(self): return f"{SITE_FULL}/h/{self.hole}/log/{self.id}" from files.helpers.config.holeaction_types import HOLEACTION_TYPES