mirror of
https://github.com/QingdaoU/OnlineJudge.git
synced 2025-11-04 14:49:58 +08:00
添加contest access api
This commit is contained in:
parent
8b85f86124
commit
17432b4c81
@ -4,7 +4,7 @@ from utils.api import JSONResponse
|
||||
|
||||
from .models import ProblemPermission
|
||||
|
||||
from contest.models import Contest, ContestType
|
||||
from contest.models import Contest, ContestType, ContestStatus
|
||||
|
||||
|
||||
class BasePermissionDecorator(object):
|
||||
@ -80,12 +80,15 @@ def check_contest_permission(func):
|
||||
except Contest.DoesNotExist:
|
||||
return self.error("Contest %s doesn't exist" % contest_id)
|
||||
|
||||
if self.contest.status == ContestStatus.CONTEST_NOT_START and user != self.contest.created_by:
|
||||
return self.error("Contest has not started yet.")
|
||||
|
||||
if self.contest.contest_type == ContestType.PASSWORD_PROTECTED_CONTEST:
|
||||
# Anonymous
|
||||
if not user.is_authenticated():
|
||||
return self.error("Please login in first.")
|
||||
# creator
|
||||
if request.user == self.contest.created_by:
|
||||
if user == self.contest.created_by:
|
||||
return func(*args, **kwargs)
|
||||
# password error
|
||||
if ("contests" not in request.session) or (self.contest.id not in request.session["contests"]):
|
||||
|
||||
@ -4,7 +4,7 @@ from ..views.user import (SSOAPI, AvatarUploadAPI, TwoFactorAuthAPI,
|
||||
UserNameAPI, UserProfileAPI)
|
||||
|
||||
urlpatterns = [
|
||||
url(r"^username/?$", UserNameAPI.as_view(), name="user_name_api"),
|
||||
# url(r"^username/?$", UserNameAPI.as_view(), name="user_name_api"),
|
||||
url(r"^profile/?$", UserProfileAPI.as_view(), name="user_profile_api"),
|
||||
url(r"^avatar/upload/?$", AvatarUploadAPI.as_view(), name="avatar_upload_api"),
|
||||
url(r"^sso/?$", SSOAPI.as_view(), name="sso_api"),
|
||||
|
||||
@ -39,15 +39,19 @@ class UserNameAPI(APIView):
|
||||
|
||||
|
||||
class UserProfileAPI(APIView):
|
||||
@login_required
|
||||
"""
|
||||
判断是否登录, 若登录返回用户信息
|
||||
"""
|
||||
@method_decorator(ensure_csrf_cookie)
|
||||
def get(self, request, **kwargs):
|
||||
"""
|
||||
Return user info according username or user_id
|
||||
"""
|
||||
user = request.user
|
||||
if not user.is_authenticated():
|
||||
return self.success(0)
|
||||
|
||||
username = request.GET.get("username")
|
||||
try:
|
||||
if username:
|
||||
user = User.objects.get(username=username)
|
||||
user = User.objects.get(username=username, is_disabled=False)
|
||||
else:
|
||||
user = request.user
|
||||
except User.DoesNotExist:
|
||||
|
||||
@ -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})
|
||||
|
||||
@ -14,7 +14,7 @@ class ProblemTagAPI(APIView):
|
||||
class ProblemAPI(APIView):
|
||||
def get(self, request):
|
||||
# 问题详情页
|
||||
problem_id = request.GET.get("id")
|
||||
problem_id = request.GET.get("problem_id")
|
||||
if problem_id:
|
||||
try:
|
||||
problem = Problem.objects.get(_id=problem_id, visible=True)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user