Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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: 4 additions & 0 deletions blt/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
StatsApiViewset,
UrlCheckApiViewset,
BugHuntApiViewset,
BugHuntApiViewsetV2
)
from company.views import ShowBughuntView
from website.alternative_views import (
Expand Down Expand Up @@ -368,6 +369,9 @@
re_path(
r"^api/v1/hunt/$",BugHuntApiViewset.as_view(),name="hunt_details"
),
re_path(
r"^api/v2/hunts/$",BugHuntApiViewsetV2.as_view(),name="hunts_detail_v2"
),
re_path(r"^api/v1/userscore/$", website.views.get_score, name="get_score"),
re_path(r"^authenticate/", CustomObtainAuthToken.as_view()),
re_path(r"^api/v1/createwallet/$", website.views.create_wallet, name="create_wallet"),
Expand Down
76 changes: 76 additions & 0 deletions website/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@
Issue,
Domain,
IssueScreenshot,
Hunt,
HuntPrize
)
from website.serializers import (
IssueSerializer,
UserProfileSerializer,
DomainSerializer,
BugHuntPrizeSerializer,
BugHuntSerializer
)

from website.models import (
Expand Down Expand Up @@ -451,6 +455,8 @@ def post(self, request, *args, **kwargs):

class BugHuntApiViewset(APIView):

permission_classes = [AllowAny]

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

hunts = Hunt.objects.values('id','name','url','prize','logo',"starts_on","end_on").filter(is_published=True,starts_on__lte=datetime.now(),end_on__gte=datetime.now()).order_by("-prize")
Expand All @@ -477,3 +483,73 @@ def get(self,request,*args,**kwargs):
hunts = Hunt.objects.values('id','name','url','prize','logo',"starts_on","end_on").filter(is_published=True).order_by("-end_on")
return Response(hunts)


class BugHuntApiViewsetV2(APIView):

permission_classes = [AllowAny]

def serialize_hunts(self,hunts):

hunts = BugHuntSerializer(hunts,many=True)

serialize_hunts_list = []

for hunt in hunts.data:
hunt_prizes = HuntPrize.objects.filter(hunt__id=hunt["id"])
hunt_prizes = BugHuntPrizeSerializer(hunt_prizes,many=True)

serialize_hunts_list.append({
**hunt,
"prizes":hunt_prizes.data
})

return serialize_hunts_list

def get_active_hunts(self,request,*args,**kwargs):
hunts = Hunt.objects.filter(is_published=True,starts_on__lte=datetime.now(),end_on__gte=datetime.now()).order_by("-prize")
return Response(self.serialize_hunts(hunts))

def get_previous_hunts(self,request,*args,**kwargs):
hunts = Hunt.objects.filter(is_published=True,end_on__lte=datetime.now()).order_by("-end_on")
return Response(self.serialize_hunts(hunts))

def get_upcoming_hunts(self,request,*args,**kwargs):
hunts = Hunt.objects.filter(is_published=True,starts_on__gte=datetime.now()).order_by("starts_on")
return Response(self.serialize_hunts(hunts))

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

paginator = PageNumberPagination()

activeHunt = request.query_params.get("activeHunt")
previousHunt = request.query_params.get("previousHunt")
upcomingHunt = request.query_params.get("upcomingHunt")
if activeHunt:
page = paginator.paginate_queryset(
self.get_active_hunts(request,*args,**kwargs),
request
)

return paginator.get_paginated_response(page)

elif previousHunt:
page = paginator.paginate_queryset(
self.get_previous_hunts(request,*args,**kwargs),
request
)

return paginator.get_paginated_response(page)

elif upcomingHunt:
page = paginator.paginate_queryset(
self.get_upcoming_hunts(request,*args,**kwargs),
request
)

return paginator.get_paginated_response(page)

hunts = self.serialize_hunts(Hunt.objects.filter(is_published=True).order_by("-end_on"))
page = paginator.paginate_queryset(hunts,request)

return paginator.get_paginated_response(page)

15 changes: 14 additions & 1 deletion website/serializers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from website.models import Issue, Points, User, UserProfile, Domain
from website.models import Issue, Points, User, UserProfile, Domain, Hunt, HuntPrize
from rest_framework import serializers
from django.db.models import Sum

Expand Down Expand Up @@ -70,3 +70,16 @@ class Meta:
model = Domain
fields = '__all__'



class BugHuntPrizeSerializer(serializers.ModelSerializer):

class Meta:
model = HuntPrize
fields = '__all__'

class BugHuntSerializer(serializers.ModelSerializer):

class Meta:
model = Hunt
fields = '__all__'