From c7a42c91752ee5c16d0f597b3c60eba3488bd703 Mon Sep 17 00:00:00 2001 From: Mohamed-Hacene Date: Tue, 17 Sep 2024 10:21:42 +0200 Subject: [PATCH] feat: send welcome mail when user created through representative --- backend/tprm/serializers.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/backend/tprm/serializers.py b/backend/tprm/serializers.py index cdd0ab0c1..a5218959f 100644 --- a/backend/tprm/serializers.py +++ b/backend/tprm/serializers.py @@ -4,7 +4,8 @@ from core.serializer_fields import FieldsRelatedField from core.serializers import BaseModelSerializer from core.utils import RoleCodename, UserGroupCodename -from iam.models import Folder, Role, RoleAssignment, User, UserGroup +from iam.models import Folder, Role, RoleAssignment, UserGroup +from django.contrib.auth import get_user_model from tprm.models import Entity, EntityAssessment, Representative, Solution from django.utils.translation import gettext_lazy as _ from ciso_assistant.settings import EMAIL_HOST, EMAIL_HOST_RESCUE @@ -13,6 +14,8 @@ logger = structlog.get_logger(__name__) +User = get_user_model() + class EntityReadSerializer(BaseModelSerializer): folder = FieldsRelatedField() @@ -174,12 +177,16 @@ class RepresentativeWriteSerializer(BaseModelSerializer): def _create_or_update_user(self, instance, user): if not user: return - user, created = User.objects.get_or_create( + user = User.objects.filter( email=instance.email, - ) - user.first_name = instance.first_name - user.last_name = instance.last_name - user.is_third_party = True + ).first() + if not user: + user = User.objects.create_user( + email=instance.email, + first_name=instance.first_name, + last_name=instance.last_name, + ) + user.is_third_party = True user.save() instance.user = user instance.save()