Skip to content

Commit 71ac241

Browse files
committed
Organizing mail processing function code
1 parent b541d6c commit 71ac241

2 files changed

Lines changed: 11 additions & 28 deletions

File tree

portfolio/views.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
from django.utils import translation
1313
from django.utils.decorators import method_decorator
1414
from django.views.generic import TemplateView, View
15-
from email_validator import (EmailNotValidError, caching_resolver,
16-
validate_email)
1715
from validate_email import validate_email
1816
from validate_email.exceptions import Error
1917

@@ -171,9 +169,6 @@ def get(self, request, *args, **kwargs):
171169
return JsonResponse(context, safe=False)
172170

173171

174-
resolver = caching_resolver(timeout=10)
175-
176-
177172
class GetInTouchView(View):
178173
email_template_get_in_touch = "/email/get_in_touch.html"
179174

@@ -202,22 +197,18 @@ def check_email_validation_with_dns(self, email: str) -> [str, bool]:
202197
if not is_valid:
203198
raise Error("The email failed validation. Please enter the email address you actually use")
204199

205-
emailinfo = validate_email(
206-
email, check_deliverability=True, dns_resolver=resolver
207-
)
208200
logger.debug(
209-
"GetInTouchView.emailinfo.normalized :",
210-
extra={"normalized": emailinfo.normalized},
201+
"GetInTouchView email validated"
211202
)
212203

213-
return emailinfo.normalized, True
204+
return is_valid
214205

215-
except (Error, EmailNotValidError, ValueError) as e:
206+
except (Error, ValueError) as e:
216207
logger.debug(
217208
"Error :",
218209
extra={"GetInTouchView.error : ": str(e)},
219210
)
220-
return "", False
211+
return is_valid
221212

222213
def post(self, request, *args, **kwargs):
223214
pattern = re.compile("^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$")
@@ -228,7 +219,7 @@ def post(self, request, *args, **kwargs):
228219
subject = request.POST.get("subject", "")
229220
message = request.POST.get("message", "")
230221

231-
_, rt = self.check_email_validation_with_dns(emailfrom)
222+
rt = self.check_email_validation_with_dns(emailfrom)
232223

233224
if not rt:
234225
logger.debug(

users/views/users_forms.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
from django import forms
77
from django.conf import settings
88
from django.utils.translation import gettext_lazy as _
9-
from email_validator import (EmailNotValidError, caching_resolver,
10-
validate_email)
119
from validate_email import validate_email
1210
from validate_email.exceptions import Error
1311

@@ -19,9 +17,6 @@
1917

2018
logger = logging.getLogger(getattr(settings, "USERS_LOGGER", "django"))
2119

22-
resolver = caching_resolver(timeout=10)
23-
24-
2520
class RegisterForm(forms.Form):
2621
email = forms.EmailField(
2722
label="Email",
@@ -79,7 +74,7 @@ class RegisterForm(forms.Form):
7974
def check_email_validation_with_dns(self, email: str) -> [str, bool]:
8075
try:
8176
if email is None:
82-
raise ValueError("Email is None type")
77+
return False
8378
logger.debug(
8479
"check_email_validation_with_dns email :", extra={"email": email}
8580
)
@@ -99,28 +94,25 @@ def check_email_validation_with_dns(self, email: str) -> [str, bool]:
9994
if not is_valid:
10095
raise Error("The email failed validation. Please enter the email address you actually use")
10196

102-
emailinfo = validate_email(
103-
email, check_deliverability=True, dns_resolver=resolver
104-
)
10597
logger.debug(
106-
"emailinfo.normalized :", extra={"normalized": emailinfo.normalized}
98+
"email validated"
10799
)
108-
return emailinfo.normalized, True
100+
return is_valid
109101

110-
except (Error, EmailNotValidError, ValueError) as e:
102+
except (Error, ValueError) as e:
111103
logger.debug(
112104
"Error :",
113105
extra={"error : ": str(e)},
114106
)
115-
return "", False
107+
return is_valid
116108

117109
def clean(self):
118110
cleaned_data = super(RegisterForm, self).clean()
119111
email = cleaned_data.get("email")
120112
password = cleaned_data.get("password")
121113
password_confirm = cleaned_data.get("password_confirm")
122114

123-
_, rt = self.check_email_validation_with_dns(email)
115+
rt = self.check_email_validation_with_dns(email)
124116
if not rt:
125117
logger.debug(
126118
"The email failed validation. Please enter the email address you actually use",

0 commit comments

Comments
 (0)