feat: Preparation des modèles Settings pour l'enregistrement SMTP [#17]

This commit is contained in:
Luc SORIGNET
2025-05-05 09:25:07 +02:00
parent 99a882a64a
commit eda6f587fb
33 changed files with 468 additions and 74 deletions

View File

@ -0,0 +1 @@
default_app_config = 'Settings.apps.SettingsConfig'

View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View File

@ -0,0 +1,5 @@
from django.apps import AppConfig
class SettingsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'Settings'

View File

@ -0,0 +1,17 @@
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils.translation import gettext_lazy as _
from django.conf import settings
from Establishment.models import Establishment
class SMTPSettings(models.Model):
establishment = models.ForeignKey(Establishment, on_delete=models.CASCADE)
smtp_server = models.CharField(max_length=255)
smtp_port = models.PositiveIntegerField()
smtp_user = models.CharField(max_length=255)
smtp_password = models.CharField(max_length=255)
use_tls = models.BooleanField(default=True)
use_ssl = models.BooleanField(default=False)
def __str__(self):
return f"SMTP Settings ({self.smtp_server}:{self.smtp_port})"

View File

@ -0,0 +1,7 @@
from rest_framework import serializers
from .models import SMTPSettings
class SMTPSettingsSerializer(serializers.ModelSerializer):
class Meta:
model = SMTPSettings
fields = '__all__'

View File

@ -0,0 +1,6 @@
from django.urls import path
from .views import SMTPSettingsView
urlpatterns = [
path('smtp-settings/', SMTPSettingsView.as_view(), name='smtp_settings'),
]

View 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)