|
|
@ -9,6 +9,7 @@ from urllib.parse import urlparse |
|
|
|
|
|
|
|
from marshmallow import pre_load, Schema, validates, validates_schema, ValidationError |
|
|
|
from marshmallow.fields import DateTime, List, Nested, String, URL |
|
|
|
from marshmallow.types import UnknownOption |
|
|
|
|
|
|
|
from tildes.lib.url_transform import apply_url_transformations |
|
|
|
from tildes.schemas.fields import Enum, ID36, Markdown, SimpleString |
|
|
@ -36,7 +37,9 @@ class TopicSchema(Schema): |
|
|
|
group = Nested(GroupSchema, dump_only=True) |
|
|
|
|
|
|
|
@pre_load |
|
|
|
def prepare_title(self, data: dict, many: bool, partial: Any) -> dict: |
|
|
|
def prepare_title( |
|
|
|
self, data: dict, many: bool, partial: Any, unknown: UnknownOption |
|
|
|
) -> dict: |
|
|
|
"""Prepare the title before it's validated.""" |
|
|
|
# pylint: disable=unused-argument |
|
|
|
if "title" not in data: |
|
|
@ -56,7 +59,9 @@ class TopicSchema(Schema): |
|
|
|
return new_data |
|
|
|
|
|
|
|
@pre_load |
|
|
|
def prepare_tags(self, data: dict, many: bool, partial: Any) -> dict: |
|
|
|
def prepare_tags( |
|
|
|
self, data: dict, many: bool, partial: Any, unknown: UnknownOption |
|
|
|
) -> dict: |
|
|
|
"""Prepare the tags before they're validated.""" |
|
|
|
# pylint: disable=unused-argument |
|
|
|
if "tags" not in data: |
|
|
@ -98,7 +103,7 @@ class TopicSchema(Schema): |
|
|
|
return new_data |
|
|
|
|
|
|
|
@validates("tags") |
|
|
|
def validate_tags(self, value: list[str]) -> None: |
|
|
|
def validate_tags(self, value: list[str], data_key: str) -> None: |
|
|
|
"""Validate the tags field, raising an error if an issue exists. |
|
|
|
|
|
|
|
Note that tags are validated by ensuring that each tag would be a valid group |
|
|
@ -107,6 +112,7 @@ class TopicSchema(Schema): |
|
|
|
between groups and tags. For example, a popular tag in a group could be |
|
|
|
converted into a sub-group easily. |
|
|
|
""" |
|
|
|
# pylint: disable=unused-argument |
|
|
|
group_schema = GroupSchema(partial=True) |
|
|
|
for tag in value: |
|
|
|
try: |
|
|
@ -115,7 +121,9 @@ class TopicSchema(Schema): |
|
|
|
raise ValidationError("Tag %s is invalid" % tag) from exc |
|
|
|
|
|
|
|
@pre_load |
|
|
|
def prepare_markdown(self, data: dict, many: bool, partial: Any) -> dict: |
|
|
|
def prepare_markdown( |
|
|
|
self, data: dict, many: bool, partial: Any, unknown: UnknownOption |
|
|
|
) -> dict: |
|
|
|
"""Prepare the markdown value before it's validated.""" |
|
|
|
# pylint: disable=unused-argument |
|
|
|
if "markdown" not in data: |
|
|
@ -130,7 +138,9 @@ class TopicSchema(Schema): |
|
|
|
return new_data |
|
|
|
|
|
|
|
@pre_load |
|
|
|
def prepare_link(self, data: dict, many: bool, partial: Any) -> dict: |
|
|
|
def prepare_link( |
|
|
|
self, data: dict, many: bool, partial: Any, unknown: UnknownOption |
|
|
|
) -> dict: |
|
|
|
"""Prepare the link value before it's validated.""" |
|
|
|
# pylint: disable=unused-argument |
|
|
|
if "link" not in data: |
|
|
@ -157,7 +167,9 @@ class TopicSchema(Schema): |
|
|
|
return new_data |
|
|
|
|
|
|
|
@validates_schema |
|
|
|
def link_or_markdown(self, data: dict, many: bool, partial: Any) -> None: |
|
|
|
def link_or_markdown( |
|
|
|
self, data: dict, many: bool, partial: Any, unknown: UnknownOption |
|
|
|
) -> None: |
|
|
|
"""Fail validation unless at least one of link or markdown were set.""" |
|
|
|
# pylint: disable=unused-argument |
|
|
|
if "link" not in data and "markdown" not in data: |
|
|
|