Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance invitation feature with email notifications and referral links #3220

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@


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": f"Failed to send invitation: {str(e)}",
"email_status": "error",
},
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show resolved Hide resolved
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 %}
Loading