From 21992f540fa06fa002166f889ca691795ff70fe2 Mon Sep 17 00:00:00 2001 From: pablohashescobar <118773738+pablohashescobar@users.noreply.github.com> Date: Tue, 2 May 2023 00:52:36 +0530 Subject: [PATCH] chore: importer user invite email (#972) --- apiserver/plane/bgtasks/importer_task.py | 10 +++- apiserver/plane/bgtasks/user_welcome_task.py | 50 ++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 apiserver/plane/bgtasks/user_welcome_task.py diff --git a/apiserver/plane/bgtasks/importer_task.py b/apiserver/plane/bgtasks/importer_task.py index fba43f6e4..b27787635 100644 --- a/apiserver/plane/bgtasks/importer_task.py +++ b/apiserver/plane/bgtasks/importer_task.py @@ -27,6 +27,7 @@ from plane.db.models import ( User, ) from .workspace_invitation_task import workspace_invitation +from plane.bgtasks.user_welcome_task import send_welcome_email @shared_task @@ -40,7 +41,7 @@ def service_importer(service, importer_id): # Check if we need to import users as well if len(users): - # For all invited users create the uers + # For all invited users create the users new_users = User.objects.bulk_create( [ User( @@ -56,6 +57,13 @@ def service_importer(service, importer_id): ignore_conflicts=True, ) + [ + send_welcome_email.delay( + user, True, f"{user.email} was imported to Plane from {service}" + ) + for user in new_users + ] + workspace_users = User.objects.filter( email__in=[ user.get("email").strip().lower() diff --git a/apiserver/plane/bgtasks/user_welcome_task.py b/apiserver/plane/bgtasks/user_welcome_task.py new file mode 100644 index 000000000..cad2aed1a --- /dev/null +++ b/apiserver/plane/bgtasks/user_welcome_task.py @@ -0,0 +1,50 @@ +# Django imports +from django.conf import settings +from django.core.mail import EmailMultiAlternatives +from django.template.loader import render_to_string +from django.utils.html import strip_tags + +# Third party imports +from celery import shared_task +from sentry_sdk import capture_exception +from slack_sdk import WebClient +from slack_sdk.errors import SlackApiError + +@shared_task +def send_welcome_email(instance, created, message): + try: + if created and not instance.is_bot: + first_name = instance.first_name.capitalize() + to_email = instance.email + from_email_string = settings.EMAIL_FROM + + subject = f"Welcome to Plane ✈️!" + + context = {"first_name": first_name, "email": instance.email} + + html_content = render_to_string( + "emails/auth/user_welcome_email.html", context + ) + + text_content = strip_tags(html_content) + + msg = EmailMultiAlternatives( + subject, text_content, from_email_string, [to_email] + ) + msg.attach_alternative(html_content, "text/html") + msg.send() + + # Send message on slack as well + if settings.SLACK_BOT_TOKEN: + client = WebClient(token=settings.SLACK_BOT_TOKEN) + try: + _ = client.chat_postMessage( + channel="#trackers", + text=message, + ) + except SlackApiError as e: + print(f"Got an error: {e.response['error']}") + return + except Exception as e: + capture_exception(e) + return