Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify the create_world API response #273

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading