mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-01-28 23:43:22 +00:00
chore: Synchronisation IMAP
This commit is contained in:
@ -6,7 +6,7 @@ import re
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import status
|
||||
from rest_framework.exceptions import NotFound
|
||||
from Settings.models import SMTPSettings
|
||||
from Settings.models import MailSettings
|
||||
from Establishment.models import Establishment # Importer le modèle Establishment
|
||||
|
||||
def getConnection(id_establishement):
|
||||
@ -15,14 +15,14 @@ def getConnection(id_establishement):
|
||||
establishment = Establishment.objects.get(id=id_establishement)
|
||||
|
||||
# Récupérer les paramètres SMTP associés à l'établissement
|
||||
smtp_settings = SMTPSettings.objects.get(establishment=establishment)
|
||||
smtp_settings = MailSettings.objects.get(establishment=establishment)
|
||||
|
||||
# Créer une connexion SMTP avec les paramètres récupérés
|
||||
connection = get_connection(
|
||||
host=smtp_settings.smtp_server,
|
||||
port=smtp_settings.smtp_port,
|
||||
username=smtp_settings.smtp_user,
|
||||
password=smtp_settings.smtp_password,
|
||||
username=smtp_settings.mail_user,
|
||||
password=smtp_settings.mail_password,
|
||||
use_tls=smtp_settings.use_tls,
|
||||
use_ssl=smtp_settings.use_ssl
|
||||
)
|
||||
@ -30,7 +30,7 @@ def getConnection(id_establishement):
|
||||
|
||||
except Establishment.DoesNotExist:
|
||||
raise NotFound(f"Aucun établissement trouvé avec l'ID {id_establishement}")
|
||||
except SMTPSettings.DoesNotExist:
|
||||
except MailSettings.DoesNotExist:
|
||||
raise NotFound(f"Aucun paramètre SMTP trouvé pour l'établissement {id_establishement}")
|
||||
|
||||
|
||||
@ -153,4 +153,25 @@ def isValid(message, fiche_inscription):
|
||||
responsable = eleve.getMainGuardian()
|
||||
mailReponsableAVerifier = responsable.mail
|
||||
|
||||
return responsableMail == mailReponsableAVerifier and str(idMail) == str(fiche_inscription.eleve.id)
|
||||
return responsableMail == mailReponsableAVerifier and str(idMail) == str(fiche_inscription.eleve.id)
|
||||
|
||||
def test_mailbox_uri(uri):
|
||||
"""
|
||||
Teste la validité d'une URI IMAP en tentant une connexion réelle.
|
||||
Retourne True si la connexion réussit, False sinon.
|
||||
"""
|
||||
from django_mailbox.models import Mailbox
|
||||
try:
|
||||
mailbox = Mailbox(uri=uri)
|
||||
conn = mailbox.get_connection()
|
||||
# Essaye de récupérer un message (ou juste la connexion)
|
||||
# Pour IMAP, get_message() va ouvrir et fermer la connexion
|
||||
try:
|
||||
next(conn.get_message())
|
||||
except StopIteration:
|
||||
# Aucun message, mais connexion OK
|
||||
pass
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"Erreur de connexion IMAP : {e}")
|
||||
return False
|
||||
Reference in New Issue
Block a user