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 .models import ProblemPermission
|
||||||
|
|
||||||
from contest.models import Contest, ContestType
|
from contest.models import Contest, ContestType, ContestStatus
|
||||||
|
|
||||||
|
|
||||||
class BasePermissionDecorator(object):
|
class BasePermissionDecorator(object):
|
||||||
@ -80,12 +80,15 @@ def check_contest_permission(func):
|
|||||||
except Contest.DoesNotExist:
|
except Contest.DoesNotExist:
|
||||||
return self.error("Contest %s doesn't exist" % contest_id)
|
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:
|
if self.contest.contest_type == ContestType.PASSWORD_PROTECTED_CONTEST:
|
||||||
# Anonymous
|
# Anonymous
|
||||||
if not user.is_authenticated():
|
if not user.is_authenticated():
|
||||||
return self.error("Please login in first.")
|
return self.error("Please login in first.")
|
||||||
# creator
|
# creator
|
||||||
if request.user == self.contest.created_by:
|
if user == self.contest.created_by:
|
||||||
return func(*args, **kwargs)
|
return func(*args, **kwargs)
|
||||||
# password error
|
# password error
|
||||||
if ("contests" not in request.session) or (self.contest.id not in request.session["contests"]):
|
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)
|
UserNameAPI, UserProfileAPI)
|
||||||
|
|
||||||
urlpatterns = [
|
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"^profile/?$", UserProfileAPI.as_view(), name="user_profile_api"),
|
||||||
url(r"^avatar/upload/?$", AvatarUploadAPI.as_view(), name="avatar_upload_api"),
|
url(r"^avatar/upload/?$", AvatarUploadAPI.as_view(), name="avatar_upload_api"),
|
||||||
url(r"^sso/?$", SSOAPI.as_view(), name="sso_api"),
|
url(r"^sso/?$", SSOAPI.as_view(), name="sso_api"),
|
||||||
|
|||||||
@ -39,15 +39,19 @@ class UserNameAPI(APIView):
|
|||||||
|
|
||||||
|
|
||||||
class UserProfileAPI(APIView):
|
class UserProfileAPI(APIView):
|
||||||
@login_required
|
"""
|
||||||
|
判断是否登录, 若登录返回用户信息
|
||||||
|
"""
|
||||||
|
@method_decorator(ensure_csrf_cookie)
|
||||||
def get(self, request, **kwargs):
|
def get(self, request, **kwargs):
|
||||||
"""
|
user = request.user
|
||||||
Return user info according username or user_id
|
if not user.is_authenticated():
|
||||||
"""
|
return self.success(0)
|
||||||
|
|
||||||
username = request.GET.get("username")
|
username = request.GET.get("username")
|
||||||
try:
|
try:
|
||||||
if username:
|
if username:
|
||||||
user = User.objects.get(username=username)
|
user = User.objects.get(username=username, is_disabled=False)
|
||||||
else:
|
else:
|
||||||
user = request.user
|
user = request.user
|
||||||
except User.DoesNotExist:
|
except User.DoesNotExist:
|
||||||
|
|||||||
@ -84,6 +84,31 @@ class ContestAPITest(APITestCase):
|
|||||||
resp = self.client.post(url, {"contest_id": contest_id, "password": DEFAULT_CONTEST_DATA["password"]})
|
resp = self.client.post(url, {"contest_id": contest_id, "password": DEFAULT_CONTEST_DATA["password"]})
|
||||||
self.assertSuccess(resp)
|
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):
|
class ContestAnnouncementAPITest(APITestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|||||||
@ -1,11 +1,12 @@
|
|||||||
from django.conf.urls import url
|
from django.conf.urls import url
|
||||||
|
|
||||||
from ..views.oj import ContestAnnouncementListAPI, ContestAPI
|
from ..views.oj import ContestAnnouncementListAPI, ContestAPI
|
||||||
from ..views.oj import ContestPasswordVerifyAPI
|
from ..views.oj import ContestPasswordVerifyAPI, ContestAccessAPI
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r"^contest/?$", ContestAPI.as_view(), name="contest_api"),
|
url(r"^contest/?$", ContestAPI.as_view(), name="contest_api"),
|
||||||
url(r"^contest/password/?$", ContestPasswordVerifyAPI.as_view(), name="contest_password_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/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 utils.api import APIView, validate_serializer
|
||||||
from account.decorators import login_required
|
from account.decorators import login_required
|
||||||
|
|
||||||
from ..models import ContestAnnouncement, Contest
|
from ..models import ContestAnnouncement, Contest, ContestStatus
|
||||||
from ..serializers import ContestAnnouncementSerializer
|
from ..serializers import ContestAnnouncementSerializer
|
||||||
from ..serializers import ContestSerializer, ContestPasswordVerifySerializer
|
from ..serializers import ContestSerializer, ContestPasswordVerifySerializer
|
||||||
|
|
||||||
@ -20,13 +20,13 @@ class ContestAnnouncementListAPI(APIView):
|
|||||||
|
|
||||||
class ContestAPI(APIView):
|
class ContestAPI(APIView):
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
contest_id = request.GET.get("contest_id")
|
contest_id = request.GET.get("id")
|
||||||
if contest_id:
|
if contest_id:
|
||||||
try:
|
try:
|
||||||
contest = Contest.objects.get(id=contest_id, visible=True)
|
contest = Contest.objects.get(id=contest_id, visible=True)
|
||||||
return self.success(ContestSerializer(contest).data)
|
|
||||||
except Contest.DoesNotExist:
|
except Contest.DoesNotExist:
|
||||||
return self.error("Contest doesn't exist.")
|
return self.error("Contest doesn't exist.")
|
||||||
|
return self.success(ContestSerializer(contest).data)
|
||||||
|
|
||||||
contests = Contest.objects.filter(visible=True)
|
contests = Contest.objects.filter(visible=True)
|
||||||
keyword = request.GET.get("keyword")
|
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
|
# https://docs.djangoproject.com/en/dev/topics/http/sessions/#when-sessions-are-saved
|
||||||
request.session.modified = True
|
request.session.modified = True
|
||||||
return self.success(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):
|
class ProblemAPI(APIView):
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
# 问题详情页
|
# 问题详情页
|
||||||
problem_id = request.GET.get("id")
|
problem_id = request.GET.get("problem_id")
|
||||||
if problem_id:
|
if problem_id:
|
||||||
try:
|
try:
|
||||||
problem = Problem.objects.get(_id=problem_id, visible=True)
|
problem = Problem.objects.get(_id=problem_id, visible=True)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user