mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-01-28 23:43:22 +00:00
feat: Preparation des modèles Settings pour l'enregistrement SMTP [#17]
This commit is contained in:
55
Back-End/Settings/views.py
Normal file
55
Back-End/Settings/views.py
Normal file
@ -0,0 +1,55 @@
|
||||
from drf_yasg.utils import swagger_auto_schema
|
||||
from drf_yasg import openapi
|
||||
from .models import SMTPSettings
|
||||
from .serializers import SMTPSettingsSerializer
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import status
|
||||
|
||||
class SMTPSettingsView(APIView):
|
||||
"""
|
||||
API pour gérer les paramètres SMTP.
|
||||
"""
|
||||
|
||||
@swagger_auto_schema(
|
||||
operation_description="Récupérer les paramètres SMTP",
|
||||
responses={
|
||||
200: SMTPSettingsSerializer(),
|
||||
404: openapi.Response(description="Aucun paramètre SMTP trouvé."),
|
||||
500: openapi.Response(description="Erreur interne du serveur."),
|
||||
},
|
||||
)
|
||||
def get(self, request):
|
||||
try:
|
||||
smtp_settings = SMTPSettings.objects.first()
|
||||
if not smtp_settings:
|
||||
return Response({'error': 'Aucun paramètre SMTP trouvé.'}, status=status.HTTP_404_NOT_FOUND)
|
||||
serializer = SMTPSettingsSerializer(smtp_settings)
|
||||
return Response(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(
|
||||
operation_description="Créer ou mettre à jour les paramètres SMTP",
|
||||
request_body=SMTPSettingsSerializer,
|
||||
responses={
|
||||
200: SMTPSettingsSerializer(),
|
||||
400: openapi.Response(description="Données invalides."),
|
||||
500: openapi.Response(description="Erreur interne du serveur."),
|
||||
},
|
||||
)
|
||||
def post(self, request):
|
||||
data = request.data
|
||||
try:
|
||||
smtp_settings = SMTPSettings.objects.first()
|
||||
if smtp_settings:
|
||||
serializer = SMTPSettingsSerializer(smtp_settings, data=data)
|
||||
else:
|
||||
serializer = SMTPSettingsSerializer(data=data)
|
||||
|
||||
if serializer.is_valid():
|
||||
serializer.save()
|
||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||
except Exception as e:
|
||||
return Response({'error': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
||||
Reference in New Issue
Block a user