From 63d8af6031d24585a96420ad3d47c8c44be46410 Mon Sep 17 00:00:00 2001 From: Jarmil Date: Mon, 9 Dec 2019 12:23:03 +0100 Subject: [PATCH] FIX: chyba pri odeslani mailu chycena, logovana --- nalodeni/auth.py | 39 +++++++++++++++++++++------------------ nalodeni/views.py | 5 ++--- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/nalodeni/auth.py b/nalodeni/auth.py index 16fc7e2..f2179b0 100644 --- a/nalodeni/auth.py +++ b/nalodeni/auth.py @@ -22,6 +22,16 @@ logger = logging.getLogger(__name__) USER_MODEL = get_user_model() +def send_mail_safe(subject, message, from_email, recipient_list): + """ Posle mail. Vyskytne-li se pri odesilani chyba, vypise ji do logu. Vrati uspesnost. """ + try: + send_mail(subject, message, from_email, recipient_list) + except Exception as e: + logger.error("send_mail failed with error: %s" % str(e)) + return False + return True + + class EmailTokenAuthBackend: """ Provides authorization via email workflow. @@ -113,7 +123,6 @@ def sendLoginToken(user): user.etStamp = datetime.now() user.save() - emailSubj = 'Piráti - nalodění - přihlašovací odkaz' emailBody = """\ Dobrý den, @@ -128,16 +137,15 @@ S pozdravem Piráti """ - send_mail( - emailSubj, + send_mail_safe( + 'Piráti - nalodění - přihlašovací odkaz', emailBody.format( emailToken=emailToken, baseUrl=appSettings.BASE_URL), "nalodeni@pirati.cz", - [user.email], # email to ... - fail_silently=False, + [user.email] ) - + def sendRegisterToken(f_email): """ @@ -186,7 +194,6 @@ def sendRegisterTokenReg(rt): if appSettings.TOKEN_VALID_SEC <= 60*60*24*1: word_days = "den" - emailSubj = 'Piráti - nalodění - registrační odkaz' emailBody = """\ Vítej na palubě! @@ -201,18 +208,17 @@ S pozdravem Piráti """ - send_mail( - emailSubj, + send_mail_safe( + 'Piráti - nalodění - registrační odkaz', emailBody.format( emailToken=emailToken, baseUrl=appSettings.BASE_URL, days=int(math.floor(appSettings.TOKEN_VALID_SEC / (60*60*24))), word=word_days), "nalodeni@pirati.cz", - [rt.email], # email to ... - fail_silently=False, + [rt.email] ) - + def sendEmailContactVerificationToken(user): """ Send validation email to user.email_contact. """ @@ -238,7 +244,6 @@ def sendEmailContactVerificationToken(user): sha256hash.update(ecw.encode('utf-8')) emailToken = sha256hash.hexdigest() - emailSubj = 'Piráti - nalodění - ověření kontaktního emailu' emailBody = """\ Dobrý den, @@ -256,19 +261,17 @@ S pozdravem Piráti """ - send_mail( - emailSubj, + send_mail_safe( + 'Piráti - nalodění - ověření kontaktního emailu', emailBody.format( email_contact = ecw, emailToken=emailToken, baseUrl=appSettings.BASE_URL), "nalodeni@pirati.cz", - [ecw], # email to ... - fail_silently=False, + [ecw] ) return True - #TODO:: osetreni transakci (pro pripad utoku typu DDoS) diff --git a/nalodeni/views.py b/nalodeni/views.py index a44605e..02de9b4 100644 --- a/nalodeni/views.py +++ b/nalodeni/views.py @@ -29,7 +29,6 @@ from django.db.models import F from django.core.validators import validate_email from django.core.exceptions import ValidationError -from django.core.mail import send_mail from django.core.cache import cache import logging @@ -572,10 +571,10 @@ def souhlas(request): request.user.dc_stamp = None request.user.dc_undo_stamp = datetime.now() messages.info(request, "Odvolal/a jste souhlas se zpracováním osobních údajů.") - send_mail( + nalodeni_auth.send_mail_safe( "Nalodeni: %s odvolal souhlas se zpracovanim osobnich udaju" % request.user.email, "Stalo se tak {mydate}".format(mydate=request.user.dc_undo_stamp), - "nalodeni@pirati.cz", [appSettings.EMAIL_RECIPIENT_GDPR], + "nalodeni@pirati.cz", [appSettings.EMAIL_RECIPIENT_GDPR] ) request.user.save()