Password changed notification email

This commit is contained in:
Kuroshini 2019-10-15 16:32:47 +03:00
parent 01a32f66e8
commit 9c09f000fb
6 changed files with 58 additions and 25 deletions

View File

@ -202,6 +202,15 @@ class User(AbstractUser):
template_name=settings.RESETTING_TOKEN_TEMPLATE,
context=context)
def notify_password_changed_template(self, country_code):
"""Get notification email template"""
context = {'contry_code': country_code}
context.update(self.base_template)
return render_to_string(
template_name=settings.NOTIFICATION_PASSWORD_TEMPLATE,
context=context,
)
def confirm_email_template(self, country_code):
"""Get confirm email template"""
context = {'token': self.confirm_email_token,

View File

@ -127,6 +127,14 @@ class ChangePasswordSerializer(serializers.ModelSerializer):
except serializers.ValidationError as e:
raise serializers.ValidationError({'detail': e.detail})
else:
if settings.USE_CELERY:
tasks.send_password_changed_email(
user_id=self.instance.id,
country_code=self.context.get('request').country_code)
else:
tasks.send_password_changed_email(
user_id=self.instance.id,
country_code=self.context.get('request').country_code)
return attrs
def update(self, instance, validated_data):

View File

@ -1,8 +1,9 @@
"""Serializers for account web"""
from django.conf import settings
from django.contrib.auth import password_validation as password_validators
from rest_framework import serializers
from account import models
from account import models, tasks
from utils import exceptions as utils_exceptions
from utils.methods import username_validator
@ -68,4 +69,12 @@ class PasswordResetConfirmSerializer(serializers.ModelSerializer):
# Update user password from instance
instance.set_password(validated_data.get('password'))
instance.save()
if settings.USE_CELERY:
tasks.send_password_changed_email(
user_id=instance.id,
country_code=self.context.get('request').country_code)
else:
tasks.send_password_changed_email(
user_id=instance.id,
country_code=self.context.get('request').country_code)
return instance

View File

@ -1,47 +1,46 @@
"""Account app celery tasks."""
import inspect
import logging
from celery import shared_task
from django.utils.translation import gettext_lazy as _
from . import models
from account.models import User
logging.basicConfig(format='[%(levelname)s] %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
def send_email(user_id: int, subject: str, message_prop: str, country_code: str):
try:
user = User.objects.get(id=user_id)
user.send_email(subject=_(subject),
message=getattr(user, message_prop, lambda _: '')(country_code))
except:
cur_frame = inspect.currentframe()
cal_frame = inspect.getouterframes(cur_frame, 2)
logger.error(f'METHOD_NAME: {cal_frame[1][3]}\n'
f'DETAIL: Exception occurred for user: {user_id}')
@shared_task
def send_reset_password_email(user_id, country_code):
"""Send email to user for reset password."""
try:
user = models.User.objects.get(id=user_id)
user.send_email(subject=_('Password resetting'),
message=user.reset_password_template(country_code))
except:
logger.error(f'METHOD_NAME: {send_reset_password_email.__name__}\n'
f'DETAIL: Exception occurred for reset password: '
f'{user_id}')
send_email(user_id, 'Password_resetting', 'reset_password_template', country_code)
@shared_task
def confirm_new_email_address(user_id, country_code):
"""Send email to user new email."""
try:
user = models.User.objects.get(id=user_id)
user.send_email(subject=_('Validate new email address'),
message=user.confirm_email_template(country_code))
except:
logger.error(f'METHOD_NAME: {confirm_new_email_address.__name__}\n'
f'DETAIL: Exception occurred for user: {user_id}')
send_email(user_id, 'Confirm new email address', 'confirm_email_template', country_code)
@shared_task
def change_email_address(user_id, country_code):
"""Send email to user new email."""
try:
user = models.User.objects.get(id=user_id)
user.send_email(subject=_('Validate new email address'),
message=user.change_email_template(country_code))
except:
logger.error(f'METHOD_NAME: {change_email_address.__name__}\n'
f'DETAIL: Exception occurred for user: {user_id}')
send_email(user_id, 'Validate new email address', 'change_email_template', country_code)
@shared_task
def send_password_changed_email(user_id, country_code):
"""Send email which notifies user that his password had changed"""
send_email(user_id, 'Notify password changed', 'notify_password_changed_template', country_code)

View File

@ -406,7 +406,8 @@ PASSWORD_RESET_TIMEOUT_DAYS = 1
RESETTING_TOKEN_TEMPLATE = 'account/password_reset_email.html'
CHANGE_EMAIL_TEMPLATE = 'account/change_email.html'
CONFIRM_EMAIL_TEMPLATE = 'authorization/confirm_email.html'
NEWS_EMAIL_TEMPLATE = "news/news_email.html"
NEWS_EMAIL_TEMPLATE = 'news/news_email.html'
NOTIFICATION_PASSWORD_TEMPLATE = 'account/password_change_email.html'
# COOKIES

View File

@ -0,0 +1,7 @@
{% load i18n %}{% autoescape off %}
{% blocktrans %}You're receiving this email because your account's password address at {{ site_name }}.{% endblocktrans %}
{% trans "Thanks for using our site!" %}
{% blocktrans %}The {{ site_name }} team{% endblocktrans %}
{% endautoescape %}