Skip to content

Commit

Permalink
Add task to send org invite email
Browse files Browse the repository at this point in the history
  • Loading branch information
pxwxnvermx committed Sep 25, 2023
1 parent 4ed36d1 commit 761d430
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
33 changes: 33 additions & 0 deletions commcare_connect/organization/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from allauth.utils import build_absolute_uri
from django.conf import settings
from django.core.mail import send_mail
from django.urls import reverse

from commcare_connect.organization.models import UserOrganizationMembership
from commcare_connect.users.models import User
from config import celery_app


@celery_app.task()
def send_org_invite(membership_id, host_user_id):
membership = UserOrganizationMembership.objects.get(pk=membership_id)
host_user = User.objects.get(pk=host_user_id)
if not membership.user.email:
return
location = reverse("organization:accept_invite", args=(membership.organization.slug, membership.invite_id))
invite_url = build_absolute_uri(None, location)
message = f"""Hi,
You have been invited to join {membership.organization.name} on Commcare Connect by {host_user.name}.
The invite can be accepted by visiting the link.
{invite_url}
Thank You,
Commcare Connect"""
send_mail(
subject=f"{host_user.name} has invite you to join {membership.organization.name}",
message=message,
recipient_list=[membership.user.email],
from_email=settings.DEFAULT_FROM_EMAIL,
)
11 changes: 6 additions & 5 deletions commcare_connect/organization/views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse
from django.utils.translation import gettext
from rest_framework.decorators import api_view

from commcare_connect.organization.decorators import org_admin_required
from commcare_connect.organization.forms import MembershipForm, OrganizationChangeForm
from commcare_connect.organization.models import Organization, UserOrganizationMembership
from commcare_connect.organization.tasks import send_org_invite


@org_admin_required
Expand Down Expand Up @@ -41,19 +41,20 @@ def add_members_form(request, org_slug):
if form.is_valid():
form.instance.organization = org
form.save()
send_org_invite.delay(membership_id=form.instance.pk, host_user_id=request.user.pk)

return redirect(reverse("organization:home", args=(org_slug,)))
return redirect("organization:home", org_slug)


@login_required
def accept_invite(request, invite_id):
def accept_invite(request, org_slug, invite_id):
membership = get_object_or_404(UserOrganizationMembership, invite_id=invite_id)
organization = membership.organization

if membership.accepted:
return redirect(reverse("organization:home", args=(organization.slug,)))
return redirect("organization:home", org_slug)

membership.accepted = True
membership.save()
messages.success(request, message=f"Accepted invite for joining {organization.slug} organization.")
return redirect("organization:home", args=(organization.slug,))
return redirect("organization:home", org_slug)

0 comments on commit 761d430

Please sign in to comment.