|
1 | 1 | import logging |
2 | 2 | from typing import TYPE_CHECKING, Any |
3 | 3 |
|
4 | | -from asgiref.sync import sync_to_async |
| 4 | +# from asgiref.sync import sync_to_async |
5 | 5 | from django.http import HttpRequest |
6 | 6 |
|
7 | 7 | if TYPE_CHECKING: |
@@ -34,54 +34,55 @@ def check_object_permission( |
34 | 34 | """ |
35 | 35 | return True |
36 | 36 |
|
37 | | - def check_permission_v2( |
38 | | - self, request: HttpRequest, controller: "ControllerBase" |
39 | | - ) -> bool: |
40 | | - """ |
41 | | - Return `True` if permission is granted, `False` otherwise. |
42 | | - """ |
43 | | - user = request.user |
44 | | - has_perm = True |
45 | | - if hasattr(controller, "model"): |
46 | | - model = controller.model |
47 | | - app = model._meta.app_label |
48 | | - has_perm = user.has_perm(f"{app}.view_{model.__name__}") |
49 | | - if request.method in ("PUT",): |
50 | | - has_perm = user.has_perm(f"{app}.add_{model.__name__}") |
51 | | - if request.method in ("PATCH", "POST"): |
52 | | - has_perm = user.has_perm(f"{app}.change_{model.__name__}") |
53 | | - if request.method in ("DELETE",): |
54 | | - has_perm = user.has_perm(f"{app}.delete_{model.__name__}") |
55 | | - if user.is_superuser: |
56 | | - has_perm = True |
57 | | - return bool(user and user.is_authenticated and user.is_active and has_perm) |
58 | | - |
59 | | - async def async_check_permission( |
60 | | - self, request: HttpRequest, controller: "ControllerBase" |
61 | | - ) -> bool: |
62 | | - """ |
63 | | - Return `True` if permission is granted, `False` otherwise. |
64 | | - """ |
65 | | - user = request.user |
66 | | - has_perm = False |
67 | | - if hasattr(controller, "model"): |
68 | | - model = controller.model |
69 | | - app = model._meta.app_label |
70 | | - has_perm = await sync_to_async(user.has_perm)( |
71 | | - f"{app}.view_{model.__name__}" |
72 | | - ) |
73 | | - if request.method in ("PUT",): |
74 | | - has_perm = await sync_to_async(user.has_perm)( |
75 | | - f"{app}.add_{model.__name__}" |
76 | | - ) |
77 | | - if request.method in ("PATCH", "POST"): |
78 | | - has_perm = await sync_to_async(user.has_perm)( |
79 | | - f"{app}.change_{model.__name__}" |
80 | | - ) |
81 | | - if request.method in ("DELETE",): |
82 | | - has_perm = await sync_to_async(user.has_perm)( |
83 | | - f"{app}.delete_{model.__name__}" |
84 | | - ) |
85 | | - if user.is_superuser: |
86 | | - has_perm = True |
87 | | - return bool(user and user.is_authenticated and user.is_active and has_perm) |
| 37 | + # |
| 38 | + # def check_permission_v2( |
| 39 | + # self, request: HttpRequest, controller: "ControllerBase" |
| 40 | + # ) -> bool: |
| 41 | + # """ |
| 42 | + # Return `True` if permission is granted, `False` otherwise. |
| 43 | + # """ |
| 44 | + # user = request.user |
| 45 | + # has_perm = True |
| 46 | + # if hasattr(controller, "model"): |
| 47 | + # model = controller.model |
| 48 | + # app = model._meta.app_label |
| 49 | + # has_perm = user.has_perm(f"{app}.view_{model.__name__}") |
| 50 | + # if request.method in ("PUT",): |
| 51 | + # has_perm = user.has_perm(f"{app}.add_{model.__name__}") |
| 52 | + # if request.method in ("PATCH", "POST"): |
| 53 | + # has_perm = user.has_perm(f"{app}.change_{model.__name__}") |
| 54 | + # if request.method in ("DELETE",): |
| 55 | + # has_perm = user.has_perm(f"{app}.delete_{model.__name__}") |
| 56 | + # if user.is_superuser: |
| 57 | + # has_perm = True |
| 58 | + # return bool(user and user.is_authenticated and user.is_active and has_perm) |
| 59 | + # |
| 60 | + # async def async_check_permission( |
| 61 | + # self, request: HttpRequest, controller: "ControllerBase" |
| 62 | + # ) -> bool: |
| 63 | + # """ |
| 64 | + # Return `True` if permission is granted, `False` otherwise. |
| 65 | + # """ |
| 66 | + # user = request.user |
| 67 | + # has_perm = False |
| 68 | + # if hasattr(controller, "model"): |
| 69 | + # model = controller.model |
| 70 | + # app = model._meta.app_label |
| 71 | + # has_perm = await sync_to_async(user.has_perm)( |
| 72 | + # f"{app}.view_{model.__name__}" |
| 73 | + # ) |
| 74 | + # if request.method in ("PUT",): |
| 75 | + # has_perm = await sync_to_async(user.has_perm)( |
| 76 | + # f"{app}.add_{model.__name__}" |
| 77 | + # ) |
| 78 | + # if request.method in ("PATCH", "POST"): |
| 79 | + # has_perm = await sync_to_async(user.has_perm)( |
| 80 | + # f"{app}.change_{model.__name__}" |
| 81 | + # ) |
| 82 | + # if request.method in ("DELETE",): |
| 83 | + # has_perm = await sync_to_async(user.has_perm)( |
| 84 | + # f"{app}.delete_{model.__name__}" |
| 85 | + # ) |
| 86 | + # if user.is_superuser: |
| 87 | + # has_perm = True |
| 88 | + # return bool(user and user.is_authenticated and user.is_active and has_perm) |
0 commit comments