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

Open
wants to merge 5 commits into
base: development
Choose a base branch
from
Open
Changes from 3 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
30 changes: 16 additions & 14 deletions server/venueless/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -150,33 +156,26 @@ 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,
Expand All @@ -198,6 +197,9 @@ def post(request, *args, **kwargs) -> 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(
Expand Down
Loading