From fd4b6a68b1ae7f3f5c928332127be387694b7b04 Mon Sep 17 00:00:00 2001 From: Kuroshini Date: Tue, 4 Feb 2020 21:30:19 +0300 Subject: [PATCH] email on new user --- apps/account/models.py | 35 ++++++-- apps/account/serializers/back.py | 7 +- apps/account/tasks.py | 19 +++++ project/settings/base.py | 1 + project/templates/account/change_email.html | 2 +- .../account/invite_est_team_new_user.html | 83 +++++++++++++++++++ .../account/password_change_email.html | 2 +- .../account/password_reset_email.html | 2 +- .../authorization/confirm_email.html | 2 +- project/templates/news/news_email.html | 2 +- .../templates/notification/update_email.html | 2 +- 11 files changed, 144 insertions(+), 13 deletions(-) create mode 100644 project/templates/account/invite_est_team_new_user.html diff --git a/apps/account/models.py b/apps/account/models.py index 61c85b87..73a0d9ba 100644 --- a/apps/account/models.py +++ b/apps/account/models.py @@ -114,17 +114,13 @@ class UserManager(BaseUserManager): obj.save() return obj - def invite_for_team(self, email: str, establishment: Establishment): + def invite_for_team(self, email: str, establishment: Establishment, country_code: str): created = False user = User.objects.filter(email=email).first() if user is None: from utils.methods import string_random user = self.make(email, string_random(), True, string_random(), email_confirmed=True) created = True - if created: - pass # TODO: send email with password reset and role id - else: - pass # TODO: send email-notification with role activation link user_role_defaults = {'for_team': True, 'state': UserRole.PENDING} role, is_role_created = Role.objects.get_or_create(role=Role.ESTABLISHMENT_ADMINISTRATOR) user_role, is_user_role_created = UserRole.objects.get_or_create(user=user, establishment=establishment, @@ -132,6 +128,15 @@ class UserManager(BaseUserManager): if not is_user_role_created: from rest_framework.serializers import ValidationError raise ValidationError({'detail': f'User with this role already exists. State: {user_role.state}'}) + + if created: + from account.tasks import send_team_invite_to_new_user + if settings.USE_CELERY: + send_team_invite_to_new_user.delay(user.pk, 'en-GB', user_role.pk, establishment.name) + else: + send_team_invite_to_new_user(user.pk, country_code, user_role.pk, establishment.name) + else: + pass # TODO: send email-notification with role activation link return user @@ -399,6 +404,26 @@ class User(PhoneModelMixin, AbstractUser): template_name=settings.RESETTING_TOKEN_TEMPLATE, context=context), get_template(settings.RESETTING_TOKEN_TEMPLATE).render(context) + def invite_new_establishment_member_template(self, country_code, username, subject, restaurant_name, user_role_id): + """Template for newly created user establishment team invite""" + context = {'token': self.reset_password_token, + 'country_code': country_code, + 'restaurant_name': restaurant_name, + 'user_role_id': user_role_id} + context.update(self.base_template(country_code, username, subject)) + return render_to_string( + template_name=settings.NEW_USER_FOR_ESTABLISHMENT_TEAM_TEMPLATE, + context=context), get_template(settings.NEW_USER_FOR_ESTABLISHMENT_TEAM_TEMPLATE).render(context) + + # def invite_establishment_member_template(self, country_code, username, subject): + # """Template existing user establishment team invite""" + # context = {'token': self.reset_password_token, + # 'country_code': country_code} + # context.update(self.base_template(country_code, username, subject)) + # return render_to_string( + # template_name=settings.NEW_USER_FOR_ESTABLISHMENT_TEAM_TEMPLATE, + # context=context), get_template(settings.RESETTING_TOKEN_TEMPLATE).render(context) + def notify_password_changed_template(self, country_code, username, subject): """Get notification email template""" context = {'contry_code': country_code} diff --git a/apps/account/serializers/back.py b/apps/account/serializers/back.py index ed5fe89d..bbb1626b 100644 --- a/apps/account/serializers/back.py +++ b/apps/account/serializers/back.py @@ -1,5 +1,6 @@ """Back account serializers""" from rest_framework import serializers +from django.db import transaction from account import models from account.serializers import RoleBaseSerializer, UserSerializer, subscriptions_handler @@ -187,5 +188,7 @@ class InviteTeamSerializer(serializers.ModelSerializer): ) def create(self, validated_data): - establishment = self.context['view'].kwargs['establishment'] - return models.User.objects.invite_for_team(validated_data['email'], establishment) + with transaction.atomic(): + establishment = self.context['view'].kwargs['establishment'] + country_code = self.context['request'].country_code + return models.User.objects.invite_for_team(validated_data['email'], establishment, country_code) diff --git a/apps/account/tasks.py b/apps/account/tasks.py index 61610c82..6667de39 100644 --- a/apps/account/tasks.py +++ b/apps/account/tasks.py @@ -33,6 +33,25 @@ def send_reset_password_email(user_id, country_code): send_email(user_id, 'Password_resetting', 'reset_password_template', country_code) +@shared_task +def send_team_invite_to_new_user(user_id, country_code, user_role_id, restaurant_name): + """Send email to establishment team member with resetting password and accepting role link""" + user = User.objects.get(id=user_id) + subject = _(f'GAULT&MILLAU INVITES YOU TO MANAGE {restaurant_name}') + message = user.invite_new_establishment_member_template(country_code, user.username, + subject, restaurant_name, user_role_id) + user.send_email(subject=subject, + message=message, + emails=None) + + +@shared_task +def send_team_invite_to_existing_user(user_id, country_code, user_role_id, restaurant_name): + """Send email to establishment team member with resetting password and accepting role link""" + send_email(user_id, f'GAULT&MILLAU INVITES YOU TO MANAGE {restaurant_name}', + 'invite_establishment_member_template', country_code) + + @shared_task def confirm_new_email_address(user_id, country_code): """Send email to user new email.""" diff --git a/project/settings/base.py b/project/settings/base.py index 2f36d428..f853472e 100644 --- a/project/settings/base.py +++ b/project/settings/base.py @@ -453,6 +453,7 @@ PASSWORD_RESET_TIMEOUT_DAYS = 1 # TEMPLATES RESETTING_TOKEN_TEMPLATE = 'account/password_reset_email.html' +NEW_USER_FOR_ESTABLISHMENT_TEAM_TEMPLATE = 'account/invite_est_team_new_user.html' CHANGE_EMAIL_TEMPLATE = 'account/change_email.html' CONFIRM_EMAIL_TEMPLATE = 'authorization/confirm_email.html' NEWS_EMAIL_TEMPLATE = 'news/news_email.html' diff --git a/project/templates/account/change_email.html b/project/templates/account/change_email.html index 9f412a8f..fdbb648f 100644 --- a/project/templates/account/change_email.html +++ b/project/templates/account/change_email.html @@ -47,7 +47,7 @@ thumb Follow us -
You can also us on our social network below + -