From f36f6564e29368a571ae4b4775a33081a0258d74 Mon Sep 17 00:00:00 2001 From: lcduong Date: Thu, 14 Nov 2024 09:07:28 +0700 Subject: [PATCH 1/5] change create_world api response --- server/venueless/api/views.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/server/venueless/api/views.py b/server/venueless/api/views.py index 322bea15..6044b2a4 100644 --- a/server/venueless/api/views.py +++ b/server/venueless/api/views.py @@ -123,6 +123,12 @@ 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) @@ -198,6 +204,14 @@ def post(request, *args, **kwargs) -> JsonResponse: {"error": "An unexpected error occurred"}, status=500 ) + site_url = settings.SITE_URL + protocol = CreateWorldView.get_protocol(site_url) + domain_path = "{}{}/{}".format( + settings.DOMAIN_PATH, + settings.BASE_PATH, + request.data.get("id"), + ) + world.domain = "{}://{}".format(protocol, domain_path) return JsonResponse(model_to_dict(world, exclude=["roles"]), status=201) else: return JsonResponse( From 17349b337ae4a30ea43faae389fc2a29be53c644 Mon Sep 17 00:00:00 2001 From: lcduong Date: Thu, 14 Nov 2024 09:24:53 +0700 Subject: [PATCH 2/5] Update code --- server/venueless/api/views.py | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/server/venueless/api/views.py b/server/venueless/api/views.py index 6044b2a4..c8a18038 100644 --- a/server/venueless/api/views.py +++ b/server/venueless/api/views.py @@ -156,20 +156,18 @@ def post(request, *args, **kwargs) -> JsonResponse: # if world already exists, update it, otherwise create a new world world_id = request.data.get("id") + domain_path = "{}{}/{}".format( + settings.DOMAIN_PATH, + settings.BASE_PATH, + request.data.get("id"), + ) try: if not world_id: raise ValidationError("World ID is required") if World.objects.filter(id=world_id).exists(): world = World.objects.get(id=world_id) world.title = title - world.domain = ( - "{}{}/{}".format( - settings.DOMAIN_PATH, - settings.BASE_PATH, - request.data.get("id"), - ) - or "" - ) + world.domain = domain_path or "" world.locale = request.data.get("locale") or "en" world.timezone = request.data.get("timezone") or "UTC" world.save() @@ -177,12 +175,7 @@ def post(request, *args, **kwargs) -> JsonResponse: world = World.objects.create( id=world_id, title=title, - domain="{}{}/{}".format( - settings.DOMAIN_PATH, - settings.BASE_PATH, - request.data.get("id"), - ) - or "", + domain=domain_path or "", locale=request.data.get("locale") or "en", timezone=request.data.get("timezone") or "UTC", config=config, @@ -206,11 +199,6 @@ def post(request, *args, **kwargs) -> JsonResponse: site_url = settings.SITE_URL protocol = CreateWorldView.get_protocol(site_url) - domain_path = "{}{}/{}".format( - settings.DOMAIN_PATH, - settings.BASE_PATH, - request.data.get("id"), - ) world.domain = "{}://{}".format(protocol, domain_path) return JsonResponse(model_to_dict(world, exclude=["roles"]), status=201) else: From baa65d070e19e91473e29c01ca6ab6ba4fbca41a Mon Sep 17 00:00:00 2001 From: lcduong Date: Thu, 14 Nov 2024 14:07:29 +0700 Subject: [PATCH 3/5] Update code --- server/venueless/api/views.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/venueless/api/views.py b/server/venueless/api/views.py index c8a18038..1305fb78 100644 --- a/server/venueless/api/views.py +++ b/server/venueless/api/views.py @@ -167,7 +167,7 @@ def post(request, *args, **kwargs) -> JsonResponse: if World.objects.filter(id=world_id).exists(): world = World.objects.get(id=world_id) world.title = title - world.domain = domain_path or "" + world.domain = domain_path world.locale = request.data.get("locale") or "en" world.timezone = request.data.get("timezone") or "UTC" world.save() @@ -175,7 +175,7 @@ def post(request, *args, **kwargs) -> JsonResponse: world = World.objects.create( id=world_id, title=title, - domain=domain_path or "", + domain=domain_path, locale=request.data.get("locale") or "en", timezone=request.data.get("timezone") or "UTC", config=config, From 77c1c4b389113291cf288b0c66b790051ea573c1 Mon Sep 17 00:00:00 2001 From: lcduong Date: Mon, 18 Nov 2024 10:29:10 +0700 Subject: [PATCH 4/5] 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"}, From 63813f6d0ca1797fb866f8d530a1b23dc34c1315 Mon Sep 17 00:00:00 2001 From: lcduong Date: Mon, 18 Nov 2024 10:37:04 +0700 Subject: [PATCH 5/5] Fix isort, flake8 in pipeline --- server/venueless/api/utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/server/venueless/api/utils.py b/server/venueless/api/utils.py index 042ad00c..c41a6a54 100644 --- a/server/venueless/api/utils.py +++ b/server/venueless/api/utils.py @@ -1,5 +1,6 @@ from urllib.parse import urlparse + def get_protocol(url): parsed = urlparse(url) protocol = parsed.scheme