Files
devplacepy/devplacepy/models/content.py
T
2026-11-05 17:59:21 +01:00

133 lines
3.9 KiB
Python

# retoor <retoor@molodetz.nl>
from typing import Literal
from pydantic import BaseModel, Field, field_validator, model_validator
from devplacepy.constants import TOPICS
from devplacepy.rendering import is_single_emoji
from devplacepy.models.shared import (
normalize_poll_options,
VisibilityCreateMixin,
VisibilityEditMixin,
)
class PostForm(VisibilityCreateMixin):
content: str = Field(min_length=10, max_length=125000)
title: str = Field(default="", max_length=500)
topic: str = "random"
project_uid: str = Field(default="", max_length=36)
attachment_uids: list[str] = []
poll_question: str = Field(default="", max_length=200)
poll_options: list[str] = []
war_faction_a: str = Field(default="", max_length=30)
war_faction_b: str = Field(default="", max_length=30)
@field_validator("poll_options")
@classmethod
def poll_options_max_length(cls, value):
if value is None:
return value
for opt in value:
if isinstance(opt, str) and len(opt) > 200:
raise ValueError("Each poll option must be 200 characters or fewer")
return value
@field_validator("topic")
@classmethod
def valid_topic(cls, value):
return value if value in TOPICS else "random"
@field_validator("poll_options", mode="before")
@classmethod
def split_poll_options(cls, value):
return normalize_poll_options(value)
class WarJoinForm(BaseModel):
faction: str
@field_validator("faction")
@classmethod
def valid_faction(cls, value):
if value not in ("a", "b"):
raise ValueError("Faction must be a or b")
return value
class PostEditForm(VisibilityEditMixin):
content: str = Field(min_length=10, max_length=125000)
title: str = Field(default="", max_length=500)
topic: str = "random"
poll_question: str = Field(default="", max_length=200)
poll_options: list[str] = []
@field_validator("poll_options")
@classmethod
def poll_options_max_length(cls, value):
if value is None:
return value
for opt in value:
if isinstance(opt, str) and len(opt) > 200:
raise ValueError("Each poll option must be 200 characters or fewer")
return value
@field_validator("topic")
@classmethod
def valid_topic(cls, value):
return value if value in TOPICS else "random"
@field_validator("poll_options", mode="before")
@classmethod
def split_poll_options(cls, value):
return normalize_poll_options(value)
class CommentForm(BaseModel):
content: str = Field(min_length=3, max_length=125000)
target_uid: str = Field(default="", max_length=36)
post_uid: str = Field(default="", max_length=36)
target_type: Literal["post", "project", "news", "issue", "gist", "quiz"] = "post"
parent_uid: str = Field(default="", max_length=36)
attachment_uids: list[str] = []
@model_validator(mode="after")
def require_target(self):
if not (self.target_uid or self.post_uid):
raise ValueError("A target is required")
return self
class CommentEditForm(BaseModel):
content: str = Field(min_length=3, max_length=125000)
class NoteForm(BaseModel):
content: str = Field(min_length=1, max_length=4000)
class VoteForm(BaseModel):
value: int
@field_validator("value")
@classmethod
def valid_value(cls, value):
if value not in (1, -1, 0):
raise ValueError("value must be 1, -1 or 0")
return value
class ReactionForm(BaseModel):
emoji: str = Field(min_length=1, max_length=16)
@field_validator("emoji")
@classmethod
def valid_emoji(cls, value):
reaction = (value or "").strip()
if not is_single_emoji(reaction):
raise ValueError("Reaction must be a single emoji")
return reaction
class PollVoteForm(BaseModel):
option_uid: str = Field(min_length=1, max_length=36)