mirror of
https://github.com/Dadechin/Dashboard-XRoom.git
synced 2025-07-03 08:44:34 +00:00
71 lines
2.5 KiB
Python
71 lines
2.5 KiB
Python
from rest_framework.decorators import api_view, authentication_classes, permission_classes
|
|
from rest_framework.authentication import SessionAuthentication, TokenAuthentication
|
|
from rest_framework.permissions import IsAuthenticated
|
|
from rest_framework.response import Response
|
|
from rest_framework import status
|
|
from core.models.Space import Space
|
|
from core.models.AssetBundleRoom import AssetBundleRoom
|
|
from core.serializers.SpaceSerializer import SpaceSerializer , SpaceSerializerforadd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@api_view(['GET'])
|
|
@authentication_classes([SessionAuthentication, TokenAuthentication])
|
|
@permission_classes([IsAuthenticated])
|
|
def getSpaces(request):
|
|
# Get the spaces associated with the authenticated user and join with AssetBundleRoom data
|
|
spaces = Space.objects.filter(userId=request.user).select_related('assetBundleRoomId')
|
|
|
|
# Serialize the spaces and include all fields from the related AssetBundleRoom
|
|
# Pass 'assetBundleRoomId' as it will be automatically included by select_related
|
|
serializer = SpaceSerializer(spaces, many=True)
|
|
|
|
# Return the serialized data as a response
|
|
return Response({
|
|
"spaces": serializer.data
|
|
}, status=status.HTTP_200_OK)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@api_view(['POST'])
|
|
@authentication_classes([SessionAuthentication, TokenAuthentication])
|
|
@permission_classes([IsAuthenticated])
|
|
def addSpace(request):
|
|
# Make a mutable copy of the request data
|
|
data = request.data.copy()
|
|
data['userId'] = request.user.id # Automatically assign the authenticated user
|
|
|
|
# Retrieve the AssetBundleRoom instance from the provided ID
|
|
try:
|
|
asset_bundle_room = AssetBundleRoom.objects.get(id=data['assetBundleRoomId'])
|
|
except AssetBundleRoom.DoesNotExist:
|
|
return Response({"detail": "AssetBundleRoom not found."}, status=status.HTTP_404_NOT_FOUND)
|
|
|
|
# Assign the AssetBundleRoom instance to the data
|
|
data['assetBundleRoomId'] = asset_bundle_room.id
|
|
|
|
# Pass the request object to the serializer context
|
|
serializer = SpaceSerializerforadd(data=data)
|
|
|
|
if serializer.is_valid():
|
|
# Save the space using the validated data
|
|
space = serializer.save() # This automatically saves the space
|
|
|
|
# Return the response with the space data
|
|
return Response({
|
|
"message": "Space added successfully.",
|
|
"space": serializer.data # This gives you the serialized data of the saved space
|
|
}, status=status.HTTP_201_CREATED)
|
|
|
|
else:
|
|
# If validation fails, return the errors
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|