-
-
Notifications
You must be signed in to change notification settings - Fork 527
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
806 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
132
misago/posting/hooks/get_start_private_thread_page_context_data.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
128
misago/posting/hooks/get_start_private_thread_page_formset.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
Oops, something went wrong.