Skip to content

Commit

Permalink
enhance invitation feature with email notifications and referral links (
Browse files Browse the repository at this point in the history
#3220)

* enhance invitation feature with email notifications and referral links

* enhance invitation feature with email notifications and referral links
  • Loading branch information
JisanAR03 authored Jan 13, 2025
1 parent 15a65e7 commit 6f29a67
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 32 deletions.
86 changes: 55 additions & 31 deletions website/api/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import smtplib
import uuid
from datetime import datetime
from urllib.parse import urlparse
Expand Down Expand Up @@ -652,45 +653,68 @@ def get(self, request, *args, **kwargs):


class InviteFriendApiViewset(APIView):
permission_classes = [IsAuthenticated]

def post(self, request, *args, **kwargs):
email = request.POST.get("email")
already_exists = User.objects.filter(email=email).exists()
email = request.data.get("email")

if already_exists:
return Response("USER EXISTS", status=409)
if not email:
return Response({"error": "Email is required"}, status=400)

site = get_current_site(self.request)
# Check if user already exists
if User.objects.filter(email=email).exists():
return Response({"error": "User already exists"}, status=400)

invite = InviteFriend.objects.create(sender=request.user, recipient=email, sent=False)
try:
current_site = get_current_site(request)
referral_code, created = InviteFriend.objects.get_or_create(sender=request.user)
referral_link = (
f"https://{current_site.domain}/referral/?ref={referral_code.referral_code}"
)

mail_status = send_mail(
"Inivtation to {site} from {user}".format(site=site.name, user=request.user.username),
"You have been invited by {user} to join {site} community.".format(
user=request.user.username, site=site.name
),
settings.DEFAULT_FROM_EMAIL,
[invite.recipient],
)
# Prepare email content
subject = f"Join me on {current_site.name}!"
message = render_to_string(
"email/invite_friend.txt",
{
"sender": request.user.username,
"referral_link": referral_link,
"site_name": current_site.name,
"site_domain": current_site.domain,
},
)

if mail_status:
invite.sent = True
invite.save()
# Send the invitation email
email_result = send_mail(
subject,
message,
settings.DEFAULT_FROM_EMAIL,
[email],
fail_silently=False,
)

if (
mail_status
and InviteFriend.objects.filter(sender=self.request.user, sent=True).count() == 2
):
Points.objects.create(user=self.request.user, score=1, reason="Invited friend")
InviteFriend.objects.filter(sender=self.request.user).delete()
if email_result == 1:
return Response(
{
"success": True,
"message": "Invitation sent successfully",
"referral_link": referral_link,
"email_status": "delivered",
}
)
else:
return Response(
{"error": "Email failed to send", "email_status": "failed"}, status=500
)

return Response(
{
"title": "SUCCESS",
"Points": "+1",
"message": "An email has been sent to your friend. Keep inviting your friends and get points!",
},
status=200,
)
except smtplib.SMTPException as e:
return Response(
{
"error": "Failed to send invitation email for an unexpected reason",
"email_status": "error",
},
status=500,
)


class OrganizationViewSet(viewsets.ModelViewSet):
Expand Down
17 changes: 17 additions & 0 deletions website/templates/email/invite_friend.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Hi there!

{{ sender }} has invited you to join {{ site_name }}!

{{ site_name }} is a platform where we can discover and report bugs across various applications and websites. It's an exciting journey where you can:

- Explore and report bugs
- Earn rewards and recognition for your contributions
- Join a community of like-minded individuals passionate about improving software quality

You can sign up using this special referral link:
{{ referral_link }}

Looking forward to seeing you on {{ site_name }}!

Best regards,
The {{ site_name }} Team
2 changes: 1 addition & 1 deletion website/templates/invite_friend.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@
alert("Copied to clipboard: " + copyText.value);
}
</script>
{% endblock content %}
{% endblock after_js %}

0 comments on commit 6f29a67

Please sign in to comment.