mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-01-29 16:03:21 +00:00
67 lines
2.3 KiB
Python
67 lines
2.3 KiB
Python
from django.http.response import JsonResponse
|
|
from rest_framework.views import APIView
|
|
from rest_framework.parsers import JSONParser
|
|
from django.core.mail import send_mail
|
|
from django.utils.html import strip_tags
|
|
from django.conf import settings
|
|
from rest_framework.response import Response
|
|
from rest_framework import status
|
|
|
|
from .models import *
|
|
|
|
from GestionMessagerie.serializers import MessageSerializer
|
|
|
|
from N3wtSchool import bdd
|
|
|
|
class MessagerieView(APIView):
|
|
def get(self, request, profile_id):
|
|
messagesList = bdd.getObjects(_objectName=Messagerie, _columnName='destinataire__id', _value=profile_id)
|
|
messages_serializer = MessageSerializer(messagesList, many=True)
|
|
return JsonResponse(messages_serializer.data, safe=False)
|
|
|
|
class MessageView(APIView):
|
|
def post(self, request):
|
|
message_data=JSONParser().parse(request)
|
|
message_serializer = MessageSerializer(data=message_data)
|
|
|
|
if message_serializer.is_valid():
|
|
message_serializer.save()
|
|
|
|
return JsonResponse('Nouveau Message ajouté', safe=False)
|
|
|
|
return JsonResponse(message_serializer.errors, safe=False)
|
|
|
|
class MessageSimpleView(APIView):
|
|
def get(self, request, id):
|
|
message=bdd.getObject(Messagerie, "id", id)
|
|
message_serializer=MessageSerializer(message)
|
|
return JsonResponse(message_serializer.data, safe=False)
|
|
|
|
class SendEmailView(APIView):
|
|
"""
|
|
API pour envoyer des emails aux parents et professeurs.
|
|
"""
|
|
def post(self, request):
|
|
data = request.data
|
|
recipients = data.get('recipients', [])
|
|
subject = data.get('subject', 'Notification')
|
|
message = data.get('message', '')
|
|
|
|
if not recipients or not message:
|
|
return Response({'error': 'Les destinataires et le message sont requis.'}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
try:
|
|
plain_message = strip_tags(message)
|
|
send_mail(
|
|
subject,
|
|
plain_message,
|
|
settings.EMAIL_HOST_USER,
|
|
recipients,
|
|
html_message=message,
|
|
fail_silently=False,
|
|
)
|
|
return Response({'message': 'Email envoyé avec succès.'}, status=status.HTTP_200_OK)
|
|
except Exception as e:
|
|
return Response({'error': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
|
|