mirror of
https://github.com/Dadechin/Dashboard-XRoom.git
synced 2025-07-03 08:44:34 +00:00
253 lines
9.4 KiB
Python
253 lines
9.4 KiB
Python
from rest_framework.decorators import api_view, authentication_classes, permission_classes
|
|
from rest_framework.permissions import IsAuthenticated
|
|
from rest_framework.authentication import TokenAuthentication, SessionAuthentication
|
|
from rest_framework.response import Response
|
|
from rest_framework import status
|
|
from drf_yasg.utils import swagger_auto_schema
|
|
from drf_yasg import openapi
|
|
from core.serializers.MeetingSerializer import MeetingSerializer , MeetingSerializerFull
|
|
from core.utils.sms import send_sms # Assuming send_sms is in core/utils/sms.py
|
|
|
|
from core.models.Meeting import Meeting
|
|
|
|
# @swagger_auto_schema(
|
|
# method='post',
|
|
# request_body=openapi.Schema(
|
|
# type=openapi.TYPE_OBJECT,
|
|
# required=['name', 'description', 'date_time'],
|
|
# properties={
|
|
# 'name': openapi.Schema(type=openapi.TYPE_STRING, default='Sprint Planning'),
|
|
# 'description': openapi.Schema(type=openapi.TYPE_STRING, default='Discuss the next sprint'),
|
|
# 'date_time': openapi.Schema(type=openapi.TYPE_STRING, format='date-time', default='2025-06-01T14:00:00Z'),
|
|
# 'space': openapi.Schema(type=openapi.TYPE_INTEGER, default=1),
|
|
# 'asset_bundle': openapi.Schema(type=openapi.TYPE_INTEGER, default=1),
|
|
# 'use_space': openapi.Schema(type=openapi.TYPE_BOOLEAN, default=False),
|
|
# }
|
|
# )
|
|
# )
|
|
# @api_view(['POST'])
|
|
# @authentication_classes([SessionAuthentication, TokenAuthentication])
|
|
# @permission_classes([IsAuthenticated])
|
|
# def addMeeting(request):
|
|
# data = request.data.copy()
|
|
# data['creator_user'] = request.user.id
|
|
|
|
# serializer = MeetingSerializer(data=data)
|
|
# if serializer.is_valid():
|
|
# meeting = serializer.save()
|
|
# return Response({
|
|
# "message": "Meeting created successfully.",
|
|
# "meeting": serializer.data
|
|
# }, status=status.HTTP_201_CREATED)
|
|
|
|
# return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from core.models.Invitation import Invitation # Ensure Invitation is imported
|
|
from django.contrib.auth.models import User
|
|
from django.db import transaction
|
|
|
|
@swagger_auto_schema(
|
|
method='post',
|
|
request_body=openapi.Schema(
|
|
type=openapi.TYPE_OBJECT,
|
|
required=['name', 'description', 'date_time', 'user_ids'],
|
|
properties={
|
|
'name': openapi.Schema(type=openapi.TYPE_STRING, default='Sprint Planning'),
|
|
'description': openapi.Schema(type=openapi.TYPE_STRING, default='Discuss the next sprint'),
|
|
'date_time': openapi.Schema(type=openapi.TYPE_STRING, format='date-time', default='2025-06-01T14:00:00Z'),
|
|
'space': openapi.Schema(type=openapi.TYPE_INTEGER, default=1),
|
|
'asset_bundle': openapi.Schema(type=openapi.TYPE_INTEGER, default=1),
|
|
'use_space': openapi.Schema(type=openapi.TYPE_BOOLEAN, default=False),
|
|
'user_ids': openapi.Schema(
|
|
type=openapi.TYPE_ARRAY,
|
|
items=openapi.Items(type=openapi.TYPE_INTEGER),
|
|
description='List of user IDs to invite'
|
|
),
|
|
}
|
|
)
|
|
)
|
|
@api_view(['POST'])
|
|
@authentication_classes([SessionAuthentication, TokenAuthentication])
|
|
@permission_classes([IsAuthenticated])
|
|
def addMeeting(request):
|
|
data = request.data.copy()
|
|
user_ids = data.pop('user_ids', [])
|
|
data['creator_user'] = request.user.id
|
|
|
|
serializer = MeetingSerializer(data=data)
|
|
|
|
if serializer.is_valid():
|
|
try:
|
|
with transaction.atomic():
|
|
meeting = serializer.save()
|
|
|
|
for user_id in user_ids:
|
|
try:
|
|
user = User.objects.get(id=user_id)
|
|
|
|
# Create the invitation
|
|
Invitation.objects.create(
|
|
user=user,
|
|
meeting=meeting,
|
|
is_admin=False,
|
|
is_sms_sent=True # mark true if SMS sent below
|
|
)
|
|
|
|
# Send SMS to user.username (which is a phone number)
|
|
sms_result = send_sms(
|
|
to_number=user.username,
|
|
name=user.get_full_name() or user.username,
|
|
date_time=str(meeting.date_time),
|
|
title=meeting.name
|
|
)
|
|
|
|
print(f"SMS sent to {user.username}: {sms_result}")
|
|
|
|
except User.DoesNotExist:
|
|
return Response(
|
|
{"error": f"User with id {user_id} does not exist."},
|
|
status=status.HTTP_400_BAD_REQUEST
|
|
)
|
|
|
|
return Response({
|
|
"message": "Meeting created successfully with invitations and SMS sent.",
|
|
"meeting": serializer.data
|
|
}, status=status.HTTP_201_CREATED)
|
|
|
|
except Exception as e:
|
|
return Response(
|
|
{"error": str(e)},
|
|
status=status.HTTP_500_INTERNAL_SERVER_ERROR
|
|
)
|
|
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
|
|
|
|
|
# @swagger_auto_schema(
|
|
# method='get',
|
|
# operation_description="Get meetings created by or invited to the authenticated user.",
|
|
# responses={200: openapi.Response('Success', MeetingSerializer(many=True))}
|
|
# )
|
|
@api_view(['GET'])
|
|
@authentication_classes([SessionAuthentication, TokenAuthentication])
|
|
@permission_classes([IsAuthenticated])
|
|
def get_user_meetings(request):
|
|
try:
|
|
user = request.user
|
|
|
|
# Meetings the user created
|
|
created_meetings = Meeting.objects.filter(creator_user=user)
|
|
|
|
# Meetings the user was invited to
|
|
# invited_meetings = Meeting.objects.filter(invitation__user=user)
|
|
invited_meetings = Meeting.objects.filter(invitations__user=user)
|
|
|
|
|
|
# Combine both querysets and eliminate duplicates
|
|
all_meetings = (created_meetings | invited_meetings).distinct()
|
|
|
|
serializer = MeetingSerializerFull(all_meetings, many=True)
|
|
return Response({"meetings": serializer.data}, status=status.HTTP_200_OK)
|
|
|
|
except Exception as e:
|
|
return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@swagger_auto_schema(
|
|
method='post',
|
|
request_body=openapi.Schema(
|
|
type=openapi.TYPE_OBJECT,
|
|
required=['meeting_id', 'name', 'description', 'date_time', 'user_ids'],
|
|
properties={
|
|
'meeting_id': openapi.Schema(type=openapi.TYPE_INTEGER, default=1),
|
|
'name': openapi.Schema(type=openapi.TYPE_STRING, default='Updated Meeting'),
|
|
'description': openapi.Schema(type=openapi.TYPE_STRING, default='Updated description'),
|
|
'date_time': openapi.Schema(type=openapi.TYPE_STRING, format='date-time', default='2025-06-01T16:00:00Z'),
|
|
'space': openapi.Schema(type=openapi.TYPE_INTEGER, default=1),
|
|
'asset_bundle': openapi.Schema(type=openapi.TYPE_INTEGER, default=1),
|
|
'use_space': openapi.Schema(type=openapi.TYPE_BOOLEAN, default=False),
|
|
'user_ids': openapi.Schema(
|
|
type=openapi.TYPE_ARRAY,
|
|
items=openapi.Items(type=openapi.TYPE_INTEGER),
|
|
description='List of user IDs to invite'
|
|
),
|
|
}
|
|
)
|
|
)
|
|
@api_view(['POST'])
|
|
@authentication_classes([SessionAuthentication, TokenAuthentication])
|
|
@permission_classes([IsAuthenticated])
|
|
def editMeeting(request):
|
|
data = request.data.copy()
|
|
user_ids = data.pop('user_ids', [])
|
|
meeting_id = data.get('meeting_id')
|
|
|
|
try:
|
|
meeting = Meeting.objects.get(id=meeting_id)
|
|
except Meeting.DoesNotExist:
|
|
return Response({"error": "Meeting not found"}, status=status.HTTP_404_NOT_FOUND)
|
|
|
|
serializer = MeetingSerializer(meeting, data=data, partial=True)
|
|
|
|
if serializer.is_valid():
|
|
try:
|
|
with transaction.atomic():
|
|
updated_meeting = serializer.save()
|
|
|
|
# Remove old invitations and add new ones
|
|
Invitation.objects.filter(meeting=meeting).delete()
|
|
|
|
for user_id in user_ids:
|
|
try:
|
|
user = User.objects.get(id=user_id)
|
|
Invitation.objects.create(
|
|
user=user,
|
|
meeting=meeting,
|
|
is_admin=False,
|
|
is_sms_sent=True
|
|
)
|
|
|
|
# Send SMS notification
|
|
sms_result = send_sms(
|
|
to_number=user.username,
|
|
name=user.get_full_name() or user.username,
|
|
date_time=str(meeting.date_time),
|
|
title=meeting.name
|
|
)
|
|
|
|
print(f"SMS sent to {user.username}: {sms_result}")
|
|
|
|
except User.DoesNotExist:
|
|
return Response(
|
|
{"error": f"User with id {user_id} does not exist."},
|
|
status=status.HTTP_400_BAD_REQUEST
|
|
)
|
|
|
|
return Response({
|
|
"message": "Meeting updated successfully with new invitations and SMS sent.",
|
|
"meeting": serializer.data
|
|
}, status=status.HTTP_200_OK)
|
|
|
|
except Exception as e:
|
|
return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
|
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|