From 354330d81ba970edb14414f4c62d814e5a562abd Mon Sep 17 00:00:00 2001 From: Aman Agrawal Date: Sun, 26 Nov 2023 17:29:51 +0000 Subject: [PATCH] decorator: Move self_hosting_management_endpoint wrapper to corporate. --- corporate/lib/decorator.py | 26 ++++++++++++++++++++++++++ corporate/views/remote_billing_page.py | 2 +- corporate/views/upgrade.py | 7 ++----- zerver/decorator.py | 16 ---------------- 4 files changed, 29 insertions(+), 22 deletions(-) create mode 100644 corporate/lib/decorator.py diff --git a/corporate/lib/decorator.py b/corporate/lib/decorator.py new file mode 100644 index 0000000000..15c49b98d6 --- /dev/null +++ b/corporate/lib/decorator.py @@ -0,0 +1,26 @@ +from functools import wraps +from typing import Callable, TypeVar + +from django.conf import settings +from django.http import HttpRequest, HttpResponse +from django.shortcuts import render +from typing_extensions import Concatenate, ParamSpec + +from zerver.lib.subdomains import get_subdomain + +ParamT = ParamSpec("ParamT") + + +def self_hosting_management_endpoint( + view_func: Callable[Concatenate[HttpRequest, ParamT], HttpResponse] +) -> Callable[Concatenate[HttpRequest, ParamT], HttpResponse]: # nocoverage + @wraps(view_func) + def _wrapped_view_func( + request: HttpRequest, /, *args: ParamT.args, **kwargs: ParamT.kwargs + ) -> HttpResponse: + subdomain = get_subdomain(request) + if not settings.DEVELOPMENT or subdomain != settings.SELF_HOSTING_MANAGEMENT_SUBDOMAIN: + return render(request, "404.html", status=404) + return view_func(request, *args, **kwargs) + + return _wrapped_view_func diff --git a/corporate/views/remote_billing_page.py b/corporate/views/remote_billing_page.py index b0a5de694e..d294d8545e 100644 --- a/corporate/views/remote_billing_page.py +++ b/corporate/views/remote_billing_page.py @@ -10,11 +10,11 @@ from django.utils.translation import gettext as _ from django.views.decorators.csrf import csrf_exempt from pydantic import Json +from corporate.lib.decorator import self_hosting_management_endpoint from corporate.lib.remote_billing_util import ( RemoteBillingIdentityDict, get_identity_dict_from_session, ) -from zerver.decorator import self_hosting_management_endpoint from zerver.lib.exceptions import JsonableError, MissingRemoteRealmError from zerver.lib.remote_server import RealmDataForAnalytics, UserDataForRemoteBilling from zerver.lib.response import json_success diff --git a/corporate/views/upgrade.py b/corporate/views/upgrade.py index e82e659dc0..b03ebb3457 100644 --- a/corporate/views/upgrade.py +++ b/corporate/views/upgrade.py @@ -8,6 +8,7 @@ from django.http import HttpRequest, HttpResponse, HttpResponseRedirect from django.shortcuts import render from pydantic import Json +from corporate.lib.decorator import self_hosting_management_endpoint from corporate.lib.remote_billing_util import get_remote_realm_from_session from corporate.lib.stripe import ( VALID_BILLING_MODALITY_VALUES, @@ -22,11 +23,7 @@ from corporate.lib.stripe import ( from corporate.lib.support import get_support_url from corporate.models import CustomerPlan, ZulipSponsorshipRequest from zerver.actions.users import do_change_is_billing_admin -from zerver.decorator import ( - require_organization_member, - self_hosting_management_endpoint, - zulip_login_required, -) +from zerver.decorator import require_organization_member, zulip_login_required from zerver.lib.request import REQ, has_request_variables from zerver.lib.response import json_success from zerver.lib.send_email import FromAddress, send_email diff --git a/zerver/decorator.py b/zerver/decorator.py index cbf8f82a57..3074d19429 100644 --- a/zerver/decorator.py +++ b/zerver/decorator.py @@ -65,7 +65,6 @@ from zerver.models import UserProfile, get_client, get_user_profile_by_api_key if TYPE_CHECKING: from django.http.request import _ImmutableQueryDict -from django.shortcuts import render webhook_logger = logging.getLogger("zulip.zerver.webhooks") webhook_unsupported_events_logger = logging.getLogger("zulip.zerver.webhooks.unsupported") @@ -75,21 +74,6 @@ ParamT = ParamSpec("ParamT") ReturnT = TypeVar("ReturnT") -def self_hosting_management_endpoint( - view_func: Callable[Concatenate[HttpRequest, ParamT], HttpResponse] -) -> Callable[Concatenate[HttpRequest, ParamT], HttpResponse]: # nocoverage - @wraps(view_func) - def _wrapped_view_func( - request: HttpRequest, /, *args: ParamT.args, **kwargs: ParamT.kwargs - ) -> HttpResponse: - subdomain = get_subdomain(request) - if not settings.DEVELOPMENT or subdomain != settings.SELF_HOSTING_MANAGEMENT_SUBDOMAIN: - return render(request, "404.html", status=404) - return view_func(request, *args, **kwargs) - - return _wrapped_view_func - - def update_user_activity( request: HttpRequest, user_profile: UserProfile, query: Optional[str] ) -> None: