diff --git a/server/venueless/api/utils.py b/server/venueless/api/utils.py new file mode 100644 index 00000000..c41a6a54 --- /dev/null +++ b/server/venueless/api/utils.py @@ -0,0 +1,7 @@ +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 322bea15..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__) @@ -150,20 +151,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 world.locale = request.data.get("locale") or "en" world.timezone = request.data.get("timezone") or "UTC" world.save() @@ -171,16 +170,16 @@ 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, locale=request.data.get("locale") or "en", 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( @@ -197,8 +196,6 @@ def post(request, *args, **kwargs) -> JsonResponse: return JsonResponse( {"error": "An unexpected error occurred"}, status=500 ) - - return JsonResponse(model_to_dict(world, exclude=["roles"]), status=201) else: return JsonResponse( {"error": "World cannot be created due to missing permission"},