refactor MailProvider model and enhance email validation in admin form
This commit is contained in:
19
api/admin.py
19
api/admin.py
@@ -1,4 +1,6 @@
|
|||||||
|
import logging
|
||||||
import smtplib
|
import smtplib
|
||||||
|
import socket
|
||||||
|
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django import forms
|
from django import forms
|
||||||
@@ -6,6 +8,8 @@ from django.core.exceptions import ValidationError
|
|||||||
|
|
||||||
from api.models import MailProvider
|
from api.models import MailProvider
|
||||||
|
|
||||||
|
_logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class MailProviderForm(forms.ModelForm):
|
class MailProviderForm(forms.ModelForm):
|
||||||
host_password = forms.CharField(widget=forms.PasswordInput(render_value=True))
|
host_password = forms.CharField(widget=forms.PasswordInput(render_value=True))
|
||||||
|
|
||||||
@@ -21,9 +25,24 @@ class MailProviderForm(forms.ModelForm):
|
|||||||
password = cleaned_data.get("host_password")
|
password = cleaned_data.get("host_password")
|
||||||
use_tls = cleaned_data.get("use_tls")
|
use_tls = cleaned_data.get("use_tls")
|
||||||
|
|
||||||
|
if host:
|
||||||
|
try:
|
||||||
|
socket.gethostbyname(host)
|
||||||
|
except socket.gaierror:
|
||||||
|
raise ValidationError(
|
||||||
|
"Invalid mail server hostname. Could not resolve host."
|
||||||
|
)
|
||||||
|
|
||||||
if host and port and user and password:
|
if host and port and user and password:
|
||||||
try:
|
try:
|
||||||
|
if port == 465:
|
||||||
|
connection = smtplib.SMTP_SSL(host, port, timeout=10)
|
||||||
|
else:
|
||||||
connection = smtplib.SMTP(host, port, timeout=10)
|
connection = smtplib.SMTP(host, port, timeout=10)
|
||||||
|
if use_tls and port == 465:
|
||||||
|
raise ValidationError(
|
||||||
|
"Port 465 should not be used with STARTTLS. Use SMTP_SSL instead or change to port 587."
|
||||||
|
)
|
||||||
if use_tls:
|
if use_tls:
|
||||||
connection.starttls()
|
connection.starttls()
|
||||||
connection.login(user, password)
|
connection.login(user, password)
|
||||||
|
18
api/migrations/0002_alter_mailprovider_host.py
Normal file
18
api/migrations/0002_alter_mailprovider_host.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
# Generated by Django 5.2.1 on 2025-05-27 21:48
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('api', '0001_initial'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='mailprovider',
|
||||||
|
name='host',
|
||||||
|
field=models.CharField(max_length=255),
|
||||||
|
),
|
||||||
|
]
|
@@ -1,7 +1,7 @@
|
|||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
class MailProvider(models.Model):
|
class MailProvider(models.Model):
|
||||||
host = models.URLField()
|
host = models.CharField(max_length=255)
|
||||||
port = models.IntegerField(default=587)
|
port = models.IntegerField(default=587)
|
||||||
host_user = models.CharField(max_length=255)
|
host_user = models.CharField(max_length=255)
|
||||||
host_password = models.CharField(max_length=255)
|
host_password = models.CharField(max_length=255)
|
||||||
|
Reference in New Issue
Block a user