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

invite friend api viewset and url #1364

Merged
merged 2 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion blt/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
StatsApiViewset,
UrlCheckApiViewset,
BugHuntApiViewset,
BugHuntApiViewsetV2
BugHuntApiViewsetV2,
InviteFriendApiViewset
)
from company.views import ShowBughuntView
from website.alternative_views import (
Expand Down Expand Up @@ -303,6 +304,7 @@
re_path(r"^api/v1/issue/like/(?P<id>\w+)/$", LikeIssueApiView.as_view(), name="like_issue"),
re_path(r"^api/v1/issue/flag/(?P<id>\w+)/$", FlagIssueApiView.as_view(), name="flag_issue"),
re_path(r"^api/v1/leaderboard/$",LeaderboardApiViewSet.as_view(),name="leaderboard"),
re_path(r"^api/v1/invite_friend/",InviteFriendApiViewset.as_view(),name="invite_friend"),

re_path(r"^scoreboard/$", ScoreboardView.as_view(), name="scoreboard"),
re_path(r"^issue/$", IssueCreate.as_view(), name="issue"),
Expand Down
53 changes: 51 additions & 2 deletions website/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.db.models import Sum
from django.contrib.auth.models import AnonymousUser
from django.db.models import Q
from django.contrib.sites.shortcuts import get_current_site

from rest_framework import viewsets, filters, status
from rest_framework.response import Response
Expand All @@ -22,7 +23,8 @@
Domain,
IssueScreenshot,
Hunt,
HuntPrize
HuntPrize,
InviteFriend
)
from website.serializers import (
IssueSerializer,
Expand Down Expand Up @@ -552,4 +554,51 @@ def get(self,request,*args,**kwargs):
page = paginator.paginate_queryset(hunts,request)

return paginator.get_paginated_response(page)


class InviteFriendApiViewset(APIView):

def post(self,request,*args,**kwargs):

email = request.POST.get("email")
already_exists = User.objects.filter(email=email).exists()

if already_exists:
return Response("USER EXISTS",status=409)

site = get_current_site(self.request)


invite = InviteFriend.objects.create(
sender = request.user,
recipient = email,
sent = False
)

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],
)

if (mail_status):
invite.sent = True
invite.save()

if (
mail_status
and InviteFriend.objects.filter(sender=self.request.user,sent=True).count() == 2
):
Points.objects.create(user=self.request.user, score=1)
InviteFriend.objects.filter(sender=self.request.user).delete()

return Response({
"title": "SUCCESS",
"Points": "+1",
"message": "An email has been sent to your friend. Keep inviting your friends and get points!"
},status=200)

Loading