mirror of
https://github.com/Dadechin/Dashboard-XRoom.git
synced 2025-07-16 15:14:33 +00:00
65 lines
2.0 KiB
Python
65 lines
2.0 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.Team import Team
|
|
from core.serializers.TeamSerializer import TeamSerializer
|
|
|
|
|
|
from drf_yasg import openapi
|
|
from drf_yasg.utils import swagger_auto_schema
|
|
|
|
|
|
|
|
|
|
|
|
@swagger_auto_schema(
|
|
method='post',
|
|
request_body=openapi.Schema(
|
|
type=openapi.TYPE_OBJECT,
|
|
required=['name', 'description', 'max_persons', 'subscriptionId'],
|
|
properties={
|
|
'name': openapi.Schema(type=openapi.TYPE_STRING, default='team1'),
|
|
'description': openapi.Schema(type=openapi.TYPE_STRING, default='TEST DES'),
|
|
'max_persons': openapi.Schema(type=openapi.TYPE_STRING, default='9'),
|
|
'subscriptionId': openapi.Schema(type=openapi.TYPE_STRING, default='1'),
|
|
}
|
|
)
|
|
)
|
|
@api_view(['POST'])
|
|
@authentication_classes([SessionAuthentication, TokenAuthentication])
|
|
@permission_classes([IsAuthenticated])
|
|
def addTeam(request):
|
|
data = request.data.copy()
|
|
data['admin'] = request.user.id
|
|
data['subscription'] = data.get('subscriptionId') # Map subscriptionId to FK field
|
|
|
|
serializer = TeamSerializer(data=data)
|
|
|
|
if serializer.is_valid():
|
|
team = serializer.save()
|
|
return Response({
|
|
"message": "Team created successfully.",
|
|
"team": serializer.data
|
|
}, status=status.HTTP_201_CREATED)
|
|
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@api_view(['GET'])
|
|
@authentication_classes([SessionAuthentication, TokenAuthentication])
|
|
@permission_classes([IsAuthenticated])
|
|
def getTeams(request):
|
|
user = request.user
|
|
teams = Team.objects.filter(admin=user)
|
|
serializer = TeamSerializer(teams, many=True)
|
|
|
|
return Response({
|
|
"teams": serializer.data
|
|
}, status=status.HTTP_200_OK) |