diff --git a/blt/urls.py b/blt/urls.py index ee248a79eb..c439378109 100644 --- a/blt/urls.py +++ b/blt/urls.py @@ -62,7 +62,8 @@ FlagIssueApiView, LeaderboardApiViewSet, StatsApiViewset, - UrlCheckApiViewset + UrlCheckApiViewset, + BugHuntApiViewset, ) from blt import settings @@ -356,6 +357,9 @@ re_path(r"^api/v1/", include(router.urls)), re_path(r"^api/v1/stats/$", StatsApiViewset.as_view(), name="get_score"), re_path(r"^api/v1/urlcheck/$", UrlCheckApiViewset.as_view(), name="url_check"), + re_path( + r"^api/v1/hunt/$",BugHuntApiViewset.as_view(),name="hunt_details" + ), 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"), diff --git a/website/api/views.py b/website/api/views.py index a0186cffef..f1f5907e5c 100644 --- a/website/api/views.py +++ b/website/api/views.py @@ -419,3 +419,32 @@ def post(self, request, *args, **kwargs): else: return Response({"found": False}) + +class BugHuntApiViewset(APIView): + + 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") + return Response(hunts) + + def get_previous_hunts(self,request,*args,**kwargs): + hunts = Hunt.objects.values('id','name','url','prize','logo',"starts_on","end_on").filter(is_published=True,end_on__lte=datetime.now()).order_by("-end_on") + return Response(hunts) + + def get_upcoming_hunts(self,request,*args,**kwargs): + hunts = Hunt.objects.values('id','name','url','prize','logo',"starts_on","end_on").filter(is_published=True,starts_on__gte=datetime.now()).order_by("starts_on") + return Response(hunts) + + def get(self,request,*args,**kwargs): + activeHunt = request.query_params.get("activeHunt") + previousHunt = request.query_params.get("previousHunt") + upcomingHunt = request.query_params.get("upcomingHunt") + if activeHunt: + return self.get_active_hunts(request,*args,**kwargs) + elif previousHunt: + return self.get_previous_hunts(request,*args,**kwargs) + elif upcomingHunt: + return self.get_upcoming_hunts(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) + diff --git a/website/test_api.py b/website/test_api.py index 67a84a53fd..5cd7874e38 100644 --- a/website/test_api.py +++ b/website/test_api.py @@ -6,6 +6,7 @@ from django.core import mail from django.utils.encoding import force_str from django.db.transaction import atomic +import datetime class APITests(APITestCase): @@ -106,3 +107,20 @@ def test_password_reset(self): self.client.post(url, data=data, status_code=200) for item in mail.outbox: print(item.__dict__) + + def test_get_bug_hunt(self): + url = "/api/v1/hunt/?" + response = self.client.get(''.join([url,"activeHunt=1/"])) + self.assertEqual(response.status_code, status.HTTP_200_OK) + if len(response.data): + self.assertTrue( + response.data[0]["starts_on"] < datetime.datetime.now() and response.data[0]["end_on"] > datetime.datetime.now(), + "Invalid Response") + response = self.client.get(''.join([url,"previousHunt=1/"])) + self.assertEqual(response.status_code, status.HTTP_200_OK) + if len(response.data): + self.assertLess(response.data[0]["end_on"], datetime.datetime.now(), "Invalid Response") + response = self.client.get(''.join([url,"upcomingHunt=1/"])) + self.assertEqual(response.status_code, status.HTTP_200_OK) + if len(response.data): + self.assertGreater(response.data[0]["starts_on"], datetime.datetime.now(), "Invalid Response")