-
-
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
31 changed files
with
1,633 additions
and
718 deletions.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
dev-docs/plugins/hooks/get-redirect-to-post-response-hook.md
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,107 @@ | ||
# `get_redirect_to_post_response_hook` | ||
|
||
This hook wraps the standard function that Misago uses to get a HTTP redirect response to a post. | ||
|
||
|
||
## Location | ||
|
||
This hook can be imported from `misago.threads.hooks`: | ||
|
||
```python | ||
from misago.threads.hooks import get_redirect_to_post_response_hook | ||
``` | ||
|
||
|
||
## Filter | ||
|
||
```python | ||
def custom_get_redirect_to_post_response_filter( | ||
action: GetRedirectToPostResponseHookAction, | ||
request: HttpRequest, | ||
post: Post, | ||
) -> HttpResponse: | ||
... | ||
``` | ||
|
||
A function implemented by a plugin that can be registered in this hook. | ||
|
||
|
||
### Arguments | ||
|
||
#### `action: GetRedirectToPostResponseHookAction` | ||
|
||
A standard Misago function used to get a HTTP redirect response to a post. | ||
|
||
See the [action](#action) section for details. | ||
|
||
|
||
#### `request: HttpRequest` | ||
|
||
The request object. | ||
|
||
|
||
#### `post: Post` | ||
|
||
A post to redirect to. It's `category` attribute is already populated. | ||
|
||
|
||
### Return value | ||
|
||
Django's `HttpResponse` with redirect to a post. | ||
|
||
|
||
## Action | ||
|
||
```python | ||
def get_redirect_to_post_response_action(request: HttpRequest, post: Post) -> HttpResponse: | ||
... | ||
``` | ||
|
||
A standard Misago function used to get a HTTP redirect response to a post. | ||
|
||
|
||
### Arguments | ||
|
||
#### `request: HttpRequest` | ||
|
||
The request object. | ||
|
||
|
||
#### `post: Post` | ||
|
||
A post to redirect to. It's `category` attribute is already populated. | ||
|
||
|
||
### Return value | ||
|
||
Django's `HttpResponse` with redirect to a post. | ||
|
||
|
||
## Example | ||
|
||
The code below implements a custom filter function that creates custom redirect response for posts in non-standard category type: | ||
|
||
```python | ||
from django.http import HttpRequest | ||
from django.shortcuts import redirect | ||
from django.urls import reverse | ||
|
||
from misago.threads.hooks import get_redirect_to_post_response_hook | ||
from misago.threads.models import Post | ||
|
||
BLOG_CATEGORY_TREE = 500 | ||
|
||
@get_redirect_to_post_response_hook.append_filter | ||
def redirect_to_blog_comment( | ||
action, request: HttpRequest, post: Post | ||
) -> HttpResponse: | ||
if post.category.tree_id == BLOG_CATEGORY_TREE: | ||
return redirect( | ||
reverse( | ||
"blog:story", | ||
kwargs={"id": post.thread_id}, | ||
) + f"#comment-{post.id}" | ||
) | ||
|
||
return action(request, post) | ||
``` |
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
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,101 @@ | ||
# `get_thread_url_hook` | ||
|
||
This hook wraps the standard function that Misago useds to retrieve a thread URL based on its category type. | ||
|
||
|
||
## Location | ||
|
||
This hook can be imported from `misago.threads.hooks`: | ||
|
||
```python | ||
from misago.threads.hooks import get_thread_url_hook | ||
``` | ||
|
||
|
||
## Filter | ||
|
||
```python | ||
def custom_get_thread_url_filter( | ||
action: GetThreadUrlHookAction, | ||
thread: Thread, | ||
category: Category | None=None, | ||
) -> QuerySet: | ||
... | ||
``` | ||
|
||
A function implemented by a plugin that can be registered in this hook. | ||
|
||
|
||
### Arguments | ||
|
||
#### `action: GetThreadUrlHookAction` | ||
|
||
A standard Misago function used to retrieve a thread URL based on its category type. | ||
|
||
See the [action](#action) section for details. | ||
|
||
|
||
#### `thread: Thread` | ||
|
||
A `Thread` instance. | ||
|
||
|
||
#### `category: Category` | ||
|
||
A `Category` instance, if `thread.category` was not populated using `select_related` or `prefetch_related`. Otherwise it's `None` and `thread.category` should be used instead. | ||
|
||
|
||
### Return value | ||
|
||
An `str` with URL. | ||
|
||
|
||
## Action | ||
|
||
```python | ||
def get_thread_url_action(thread: Thread, category: Category | None=None) -> QuerySet: | ||
... | ||
``` | ||
|
||
A standard Misago function used to retrieve a thread URL based on its category type. | ||
|
||
|
||
### Arguments | ||
|
||
#### `thread: Thread` | ||
|
||
A `Thread` instance. | ||
|
||
|
||
#### `category: Category` | ||
|
||
A `Category` instance, if `thread.category` was not populated using `select_related` or `prefetch_related`. Otherwise it's `None` and `thread.category` should be used instead. | ||
|
||
|
||
### Return value | ||
|
||
An `str` with URL. | ||
|
||
|
||
## Example | ||
|
||
The code below implements a custom filter function that returns thread's URL for custom category type: | ||
|
||
```python | ||
from django.urls import reverse | ||
from misago.categories.models import Category | ||
from misago.threads.hooks import get_thread_url_hook | ||
from misago.threads.models import Thread | ||
|
||
|
||
@get_thread_url_hook.append_filter | ||
def get_thread_blog_url( | ||
action, thread: Thread, category: Category | None = None | ||
): | ||
if (category or thread.category).plugin_data.get("is_blog"): | ||
return reverse( | ||
"blog", kwargs={"id": thread.id, "slug": thread.slug} | ||
) | ||
|
||
return = action(thread, category) | ||
``` |
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
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
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
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
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
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
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
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
Oops, something went wrong.