Exception handling and logging for SMTP errors
Should mitigate internal server errors if SMTP server fails.
This commit is contained in:
parent
f132cdbeef
commit
1c57950558
@ -6,6 +6,7 @@ from .test import Test
|
|||||||
|
|
||||||
from flask_login import current_user
|
from flask_login import current_user
|
||||||
from flask_mail import Message
|
from flask_mail import Message
|
||||||
|
from smtplib import SMTPException
|
||||||
|
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
@ -174,4 +175,7 @@ class Entry(db.Model):
|
|||||||
<p>Best wishes, <br/> SKA Refereeing</p>
|
<p>Best wishes, <br/> SKA Refereeing</p>
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
try:
|
||||||
mail.send(email)
|
mail.send(email)
|
||||||
|
except SMTPException as exception:
|
||||||
|
write('system.log', f'SMTP Error when trying to notify results to {self.get_surname()}, {self.get_first_name()} with error: {exception}')
|
@ -6,6 +6,7 @@ from flask import flash, jsonify, session
|
|||||||
from flask.helpers import url_for
|
from flask.helpers import url_for
|
||||||
from flask_login import current_user, login_user, logout_user, UserMixin
|
from flask_login import current_user, login_user, logout_user, UserMixin
|
||||||
from flask_mail import Message
|
from flask_mail import Message
|
||||||
|
from smtplib import SMTPException
|
||||||
from werkzeug.security import check_password_hash, generate_password_hash
|
from werkzeug.security import check_password_hash, generate_password_hash
|
||||||
|
|
||||||
import secrets
|
import secrets
|
||||||
@ -90,7 +91,10 @@ class User(UserMixin, db.Model):
|
|||||||
<p>SKA Refereeing</p>
|
<p>SKA Refereeing</p>
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
try:
|
||||||
mail.send(email)
|
mail.send(email)
|
||||||
|
except SMTPException as exception:
|
||||||
|
write('system.log', f'SMTP Error while trying to notify new user account creation to {self.get_username()} with error: {exception}')
|
||||||
return True, f'User {self.get_username()} was created successfully.'
|
return True, f'User {self.get_username()} was created successfully.'
|
||||||
|
|
||||||
def login(self, remember:bool=False):
|
def login(self, remember:bool=False):
|
||||||
@ -109,7 +113,6 @@ class User(UserMixin, db.Model):
|
|||||||
self.set_password(new_password)
|
self.set_password(new_password)
|
||||||
self.reset_token = secrets.token_urlsafe(16)
|
self.reset_token = secrets.token_urlsafe(16)
|
||||||
self.verification_token = secrets.token_urlsafe(16)
|
self.verification_token = secrets.token_urlsafe(16)
|
||||||
db.session.commit()
|
|
||||||
email = Message(
|
email = Message(
|
||||||
subject='RefTest | Password Reset',
|
subject='RefTest | Password Reset',
|
||||||
recipients=[self.get_email()],
|
recipients=[self.get_email()],
|
||||||
@ -142,11 +145,12 @@ class User(UserMixin, db.Model):
|
|||||||
<p>SKA Refereeing</p>
|
<p>SKA Refereeing</p>
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
try:
|
||||||
mail.send(email)
|
mail.send(email)
|
||||||
print('Password', new_password)
|
except SMTPException as exception:
|
||||||
print('Reset Token', self.reset_token)
|
write('system.log', f'SMTP Error while trying to reset password for {self.get_username()} with error: {exception}')
|
||||||
print('Verification Token', self.verification_token)
|
return jsonify({'error': f'SMTP Error: {exception}'}), 500
|
||||||
print('Reset Link', f'{url_for("admin._reset", token=self.reset_token, verification=self.verification_token, _external=True)}')
|
db.session.commit()
|
||||||
return jsonify({'success': 'Your password reset link has been generated.'}), 200
|
return jsonify({'success': 'Your password reset link has been generated.'}), 200
|
||||||
|
|
||||||
def clear_reset_tokens(self):
|
def clear_reset_tokens(self):
|
||||||
@ -182,7 +186,10 @@ class User(UserMixin, db.Model):
|
|||||||
<p>SKA Refereeing</p>
|
<p>SKA Refereeing</p>
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
try:
|
||||||
mail.send(email)
|
mail.send(email)
|
||||||
|
except SMTPException as exception:
|
||||||
|
write('system.log', f'SMTP Error when trying to delete account {username} with error: {exception}')
|
||||||
return True, message
|
return True, message
|
||||||
|
|
||||||
def update(self, password:str=None, email:str=None, notify:bool=False):
|
def update(self, password:str=None, email:str=None, notify:bool=False):
|
||||||
@ -223,5 +230,8 @@ class User(UserMixin, db.Model):
|
|||||||
<p>SKA Refereeing</p>
|
<p>SKA Refereeing</p>
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
try:
|
||||||
mail.send(message)
|
mail.send(message)
|
||||||
|
except SMTPException as exception:
|
||||||
|
write('system.log', f'SMTP Error when trying to update account {self.get_username()} with error: {exception}')
|
||||||
return True, f'Account {self.get_username()} has been updated.'
|
return True, f'Account {self.get_username()} has been updated.'
|
||||||
|
Loading…
Reference in New Issue
Block a user