Skip to content

Commit ebeadc4

Browse files
committed
lint: cleanups
1 parent 5575b14 commit ebeadc4

File tree

7 files changed

+56
-300
lines changed

7 files changed

+56
-300
lines changed

easy/decorators.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ def request_passes_test(
1919
def decorator(view_func):
2020
@wraps(view_func)
2121
def _wrapped_view(request, *args, **kwargs):
22-
# print(f"request --- {request.__dict__.items()}")
2322
if test_func(request):
2423
return view_func(request, *args, **kwargs)
2524
path = request.build_absolute_uri()

easy/domain/serializers.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,7 @@ def is_paginated(data: Any) -> bool:
3333

3434
def is_base_response(data: Any) -> bool:
3535
"""Check if it is already good or a BaseApiResponse"""
36-
return (
37-
data
38-
and isinstance(data, dict)
39-
and data.get("now", None) is not None
40-
and data.get("data", None) is not None
41-
) or (
42-
isinstance(data, BaseApiResponse)
43-
and data.get("now", None) is not None
44-
and data.get("data", None) is not None
45-
)
36+
return isinstance(data, BaseApiResponse) and data.get("data", None) is not None
4637

4738

4839
def serialize_base_response(data: Any) -> BaseApiResponse:

easy/exception.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
from ninja_extra import status
44
from ninja_extra.exceptions import APIException
55

6-
from easy.response import BaseApiResponse
7-
86

97
class BaseAPIException(APIException):
108
"""
@@ -28,23 +26,8 @@ def __init__(
2826

2927
class APIAuthException(BaseAPIException):
3028
"""
31-
API权限异常
29+
API Auth Exception
3230
"""
3331

3432
status_code = status.HTTP_401_UNAUTHORIZED
3533
default_detail = "Unauthorized"
36-
37-
38-
def http_error_handler(request, exc):
39-
"""
40-
API异常处理函数
41-
"""
42-
code = exc.status_code
43-
msg = str(exc)
44-
data = {
45-
"status": code,
46-
"message": msg,
47-
"detail": msg,
48-
}
49-
50-
return BaseApiResponse(status=code, data=data, errno=code, message=msg)

easy/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class EasyAPI(NinjaExtraAPI):
2727
Can serialize queryset or model, and support pagination
2828
Easy_output: bool = True,
2929
If True, will be encapsulate in BaseAPIResponse
30-
-renderer, default EasyJSONRenderer
30+
-renderer, default to EasyJSONRenderer
3131
-API docs default to docs_permission_required,only logged in Staff users can visit
3232
-Auto generate AdminAPIs, it will read the following settings:
3333
AUTO_ADMIN_ENABLED_ALL_APPS

easy/response.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import json
2-
import time
32

43
from django.http.response import JsonResponse
54

@@ -26,7 +25,6 @@ def __init__(self, data=None, errno=None, message=None, *args, **kwargs):
2625
"code": errno,
2726
"message": message,
2827
"data": data if data is not None else {},
29-
"now": int(time.time()),
3028
}
3129
super().__init__(data=_data, encoder=EasyJSONEncoder, *args, **kwargs)
3230

easy/services/permission.py

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
from typing import TYPE_CHECKING, Any
33

4-
from asgiref.sync import sync_to_async
4+
# from asgiref.sync import sync_to_async
55
from django.http import HttpRequest
66

77
if TYPE_CHECKING:
@@ -34,54 +34,55 @@ def check_object_permission(
3434
"""
3535
return True
3636

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

Comments
 (0)