This hook allows plugins to replace or extend the standard logic used to validate thread titles.
This hook can be imported from misago.posting.hooks:
from misago.posting.hooks import validate_thread_title_hookdef custom_validate_thread_title_filter(
action: ValidateThreadTitleHookAction,
value: str,
min_length: int,
max_length: int,
request: HttpRequest | None=None,
) -> None:
...A function implemented by a plugin that can be registered in this hook.
The next function registered in this hook, either a custom function or Misago's default.
See the action section for details.
The value to validate.
The minimum required length of the thread title.
The maximum allowed length of the thread title.
The request object or None if not provided.
def validate_thread_title_action(
value: str,
min_length: int,
max_length: int,
request: HttpRequest | None=None,
) -> None:
...Misago function for validating a thread title. Raises ValidationError if the thread title is invalid.
The value to validate.
The minimum required length of the thread title.
The maximum allowed length of the thread title.
The request object or None if not provided.
Forbid selected words in thread titles:
from django.core.exceptions import ValidationError
from django.http import HttpRequest
from django.utils.translation import pgettext
from misago.posting.hooks import validate_thread_title_hook
BAD_WORDS = ("casino", "win", "spam")
@validate_thread_title_hook.append_filter
def validate_thread_title_bad_words(
action,
value: str,
min_length: int,
max_length: int,
request: HttpRequest | None = None,
) -> None:
if value:
value_lowercase = value.lower()
for bad_word in BAD_WORDS:
if bad_word in value_lowercase:
raise ValidationError(
message=pgettext(
"thread validator",
"This title is not allowed.",
),
code="invalid",
)
action(value, min_length, max_length, request)