From 77c1c4b389113291cf288b0c66b790051ea573c1 Mon Sep 17 00:00:00 2001 From: lcduong Date: Mon, 18 Nov 2024 10:29:10 +0700 Subject: [PATCH] Update code --- server/venueless/api/utils.py | 6 ++++++ server/venueless/api/views.py | 17 ++++++----------- 2 files changed, 12 insertions(+), 11 deletions(-) create mode 100644 server/venueless/api/utils.py diff --git a/server/venueless/api/utils.py b/server/venueless/api/utils.py new file mode 100644 index 00000000..042ad00c --- /dev/null +++ b/server/venueless/api/utils.py @@ -0,0 +1,6 @@ +from urllib.parse import urlparse + +def get_protocol(url): + parsed = urlparse(url) + protocol = parsed.scheme + return protocol.lower() diff --git a/server/venueless/api/views.py b/server/venueless/api/views.py index 1305fb78..1652eb99 100644 --- a/server/venueless/api/views.py +++ b/server/venueless/api/views.py @@ -31,6 +31,7 @@ from venueless.core.services.world import notify_schedule_change, notify_world_change from ..core.models import Room, World +from .utils import get_protocol logger = logging.getLogger(__name__) @@ -123,12 +124,6 @@ class CreateWorldView(APIView): authentication_classes = [] # disables authentication permission_classes = [] - @staticmethod - def get_protocol(url): - parsed = urlparse(url) - protocol = parsed.scheme - return protocol.lower() - @staticmethod def post(request, *args, **kwargs) -> JsonResponse: payload = CreateWorldView.get_payload_from_token(request) @@ -180,6 +175,11 @@ def post(request, *args, **kwargs) -> JsonResponse: timezone=request.data.get("timezone") or "UTC", config=config, ) + + site_url = settings.SITE_URL + protocol = get_protocol(site_url) + world.domain = "{}://{}".format(protocol, domain_path) + return JsonResponse(model_to_dict(world, exclude=["roles"]), status=201) except IntegrityError as e: logger.error(f"Database integrity error while saving world: {e}") return JsonResponse( @@ -196,11 +196,6 @@ def post(request, *args, **kwargs) -> JsonResponse: return JsonResponse( {"error": "An unexpected error occurred"}, status=500 ) - - site_url = settings.SITE_URL - protocol = CreateWorldView.get_protocol(site_url) - world.domain = "{}://{}".format(protocol, domain_path) - return JsonResponse(model_to_dict(world, exclude=["roles"]), status=201) else: return JsonResponse( {"error": "World cannot be created due to missing permission"},