Skip to content

Commit

Permalink
Modify the create_world API response (#273)
Browse files Browse the repository at this point in the history
* change create_world api response

* Update code

* Update code

* Update code

* Fix isort, flake8 in pipeline

---------

Co-authored-by: lcduong <[email protected]>
  • Loading branch information
odkhang and lcduong authored Nov 23, 2024
1 parent 928b0a7 commit a9445c5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
7 changes: 7 additions & 0 deletions server/venueless/api/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from urllib.parse import urlparse


def get_protocol(url):
parsed = urlparse(url)
protocol = parsed.scheme
return protocol.lower()
29 changes: 13 additions & 16 deletions server/venueless/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -150,37 +151,35 @@ 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()
else:
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(
Expand All @@ -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"},
Expand Down

0 comments on commit a9445c5

Please sign in to comment.