-
-
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.
Minimal posting threads views (#1778)
- Loading branch information
Showing
92 changed files
with
3,368 additions
and
160 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
100 changes: 100 additions & 0 deletions
100
dev-docs/plugins/hooks/check-post-in-closed-category-permission-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,100 @@ | ||
# `check_post_in_closed_category_permission_hook` | ||
|
||
This hook wraps the standard function that Misago uses to check if the user has permission to post in a closed category. It raises Django's `PermissionDenied` with an error message if category is closed and they can't post in it. | ||
|
||
|
||
## Location | ||
|
||
This hook can be imported from `misago.permissions.hooks`: | ||
|
||
```python | ||
from misago.permissions.hooks import check_post_in_closed_category_permission_hook | ||
``` | ||
|
||
|
||
## Filter | ||
|
||
```python | ||
def custom_check_post_in_closed_category_permission_filter( | ||
action: CheckPostInClosedCategoryPermissionHookAction, | ||
permissions: 'UserPermissionsProxy', | ||
category: Category, | ||
) -> None: | ||
... | ||
``` | ||
|
||
A function implemented by a plugin that can be registered in this hook. | ||
|
||
|
||
### Arguments | ||
|
||
#### `action: CheckPostInClosedCategoryPermissionHookAction` | ||
|
||
A standard Misago function used to check if the user has permission to post in a closed category. It raises Django's `PermissionDenied` with an error message if category is closed and they can't post in it. | ||
|
||
See the [action](#action) section for details. | ||
|
||
|
||
#### `user_permissions: UserPermissionsProxy` | ||
|
||
A proxy object with the current user's permissions. | ||
|
||
|
||
#### `category: Category` | ||
|
||
A category to check permissions for. | ||
|
||
|
||
## Action | ||
|
||
```python | ||
def check_post_in_closed_category_permission_action( | ||
permissions: 'UserPermissionsProxy', category: Category | ||
) -> None: | ||
... | ||
``` | ||
|
||
A standard Misago function used to check if the user has permission to post in a closed category. It raises Django's `PermissionDenied` with an error message if category is closed and they can't post in it. | ||
|
||
|
||
### Arguments | ||
|
||
#### `user_permissions: UserPermissionsProxy` | ||
|
||
A proxy object with the current user's permissions. | ||
|
||
|
||
#### `category: Category` | ||
|
||
A category to check permissions for. | ||
|
||
|
||
## Example | ||
|
||
The code below implements a custom filter function that permits a user to post in the specific category if they have a custom flag set on their account. | ||
|
||
```python | ||
from misago.categories.models import Category | ||
from misago.permissions.hooks import check_post_in_closed_category_permission_hook | ||
from misago.permissions.proxy import UserPermissionsProxy | ||
|
||
@check_post_in_closed_category_permission_hook.append_filter | ||
def check_user_can_post_in_closed_category( | ||
action, | ||
permissions: UserPermissionsProxy, | ||
category: Category, | ||
) -> None: | ||
user = permissions.user | ||
if user.is_authenticated: | ||
post_in_closed_categories = ( | ||
user.plugin_data.get("post_in_closed_categories") or [] | ||
) | ||
else: | ||
post_in_closed_categories = None | ||
|
||
if ( | ||
not post_in_closed_categories | ||
or category.id not in post_in_closed_categories | ||
): | ||
action(permissions, category) | ||
``` |
84 changes: 84 additions & 0 deletions
84
dev-docs/plugins/hooks/check-start-private-threads-permission-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,84 @@ | ||
# `check_start_private_threads_permission_hook` | ||
|
||
This hook wraps the standard function that Misago uses to check if the user has a permission to start bew private threads. Raises Django's `PermissionDenied` with an error message if they don't. | ||
|
||
|
||
## Location | ||
|
||
This hook can be imported from `misago.permissions.hooks`: | ||
|
||
```python | ||
from misago.permissions.hooks import check_start_private_threads_permission_hook | ||
``` | ||
|
||
|
||
## Filter | ||
|
||
```python | ||
def custom_check_start_private_threads_permission_filter( | ||
action: CheckStartPrivateThreadsPermissionHookAction, | ||
permissions: 'UserPermissionsProxy', | ||
) -> None: | ||
... | ||
``` | ||
|
||
A function implemented by a plugin that can be registered in this hook. | ||
|
||
|
||
### Arguments | ||
|
||
#### `action: CheckStartPrivateThreadsPermissionHookAction` | ||
|
||
A standard Misago function used to check if the user has a permission to start new private threads. Raises Django's `PermissionDenied` with an error message if they don't. | ||
|
||
See the [action](#action) section for details. | ||
|
||
|
||
#### `user_permissions: UserPermissionsProxy` | ||
|
||
A proxy object with the current user's permissions. | ||
|
||
|
||
## Action | ||
|
||
```python | ||
def check_start_private_threads_permission_action(permissions: 'UserPermissionsProxy') -> None: | ||
... | ||
``` | ||
|
||
A standard Misago function used to check if the user has a permission to start new private threads. Raises Django's `PermissionDenied` with an error message if they don't. | ||
|
||
|
||
### Arguments | ||
|
||
#### `user_permissions: UserPermissionsProxy` | ||
|
||
A proxy object with the current user's permissions. | ||
|
||
|
||
## Example | ||
|
||
The code below implements a custom filter function that blocks user from starting new private threads if there's a custom flag set on their account. | ||
|
||
```python | ||
from django.core.exceptions import PermissionDenied | ||
from django.utils.translation import pgettext | ||
from misago.permissions.hooks import check_start_private_threads_permission_hook | ||
from misago.permissions.proxy import UserPermissionsProxy | ||
|
||
@check_start_private_threads_permission_hook.append_filter | ||
def check_user_is_banned_from_starting_private_threads( | ||
action, | ||
permissions: UserPermissionsProxy, | ||
) -> None: | ||
# Run standard permission checks | ||
action(permissions) | ||
|
||
if permissions.user.plugin_data.get("ban_start_private_threads"): | ||
raise PermissionDenied( | ||
pgettext( | ||
"private threads permission error", | ||
"Site admin has removed your option to start private threads." | ||
) | ||
) | ||
``` |
102 changes: 102 additions & 0 deletions
102
dev-docs/plugins/hooks/check-start-thread-in-category-permission-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,102 @@ | ||
# `check_start_thread_in_category_permission_hook` | ||
|
||
This hook wraps the standard Misago function used to check if the user has permission to start a new thread in a category. It raises Django's `PermissionDenied` with an error message if they can't start thread in a category. | ||
|
||
|
||
## Location | ||
|
||
This hook can be imported from `misago.permissions.hooks`: | ||
|
||
```python | ||
from misago.permissions.hooks import check_start_thread_in_category_permission_hook | ||
``` | ||
|
||
|
||
## Filter | ||
|
||
```python | ||
def custom_check_start_thread_in_category_permission_filter( | ||
action: CheckStartThreadInCategoryPermissionHookAction, | ||
permissions: 'UserPermissionsProxy', | ||
category: Category, | ||
) -> None: | ||
... | ||
``` | ||
|
||
A function implemented by a plugin that can be registered in this hook. | ||
|
||
|
||
### Arguments | ||
|
||
#### `action: CheckStartThreadInCategoryPermissionHookAction` | ||
|
||
A standard Misago function used to check if the user has permission to start a new thread in a category. It raises Django's `PermissionDenied` with an error message if they can't start thread in a category. | ||
|
||
See the [action](#action) section for details. | ||
|
||
|
||
#### `user_permissions: UserPermissionsProxy` | ||
|
||
A proxy object with the current user's permissions. | ||
|
||
|
||
#### `category: Category` | ||
|
||
A category to check permissions for. | ||
|
||
|
||
## Action | ||
|
||
```python | ||
def check_start_thread_in_category_permission_action( | ||
permissions: 'UserPermissionsProxy', category: Category | ||
) -> None: | ||
... | ||
``` | ||
|
||
A standard Misago function used to check if the user has permission to start a new thread in a category. It raises Django's `PermissionDenied` with an error message if they can't start thread in a category. | ||
|
||
|
||
### Arguments | ||
|
||
#### `user_permissions: UserPermissionsProxy` | ||
|
||
A proxy object with the current user's permissions. | ||
|
||
|
||
#### `category: Category` | ||
|
||
A category to check permissions for. | ||
|
||
|
||
## Example | ||
|
||
The code below implements a custom filter function that prevents the user from starting a thread in category if their account is newer than 7 days. | ||
|
||
```python | ||
from datetime import timedelta | ||
|
||
from django.core.exceptions import PermissionDenied | ||
from django.utils import timezone | ||
from misago.categories.models import Category | ||
from misago.permissions.hooks import check_start_thread_in_category_permission_hook | ||
from misago.permissions.proxy import UserPermissionsProxy | ||
|
||
@check_start_thread_in_category_permission_hook.append_filter | ||
def check_user_can_start_thread( | ||
action, | ||
permissions: UserPermissionsProxy, | ||
category: Category, | ||
) -> None: | ||
action(permissions, category) | ||
|
||
user = permissions.user | ||
if ( | ||
user.is_authenticated | ||
and user.joined_on > timezone.now() - timedelta(days=7): | ||
): | ||
raise PermissionDenied( | ||
"Your account was created less than 7 days ago. " | ||
"You can't start threads yet." | ||
) | ||
``` |
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.