添加contest access api
This commit is contained in:
@@ -84,6 +84,31 @@ class ContestAPITest(APITestCase):
|
||||
resp = self.client.post(url, {"contest_id": contest_id, "password": DEFAULT_CONTEST_DATA["password"]})
|
||||
self.assertSuccess(resp)
|
||||
|
||||
def test_contest_access(self):
|
||||
contest_id = self.create_contest().data["data"]["id"]
|
||||
self.create_user("test", "test123")
|
||||
url = self.reverse("contest_access_api")
|
||||
resp = self.client.get(url + "?contest_id=" + str(contest_id))
|
||||
self.assertFalse(resp.data["data"]["Access"])
|
||||
|
||||
password_url = self.reverse("contest_password_api")
|
||||
resp = self.client.post(password_url, {"contest_id": contest_id, "password": DEFAULT_CONTEST_DATA["password"]})
|
||||
self.assertSuccess(resp)
|
||||
resp = self.client.get(url + "?contest_id=" + str(contest_id))
|
||||
self.assertSuccess(resp)
|
||||
|
||||
# def test_get_not_started_contest(self):
|
||||
# contest_id = self.create_contest().data["data"]["id"]
|
||||
# resp = self.client.get(self.url + "?id=" + str(contest_id))
|
||||
# self.assertSuccess(resp)
|
||||
#
|
||||
# self.create_user("test", "1234")
|
||||
# url = self.reverse("contest_password_api")
|
||||
# resp = self.client.post(url, {"contest_id": contest_id, "password": DEFAULT_CONTEST_DATA["password"]})
|
||||
# self.assertSuccess(resp)
|
||||
# resp = self.client.get(self.url + "?id=" + str(contest_id))
|
||||
# self.assertFailed(resp)
|
||||
|
||||
|
||||
class ContestAnnouncementAPITest(APITestCase):
|
||||
def setUp(self):
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
from django.conf.urls import url
|
||||
|
||||
from ..views.oj import ContestAnnouncementListAPI, ContestAPI
|
||||
from ..views.oj import ContestPasswordVerifyAPI
|
||||
from ..views.oj import ContestPasswordVerifyAPI, ContestAccessAPI
|
||||
|
||||
urlpatterns = [
|
||||
url(r"^contest/?$", ContestAPI.as_view(), name="contest_api"),
|
||||
url(r"^contest/password/?$", ContestPasswordVerifyAPI.as_view(), name="contest_password_api"),
|
||||
url(r"^contest/announcement/?$", ContestAnnouncementListAPI.as_view(), name="contest_announcement_api"),
|
||||
url(r"^contest/access/?$", ContestAccessAPI.as_view(), name="contest_access_api"),
|
||||
|
||||
]
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from utils.api import APIView, validate_serializer
|
||||
from account.decorators import login_required
|
||||
|
||||
from ..models import ContestAnnouncement, Contest
|
||||
from ..models import ContestAnnouncement, Contest, ContestStatus
|
||||
from ..serializers import ContestAnnouncementSerializer
|
||||
from ..serializers import ContestSerializer, ContestPasswordVerifySerializer
|
||||
|
||||
@@ -20,13 +20,13 @@ class ContestAnnouncementListAPI(APIView):
|
||||
|
||||
class ContestAPI(APIView):
|
||||
def get(self, request):
|
||||
contest_id = request.GET.get("contest_id")
|
||||
contest_id = request.GET.get("id")
|
||||
if contest_id:
|
||||
try:
|
||||
contest = Contest.objects.get(id=contest_id, visible=True)
|
||||
return self.success(ContestSerializer(contest).data)
|
||||
except Contest.DoesNotExist:
|
||||
return self.error("Contest doesn't exist.")
|
||||
return self.success(ContestSerializer(contest).data)
|
||||
|
||||
contests = Contest.objects.filter(visible=True)
|
||||
keyword = request.GET.get("keyword")
|
||||
@@ -54,3 +54,17 @@ class ContestPasswordVerifyAPI(APIView):
|
||||
# https://docs.djangoproject.com/en/dev/topics/http/sessions/#when-sessions-are-saved
|
||||
request.session.modified = True
|
||||
return self.success(True)
|
||||
|
||||
|
||||
class ContestAccessAPI(APIView):
|
||||
@login_required
|
||||
def get(self, request):
|
||||
contest_id = request.GET.get("contest_id")
|
||||
if not contest_id:
|
||||
return self.error("Parameter contest_id not exist.")
|
||||
if "contests" not in request.session:
|
||||
request.session["contests"] = []
|
||||
if int(contest_id) in request.session["contests"]:
|
||||
return self.success({"Access": True})
|
||||
else:
|
||||
return self.success({"Access": False})
|
||||
|
||||
Reference in New Issue
Block a user