mirror of
https://github.com/QingdaoU/OnlineJudge.git
synced 2025-01-16 01:13:47 +00:00
update to django 2.0
This commit is contained in:
parent
f7bd9f16b4
commit
6b7654a0c3
1
.flake8
1
.flake8
@ -4,6 +4,7 @@ exclude =
|
|||||||
*/migrations/,
|
*/migrations/,
|
||||||
*settings.py
|
*settings.py
|
||||||
*/apps.py
|
*/apps.py
|
||||||
|
venv/
|
||||||
max-line-length = 180
|
max-line-length = 180
|
||||||
inline-quotes = "
|
inline-quotes = "
|
||||||
no-accept-encodings = True
|
no-accept-encodings = True
|
||||||
|
@ -31,19 +31,19 @@ class BasePermissionDecorator(object):
|
|||||||
|
|
||||||
class login_required(BasePermissionDecorator):
|
class login_required(BasePermissionDecorator):
|
||||||
def check_permission(self):
|
def check_permission(self):
|
||||||
return self.request.user.is_authenticated()
|
return self.request.user.is_authenticated
|
||||||
|
|
||||||
|
|
||||||
class super_admin_required(BasePermissionDecorator):
|
class super_admin_required(BasePermissionDecorator):
|
||||||
def check_permission(self):
|
def check_permission(self):
|
||||||
user = self.request.user
|
user = self.request.user
|
||||||
return user.is_authenticated() and user.is_super_admin()
|
return user.is_authenticated and user.is_super_admin()
|
||||||
|
|
||||||
|
|
||||||
class admin_role_required(BasePermissionDecorator):
|
class admin_role_required(BasePermissionDecorator):
|
||||||
def check_permission(self):
|
def check_permission(self):
|
||||||
user = self.request.user
|
user = self.request.user
|
||||||
return user.is_authenticated() and user.is_admin_role()
|
return user.is_authenticated and user.is_admin_role()
|
||||||
|
|
||||||
|
|
||||||
class problem_permission_required(admin_role_required):
|
class problem_permission_required(admin_role_required):
|
||||||
@ -80,7 +80,7 @@ def check_contest_permission(check_type="details"):
|
|||||||
return self.error("Contest %s doesn't exist" % contest_id)
|
return self.error("Contest %s doesn't exist" % contest_id)
|
||||||
|
|
||||||
# Anonymous
|
# Anonymous
|
||||||
if not user.is_authenticated():
|
if not user.is_authenticated:
|
||||||
return self.error("Please login first.")
|
return self.error("Please login first.")
|
||||||
|
|
||||||
# creator or owner
|
# creator or owner
|
||||||
|
@ -22,7 +22,7 @@ class APITokenAuthMiddleware(MiddlewareMixin):
|
|||||||
class SessionRecordMiddleware(MiddlewareMixin):
|
class SessionRecordMiddleware(MiddlewareMixin):
|
||||||
def process_request(self, request):
|
def process_request(self, request):
|
||||||
request.ip = request.META.get(settings.IP_HEADER, request.META.get("REMOTE_ADDR"))
|
request.ip = request.META.get(settings.IP_HEADER, request.META.get("REMOTE_ADDR"))
|
||||||
if request.user.is_authenticated():
|
if request.user.is_authenticated:
|
||||||
session = request.session
|
session = request.session
|
||||||
session["user_agent"] = request.META.get("HTTP_USER_AGENT", "")
|
session["user_agent"] = request.META.get("HTTP_USER_AGENT", "")
|
||||||
session["ip"] = request.ip
|
session["ip"] = request.ip
|
||||||
@ -37,7 +37,7 @@ class AdminRoleRequiredMiddleware(MiddlewareMixin):
|
|||||||
def process_request(self, request):
|
def process_request(self, request):
|
||||||
path = request.path_info
|
path = request.path_info
|
||||||
if path.startswith("/admin/") or path.startswith("/api/admin/"):
|
if path.startswith("/admin/") or path.startswith("/api/admin/"):
|
||||||
if not (request.user.is_authenticated() and request.user.is_admin_role()):
|
if not (request.user.is_authenticated and request.user.is_admin_role()):
|
||||||
return JSONResponse.response({"error": "login-required", "data": "Please login in first"})
|
return JSONResponse.response({"error": "login-required", "data": "Please login in first"})
|
||||||
|
|
||||||
|
|
||||||
|
@ -60,7 +60,7 @@ class User(AbstractBaseUser):
|
|||||||
return self.problem_permission == ProblemPermission.ALL
|
return self.problem_permission == ProblemPermission.ALL
|
||||||
|
|
||||||
def is_contest_admin(self, contest):
|
def is_contest_admin(self, contest):
|
||||||
return self.is_authenticated() and (contest.created_by == self or self.admin_type == AdminType.SUPER_ADMIN)
|
return self.is_authenticated and (contest.created_by == self or self.admin_type == AdminType.SUPER_ADMIN)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
db_table = "user"
|
db_table = "user"
|
||||||
|
@ -101,13 +101,13 @@ class UserLoginAPITest(APITestCase):
|
|||||||
self.assertDictEqual(response.data, {"error": None, "data": "Succeeded"})
|
self.assertDictEqual(response.data, {"error": None, "data": "Succeeded"})
|
||||||
|
|
||||||
user = auth.get_user(self.client)
|
user = auth.get_user(self.client)
|
||||||
self.assertTrue(user.is_authenticated())
|
self.assertTrue(user.is_authenticated)
|
||||||
|
|
||||||
def test_login_with_correct_info_upper_username(self):
|
def test_login_with_correct_info_upper_username(self):
|
||||||
resp = self.client.post(self.login_url, data={"username": self.username.upper(), "password": self.password})
|
resp = self.client.post(self.login_url, data={"username": self.username.upper(), "password": self.password})
|
||||||
self.assertDictEqual(resp.data, {"error": None, "data": "Succeeded"})
|
self.assertDictEqual(resp.data, {"error": None, "data": "Succeeded"})
|
||||||
user = auth.get_user(self.client)
|
user = auth.get_user(self.client)
|
||||||
self.assertTrue(user.is_authenticated())
|
self.assertTrue(user.is_authenticated)
|
||||||
|
|
||||||
def test_login_with_wrong_info(self):
|
def test_login_with_wrong_info(self):
|
||||||
response = self.client.post(self.login_url,
|
response = self.client.post(self.login_url,
|
||||||
@ -115,7 +115,7 @@ class UserLoginAPITest(APITestCase):
|
|||||||
self.assertDictEqual(response.data, {"error": "error", "data": "Invalid username or password"})
|
self.assertDictEqual(response.data, {"error": "error", "data": "Invalid username or password"})
|
||||||
|
|
||||||
user = auth.get_user(self.client)
|
user = auth.get_user(self.client)
|
||||||
self.assertFalse(user.is_authenticated())
|
self.assertFalse(user.is_authenticated)
|
||||||
|
|
||||||
def test_tfa_login(self):
|
def test_tfa_login(self):
|
||||||
token = self._set_tfa()
|
token = self._set_tfa()
|
||||||
@ -129,7 +129,7 @@ class UserLoginAPITest(APITestCase):
|
|||||||
self.assertDictEqual(response.data, {"error": None, "data": "Succeeded"})
|
self.assertDictEqual(response.data, {"error": None, "data": "Succeeded"})
|
||||||
|
|
||||||
user = auth.get_user(self.client)
|
user = auth.get_user(self.client)
|
||||||
self.assertTrue(user.is_authenticated())
|
self.assertTrue(user.is_authenticated)
|
||||||
|
|
||||||
def test_tfa_login_wrong_code(self):
|
def test_tfa_login_wrong_code(self):
|
||||||
self._set_tfa()
|
self._set_tfa()
|
||||||
@ -140,7 +140,7 @@ class UserLoginAPITest(APITestCase):
|
|||||||
self.assertDictEqual(response.data, {"error": "error", "data": "Invalid two factor verification code"})
|
self.assertDictEqual(response.data, {"error": "error", "data": "Invalid two factor verification code"})
|
||||||
|
|
||||||
user = auth.get_user(self.client)
|
user = auth.get_user(self.client)
|
||||||
self.assertFalse(user.is_authenticated())
|
self.assertFalse(user.is_authenticated)
|
||||||
|
|
||||||
def test_tfa_login_without_code(self):
|
def test_tfa_login_without_code(self):
|
||||||
self._set_tfa()
|
self._set_tfa()
|
||||||
@ -150,7 +150,7 @@ class UserLoginAPITest(APITestCase):
|
|||||||
self.assertDictEqual(response.data, {"error": "error", "data": "tfa_required"})
|
self.assertDictEqual(response.data, {"error": "error", "data": "tfa_required"})
|
||||||
|
|
||||||
user = auth.get_user(self.client)
|
user = auth.get_user(self.client)
|
||||||
self.assertFalse(user.is_authenticated())
|
self.assertFalse(user.is_authenticated)
|
||||||
|
|
||||||
def test_user_disabled(self):
|
def test_user_disabled(self):
|
||||||
self.user.is_disabled = True
|
self.user.is_disabled = True
|
||||||
|
@ -35,7 +35,7 @@ class UserProfileAPI(APIView):
|
|||||||
判断是否登录, 若登录返回用户信息
|
判断是否登录, 若登录返回用户信息
|
||||||
"""
|
"""
|
||||||
user = request.user
|
user = request.user
|
||||||
if not user.is_authenticated():
|
if not user.is_authenticated:
|
||||||
return self.success()
|
return self.success()
|
||||||
show_real_name = False
|
show_real_name = False
|
||||||
username = request.GET.get("username")
|
username = request.GET.get("username")
|
||||||
@ -280,7 +280,7 @@ class UserChangePasswordAPI(APIView):
|
|||||||
class ApplyResetPasswordAPI(APIView):
|
class ApplyResetPasswordAPI(APIView):
|
||||||
@validate_serializer(ApplyResetPasswordSerializer)
|
@validate_serializer(ApplyResetPasswordSerializer)
|
||||||
def post(self, request):
|
def post(self, request):
|
||||||
if request.user.is_authenticated():
|
if request.user.is_authenticated:
|
||||||
return self.error("You have already logged in, are you kidding me? ")
|
return self.error("You have already logged in, are you kidding me? ")
|
||||||
data = request.data
|
data = request.data
|
||||||
captcha = Captcha(request)
|
captcha = Captcha(request)
|
||||||
|
@ -9,7 +9,7 @@ class Announcement(models.Model):
|
|||||||
# HTML
|
# HTML
|
||||||
content = RichTextField()
|
content = RichTextField()
|
||||||
create_time = models.DateTimeField(auto_now_add=True)
|
create_time = models.DateTimeField(auto_now_add=True)
|
||||||
created_by = models.ForeignKey(User)
|
created_by = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||||
last_update_time = models.DateTimeField(auto_now=True)
|
last_update_time = models.DateTimeField(auto_now=True)
|
||||||
visible = models.BooleanField(default=True)
|
visible = models.BooleanField(default=True)
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ class Contest(models.Model):
|
|||||||
end_time = models.DateTimeField()
|
end_time = models.DateTimeField()
|
||||||
create_time = models.DateTimeField(auto_now_add=True)
|
create_time = models.DateTimeField(auto_now_add=True)
|
||||||
last_update_time = models.DateTimeField(auto_now=True)
|
last_update_time = models.DateTimeField(auto_now=True)
|
||||||
created_by = models.ForeignKey(User)
|
created_by = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||||
# 是否可见 false的话相当于删除
|
# 是否可见 false的话相当于删除
|
||||||
visible = models.BooleanField(default=True)
|
visible = models.BooleanField(default=True)
|
||||||
allowed_ip_ranges = JSONField(default=list)
|
allowed_ip_ranges = JSONField(default=list)
|
||||||
@ -47,7 +47,7 @@ class Contest(models.Model):
|
|||||||
def problem_details_permission(self, user):
|
def problem_details_permission(self, user):
|
||||||
return self.rule_type == ContestRuleType.ACM or \
|
return self.rule_type == ContestRuleType.ACM or \
|
||||||
self.status == ContestStatus.CONTEST_ENDED or \
|
self.status == ContestStatus.CONTEST_ENDED or \
|
||||||
user.is_authenticated() and user.is_contest_admin(self) or \
|
user.is_authenticated and user.is_contest_admin(self) or \
|
||||||
self.real_time_rank
|
self.real_time_rank
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
@ -56,8 +56,8 @@ class Contest(models.Model):
|
|||||||
|
|
||||||
|
|
||||||
class AbstractContestRank(models.Model):
|
class AbstractContestRank(models.Model):
|
||||||
user = models.ForeignKey(User)
|
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||||
contest = models.ForeignKey(Contest)
|
contest = models.ForeignKey(Contest, on_delete=models.CASCADE)
|
||||||
submission_number = models.IntegerField(default=0)
|
submission_number = models.IntegerField(default=0)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
@ -87,10 +87,10 @@ class OIContestRank(AbstractContestRank):
|
|||||||
|
|
||||||
|
|
||||||
class ContestAnnouncement(models.Model):
|
class ContestAnnouncement(models.Model):
|
||||||
contest = models.ForeignKey(Contest)
|
contest = models.ForeignKey(Contest, on_delete=models.CASCADE)
|
||||||
title = models.TextField()
|
title = models.TextField()
|
||||||
content = RichTextField()
|
content = RichTextField()
|
||||||
created_by = models.ForeignKey(User)
|
created_by = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||||
visible = models.BooleanField(default=True)
|
visible = models.BooleanField(default=True)
|
||||||
create_time = models.DateTimeField(auto_now_add=True)
|
create_time = models.DateTimeField(auto_now_add=True)
|
||||||
|
|
||||||
|
@ -121,7 +121,7 @@ class ContestRankAPI(APIView):
|
|||||||
def get(self, request):
|
def get(self, request):
|
||||||
download_csv = request.GET.get("download_csv")
|
download_csv = request.GET.get("download_csv")
|
||||||
force_refresh = request.GET.get("force_refresh")
|
force_refresh = request.GET.get("force_refresh")
|
||||||
is_contest_admin = request.user.is_authenticated() and request.user.is_contest_admin(self.contest)
|
is_contest_admin = request.user.is_authenticated and request.user.is_contest_admin(self.contest)
|
||||||
if self.contest.rule_type == ContestRuleType.OI:
|
if self.contest.rule_type == ContestRuleType.OI:
|
||||||
serializer = OIContestRankSerializer
|
serializer = OIContestRankSerializer
|
||||||
else:
|
else:
|
||||||
|
@ -1,19 +1,34 @@
|
|||||||
django==1.11.4
|
amqp==2.4.2
|
||||||
djangorestframework==3.4.0
|
billiard==3.5.0.5
|
||||||
pillow
|
celery==4.2.1
|
||||||
otpauth
|
certifi==2019.3.9
|
||||||
flake8-quotes
|
chardet==3.0.4
|
||||||
pytz
|
coverage==4.5.3
|
||||||
coverage
|
Django==2.1.7
|
||||||
python-dateutil
|
django-redis==4.10.0
|
||||||
celery
|
djangorestframework==3.8.2
|
||||||
Envelopes
|
entrypoints==0.3
|
||||||
qrcode
|
Envelopes==0.4
|
||||||
flake8-coding
|
flake8==3.7.7
|
||||||
requests
|
flake8-coding==1.3.1
|
||||||
django-redis
|
flake8-quotes==1.0.0
|
||||||
psycopg2-binary
|
gunicorn==19.9.0
|
||||||
gunicorn
|
idna==2.8
|
||||||
jsonfield
|
jsonfield==2.0.2
|
||||||
XlsxWriter
|
kombu==4.4.0
|
||||||
raven
|
mccabe==0.6.1
|
||||||
|
otpauth==1.0.1
|
||||||
|
Pillow==5.4.1
|
||||||
|
psycopg2-binary==2.7.7
|
||||||
|
pycodestyle==2.5.0
|
||||||
|
pyflakes==2.1.1
|
||||||
|
python-dateutil==2.8.0
|
||||||
|
pytz==2018.9
|
||||||
|
qrcode==6.1
|
||||||
|
raven==6.10.0
|
||||||
|
redis==3.2.0
|
||||||
|
requests==2.21.0
|
||||||
|
six==1.12.0
|
||||||
|
urllib3==1.24.1
|
||||||
|
vine==1.2.0
|
||||||
|
XlsxWriter==1.1.5
|
||||||
|
@ -49,7 +49,7 @@ LOCAL_APPS = (
|
|||||||
|
|
||||||
INSTALLED_APPS = VENDOR_APPS + LOCAL_APPS
|
INSTALLED_APPS = VENDOR_APPS + LOCAL_APPS
|
||||||
|
|
||||||
MIDDLEWARE_CLASSES = (
|
MIDDLEWARE = (
|
||||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||||
'django.middleware.common.CommonMiddleware',
|
'django.middleware.common.CommonMiddleware',
|
||||||
'django.middleware.csrf.CsrfViewMiddleware',
|
'django.middleware.csrf.CsrfViewMiddleware',
|
||||||
|
@ -28,7 +28,7 @@ class ProblemDifficulty(object):
|
|||||||
class Problem(models.Model):
|
class Problem(models.Model):
|
||||||
# display ID
|
# display ID
|
||||||
_id = models.TextField(db_index=True)
|
_id = models.TextField(db_index=True)
|
||||||
contest = models.ForeignKey(Contest, null=True)
|
contest = models.ForeignKey(Contest, null=True, on_delete=models.CASCADE)
|
||||||
# for contest problem
|
# for contest problem
|
||||||
is_public = models.BooleanField(default=False)
|
is_public = models.BooleanField(default=False)
|
||||||
title = models.TextField()
|
title = models.TextField()
|
||||||
@ -47,7 +47,7 @@ class Problem(models.Model):
|
|||||||
create_time = models.DateTimeField(auto_now_add=True)
|
create_time = models.DateTimeField(auto_now_add=True)
|
||||||
# we can not use auto_now here
|
# we can not use auto_now here
|
||||||
last_update_time = models.DateTimeField(null=True)
|
last_update_time = models.DateTimeField(null=True)
|
||||||
created_by = models.ForeignKey(User)
|
created_by = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||||
# ms
|
# ms
|
||||||
time_limit = models.IntegerField()
|
time_limit = models.IntegerField()
|
||||||
# MB
|
# MB
|
||||||
|
@ -25,7 +25,7 @@ class PickOneAPI(APIView):
|
|||||||
class ProblemAPI(APIView):
|
class ProblemAPI(APIView):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _add_problem_status(request, queryset_values):
|
def _add_problem_status(request, queryset_values):
|
||||||
if request.user.is_authenticated():
|
if request.user.is_authenticated:
|
||||||
profile = request.user.userprofile
|
profile = request.user.userprofile
|
||||||
acm_problems_status = profile.acm_problems_status.get("problems", {})
|
acm_problems_status = profile.acm_problems_status.get("problems", {})
|
||||||
oi_problems_status = profile.oi_problems_status.get("problems", {})
|
oi_problems_status = profile.oi_problems_status.get("problems", {})
|
||||||
@ -81,7 +81,7 @@ class ProblemAPI(APIView):
|
|||||||
|
|
||||||
class ContestProblemAPI(APIView):
|
class ContestProblemAPI(APIView):
|
||||||
def _add_problem_status(self, request, queryset_values):
|
def _add_problem_status(self, request, queryset_values):
|
||||||
if request.user.is_authenticated():
|
if request.user.is_authenticated:
|
||||||
profile = request.user.userprofile
|
profile = request.user.userprofile
|
||||||
if self.contest.rule_type == ContestRuleType.ACM:
|
if self.contest.rule_type == ContestRuleType.ACM:
|
||||||
problems_status = profile.acm_problems_status.get("contest_problems", {})
|
problems_status = profile.acm_problems_status.get("contest_problems", {})
|
||||||
|
@ -22,8 +22,8 @@ class JudgeStatus:
|
|||||||
|
|
||||||
class Submission(models.Model):
|
class Submission(models.Model):
|
||||||
id = models.TextField(default=rand_str, primary_key=True, db_index=True)
|
id = models.TextField(default=rand_str, primary_key=True, db_index=True)
|
||||||
contest = models.ForeignKey(Contest, null=True)
|
contest = models.ForeignKey(Contest, null=True, on_delete=models.CASCADE)
|
||||||
problem = models.ForeignKey(Problem)
|
problem = models.ForeignKey(Problem, on_delete=models.CASCADE)
|
||||||
create_time = models.DateTimeField(auto_now_add=True)
|
create_time = models.DateTimeField(auto_now_add=True)
|
||||||
user_id = models.IntegerField(db_index=True)
|
user_id = models.IntegerField(db_index=True)
|
||||||
username = models.TextField()
|
username = models.TextField()
|
||||||
|
@ -46,6 +46,6 @@ class SubmissionListSerializer(serializers.ModelSerializer):
|
|||||||
|
|
||||||
def get_show_link(self, obj):
|
def get_show_link(self, obj):
|
||||||
# 没传user或为匿名user
|
# 没传user或为匿名user
|
||||||
if self.user is None or not self.user.is_authenticated():
|
if self.user is None or not self.user.is_authenticated:
|
||||||
return False
|
return False
|
||||||
return obj.check_user_permission(self.user)
|
return obj.check_user_permission(self.user)
|
||||||
|
@ -198,6 +198,6 @@ class SubmissionExistsAPI(APIView):
|
|||||||
def get(self, request):
|
def get(self, request):
|
||||||
if not request.GET.get("problem_id"):
|
if not request.GET.get("problem_id"):
|
||||||
return self.error("Parameter error, problem_id is required")
|
return self.error("Parameter error, problem_id is required")
|
||||||
return self.success(request.user.is_authenticated() and
|
return self.success(request.user.is_authenticated and
|
||||||
Submission.objects.filter(problem_id=request.GET["problem_id"],
|
Submission.objects.filter(problem_id=request.GET["problem_id"],
|
||||||
user_id=request.user.id).exists())
|
user_id=request.user.id).exists())
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
from django.core.urlresolvers import reverse
|
from django.urls import reverse
|
||||||
from django.test.testcases import TestCase
|
from django.test.testcases import TestCase
|
||||||
from rest_framework.test import APIClient
|
from rest_framework.test import APIClient
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user