Skip to content

Commit

Permalink
Add hooks to start thread page
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalp committed Sep 19, 2024
1 parent 3b94531 commit 724c69c
Show file tree
Hide file tree
Showing 9 changed files with 806 additions and 9 deletions.
22 changes: 18 additions & 4 deletions misago/posting/hooks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
from .get_start_private_thread_state import get_start_private_thread_state_hook
from .get_start_thread_state import get_start_thread_state_hook
from .get_start_private_thread_page_context_data import (
get_start_private_thread_page_context_data_hook,
)
from .get_start_private_thread_page_formset import (
get_start_private_thread_page_formset_hook,
)
from .get_start_private_thread_page_state import (
get_start_private_thread_page_state_hook,
)
from .get_start_thread_page_context_data import get_start_thread_page_context_data_hook
from .get_start_thread_page_formset import get_start_thread_page_formset_hook
from .get_start_thread_page_state import get_start_thread_page_state_hook
from .save_start_private_thread_state import save_start_private_thread_state_hook
from .save_start_thread_state import save_start_thread_state_hook


__all__ = [
"get_start_private_thread_state_hook",
"get_start_thread_state_hook",
"get_start_private_thread_page_context_data_hook",
"get_start_private_thread_page_formset_hook",
"get_start_private_thread_page_state_hook",
"get_start_thread_page_context_data_hook",
"get_start_thread_page_formset_hook",
"get_start_thread_page_state_hook",
"save_start_private_thread_state_hook",
"save_start_thread_state_hook",
]
132 changes: 132 additions & 0 deletions misago/posting/hooks/get_start_private_thread_page_context_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
from typing import TYPE_CHECKING, Protocol

from django.http import HttpRequest

from ...categories.models import Category
from ...plugins.hooks import FilterHook

if TYPE_CHECKING:
from ..forms.start import StartThreadFormset


class GetStartPrivateThreadPageContextDataHookAction(Protocol):
"""
A standard Misago function used to get the template context data
for the start private thread page.
# Arguments
## `request: HttpRequest`
The request object.
## `category: Category`
The `Category` instance.
## `formset: StartThreadFormset`
The `StartThreadFormset` instance.
# Return value
A Python `dict` with context data to use to `render` the start private thread page.
"""

def __call__(
self,
request: HttpRequest,
category: Category,
formset: "StartThreadFormset",
) -> dict: ...


class GetStartPrivateThreadPageContextDataHookFilter(Protocol):
"""
A function implemented by a plugin that can be registered in this hook.
# Arguments
## `action: GetStartPrivateThreadPageContextDataHookAction`
A standard Misago function used to get the template context data
for the start private thread page.
See the [action](#action) section for details.
## `request: HttpRequest`
The request object.
## `category: Category`
The `Category` instance.
## `formset: StartThreadFormset`
The `StartThreadFormset` instance.
# Return value
A Python `dict` with context data to use to `render` the start private thread page.
"""

def __call__(
self,
action: GetStartPrivateThreadPageContextDataHookAction,
request: HttpRequest,
category: Category,
formset: "StartThreadFormset",
) -> dict: ...


class GetStartPrivateThreadPageContextDataHook(
FilterHook[
GetStartPrivateThreadPageContextDataHookAction,
GetStartPrivateThreadPageContextDataHookFilter,
]
):
"""
This hook wraps the standard function that Misago uses to get the template
context data for the start private thread page.
# Example
The code below implements a custom filter function that adds extra values to
the template context data:
```python
from django.http import HttpRequest
from misago.categories.models import Category
from misago.posting.forms.start import StartThreadFormset
from misago.posting.hooks import get_start_private_thread_page_context_data_hook
@get_start_private_thread_page_context_data_hook.append_filter
def set_show_first_post_warning_in_context(
action,
request: HttpRequest,
category: Category,
formset: StartThreadFormset,
) -> dict:
context = action(request, category, formset)
context["show_first_post_warning"] = not requser.user.posts
return context
```
"""

__slots__ = FilterHook.__slots__

def __call__(
self,
action: GetStartPrivateThreadPageContextDataHookAction,
request: HttpRequest,
category: Category,
formset: "StartThreadFormset",
) -> dict:
return super().__call__(action, request, category, formset)


get_start_private_thread_page_context_data_hook = (
GetStartPrivateThreadPageContextDataHook(cache=False)
)
128 changes: 128 additions & 0 deletions misago/posting/hooks/get_start_private_thread_page_formset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
from typing import TYPE_CHECKING, Protocol

from django.http import HttpRequest

from ...categories.models import Category
from ...plugins.hooks import FilterHook

if TYPE_CHECKING:
from ..forms.start import StartThreadFormset


class GetStartPrivateThreadPageFormsetHookAction(Protocol):
"""
A standard function that Misago uses to create a new
`StartThreadFormset` instance for the start a new private thread page.
# Arguments
## `request: HttpRequest`
The request object.
## `category: Category`
The `Category` instance.
# Return value
A `StartThreadFormset` instance with forms to display
on the start a new private thread page.
"""

def __call__(
self,
request: HttpRequest,
category: Category,
) -> "StartThreadFormset": ...


class GetStartPrivateThreadPageFormsetHookFilter(Protocol):
"""
A function implemented by a plugin that can be registered in this hook.
# Arguments
## `action: GetStartPrivateThreadPageFormsetHookAction`
A standard function that Misago uses to create a new
`StartThreadFormset` instance for the start a new private thread page.
See the [action](#action) section for details.
## `request: HttpRequest`
The request object.
## `category: Category`
The `Category` instance.
# Return value
A `StartThreadFormset` instance with forms to display
on the start a new private thread page.
"""

def __call__(
self,
action: GetStartPrivateThreadPageFormsetHookAction,
request: HttpRequest,
category: Category,
) -> "StartThreadFormset": ...


class GetStartPrivateThreadPageFormsetHook(
FilterHook[
GetStartPrivateThreadPageFormsetHookAction,
GetStartPrivateThreadPageFormsetHookFilter,
]
):
"""
This hook wraps the standard function that Misago uses to create a new
`StartThreadFormset` instance for the start a new private thread page.
# Example
The code below implements a custom filter function that adds custom form to
the start a new private thread page:
```python
from django.http import HttpRequest
from misago.categories.models import Category
from misago.posting.hooks import get_start_private_thread_page_formset_hook
from misago.posting.forms.start import StartThreadFormset
from .forms import SelectUserForm
@get_start_private_thread_page_formset_hook.append_filter
def add_select_user_form(
action, request: HttpRequest, category: Category
) -> StartThreadFormset:
formset = action(request, category)
if request.method == "POST":
form = SelectUserForm(request.POST, prefix="select-user")
else:
form = SelectUserForm(prefix="select-user")
formset.add_form(form)
return formset
```
"""

__slots__ = FilterHook.__slots__

def __call__(
self,
action: GetStartPrivateThreadPageFormsetHookAction,
request: HttpRequest,
category: Category,
) -> "StartThreadFormset":
return super().__call__(action, request, category)


get_start_private_thread_page_formset_hook = GetStartPrivateThreadPageFormsetHook(
cache=False
)
Loading

0 comments on commit 724c69c

Please sign in to comment.