decorator: Refactor require_server_admin_api with ParamSpec.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
This commit is contained in:
Zixuan James Li 2022-07-15 15:39:46 -04:00 committed by Tim Abbott
parent 9f2dfd848f
commit 95394de186
2 changed files with 15 additions and 8 deletions

View File

@ -124,7 +124,7 @@ def stats_for_remote_realm(
@require_server_admin_api
@has_request_variables
def get_chart_data_for_realm(
request: HttpRequest, user_profile: UserProfile, realm_str: str, **kwargs: Any
request: HttpRequest, /, user_profile: UserProfile, realm_str: str, **kwargs: Any
) -> HttpResponse:
try:
realm = get_realm(realm_str)
@ -138,6 +138,7 @@ def get_chart_data_for_realm(
@has_request_variables
def get_chart_data_for_remote_realm(
request: HttpRequest,
/,
user_profile: UserProfile,
remote_server_id: int,
remote_realm_id: int,
@ -176,7 +177,7 @@ def stats_for_remote_installation(request: HttpRequest, remote_server_id: int) -
@require_server_admin_api
@has_request_variables
def get_chart_data_for_installation(
request: HttpRequest, user_profile: UserProfile, chart_name: str = REQ(), **kwargs: Any
request: HttpRequest, /, user_profile: UserProfile, chart_name: str = REQ(), **kwargs: Any
) -> HttpResponse:
return get_chart_data(
request=request, user_profile=user_profile, for_installation=True, **kwargs
@ -187,6 +188,7 @@ def get_chart_data_for_installation(
@has_request_variables
def get_chart_data_for_remote_installation(
request: HttpRequest,
/,
user_profile: UserProfile,
remote_server_id: int,
chart_name: str = REQ(),

View File

@ -35,7 +35,7 @@ from django.utils.translation import gettext as _
from django.views.decorators.csrf import csrf_exempt
from django_otp import user_has_device
from two_factor.utils import default_device
from typing_extensions import ParamSpec
from typing_extensions import Concatenate, ParamSpec
from zerver.lib.cache import cache_with_key
from zerver.lib.exceptions import (
@ -572,17 +572,22 @@ def require_server_admin(view_func: ViewFuncT) -> ViewFuncT:
return cast(ViewFuncT, _wrapped_view_func) # https://github.com/python/mypy/issues/1927
def require_server_admin_api(view_func: ViewFuncT) -> ViewFuncT:
def require_server_admin_api(
view_func: Callable[Concatenate[HttpRequest, ParamT], HttpResponse]
) -> Callable[Concatenate[HttpRequest, ParamT], HttpResponse]:
@zulip_login_required
@wraps(view_func)
def _wrapped_view_func(
request: HttpRequest, user_profile: UserProfile, *args: object, **kwargs: object
request: HttpRequest,
/,
*args: ParamT.args,
**kwargs: ParamT.kwargs,
) -> HttpResponse:
if not user_profile.is_staff:
if not request.user.is_staff:
raise JsonableError(_("Must be an server administrator"))
return view_func(request, user_profile, *args, **kwargs)
return view_func(request, *args, **kwargs)
return cast(ViewFuncT, _wrapped_view_func) # https://github.com/python/mypy/issues/1927
return _wrapped_view_func
def require_non_guest_user(view_func: ViewFuncT) -> ViewFuncT: