update to django 2.0

This commit is contained in:
virusdefender 2019-03-11 11:25:10 +08:00
parent f7bd9f16b4
commit 6b7654a0c3
18 changed files with 69 additions and 53 deletions

View File

@ -4,6 +4,7 @@ exclude =
*/migrations/,
*settings.py
*/apps.py
venv/
max-line-length = 180
inline-quotes = "
no-accept-encodings = True

View File

@ -31,19 +31,19 @@ class BasePermissionDecorator(object):
class login_required(BasePermissionDecorator):
def check_permission(self):
return self.request.user.is_authenticated()
return self.request.user.is_authenticated
class super_admin_required(BasePermissionDecorator):
def check_permission(self):
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):
def check_permission(self):
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):
@ -80,7 +80,7 @@ def check_contest_permission(check_type="details"):
return self.error("Contest %s doesn't exist" % contest_id)
# Anonymous
if not user.is_authenticated():
if not user.is_authenticated:
return self.error("Please login first.")
# creator or owner

View File

@ -22,7 +22,7 @@ class APITokenAuthMiddleware(MiddlewareMixin):
class SessionRecordMiddleware(MiddlewareMixin):
def process_request(self, request):
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["user_agent"] = request.META.get("HTTP_USER_AGENT", "")
session["ip"] = request.ip
@ -37,7 +37,7 @@ class AdminRoleRequiredMiddleware(MiddlewareMixin):
def process_request(self, request):
path = request.path_info
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"})

View File

@ -60,7 +60,7 @@ class User(AbstractBaseUser):
return self.problem_permission == ProblemPermission.ALL
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:
db_table = "user"

View File

@ -101,13 +101,13 @@ class UserLoginAPITest(APITestCase):
self.assertDictEqual(response.data, {"error": None, "data": "Succeeded"})
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):
resp = self.client.post(self.login_url, data={"username": self.username.upper(), "password": self.password})
self.assertDictEqual(resp.data, {"error": None, "data": "Succeeded"})
user = auth.get_user(self.client)
self.assertTrue(user.is_authenticated())
self.assertTrue(user.is_authenticated)
def test_login_with_wrong_info(self):
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"})
user = auth.get_user(self.client)
self.assertFalse(user.is_authenticated())
self.assertFalse(user.is_authenticated)
def test_tfa_login(self):
token = self._set_tfa()
@ -129,7 +129,7 @@ class UserLoginAPITest(APITestCase):
self.assertDictEqual(response.data, {"error": None, "data": "Succeeded"})
user = auth.get_user(self.client)
self.assertTrue(user.is_authenticated())
self.assertTrue(user.is_authenticated)
def test_tfa_login_wrong_code(self):
self._set_tfa()
@ -140,7 +140,7 @@ class UserLoginAPITest(APITestCase):
self.assertDictEqual(response.data, {"error": "error", "data": "Invalid two factor verification code"})
user = auth.get_user(self.client)
self.assertFalse(user.is_authenticated())
self.assertFalse(user.is_authenticated)
def test_tfa_login_without_code(self):
self._set_tfa()
@ -150,7 +150,7 @@ class UserLoginAPITest(APITestCase):
self.assertDictEqual(response.data, {"error": "error", "data": "tfa_required"})
user = auth.get_user(self.client)
self.assertFalse(user.is_authenticated())
self.assertFalse(user.is_authenticated)
def test_user_disabled(self):
self.user.is_disabled = True

View File

@ -35,7 +35,7 @@ class UserProfileAPI(APIView):
判断是否登录 若登录返回用户信息
"""
user = request.user
if not user.is_authenticated():
if not user.is_authenticated:
return self.success()
show_real_name = False
username = request.GET.get("username")
@ -280,7 +280,7 @@ class UserChangePasswordAPI(APIView):
class ApplyResetPasswordAPI(APIView):
@validate_serializer(ApplyResetPasswordSerializer)
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? ")
data = request.data
captcha = Captcha(request)

View File

@ -9,7 +9,7 @@ class Announcement(models.Model):
# HTML
content = RichTextField()
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)
visible = models.BooleanField(default=True)

View File

@ -20,7 +20,7 @@ class Contest(models.Model):
end_time = models.DateTimeField()
create_time = models.DateTimeField(auto_now_add=True)
last_update_time = models.DateTimeField(auto_now=True)
created_by = models.ForeignKey(User)
created_by = models.ForeignKey(User, on_delete=models.CASCADE)
# 是否可见 false的话相当于删除
visible = models.BooleanField(default=True)
allowed_ip_ranges = JSONField(default=list)
@ -47,7 +47,7 @@ class Contest(models.Model):
def problem_details_permission(self, user):
return self.rule_type == ContestRuleType.ACM 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
class Meta:
@ -56,8 +56,8 @@ class Contest(models.Model):
class AbstractContestRank(models.Model):
user = models.ForeignKey(User)
contest = models.ForeignKey(Contest)
user = models.ForeignKey(User, on_delete=models.CASCADE)
contest = models.ForeignKey(Contest, on_delete=models.CASCADE)
submission_number = models.IntegerField(default=0)
class Meta:
@ -87,10 +87,10 @@ class OIContestRank(AbstractContestRank):
class ContestAnnouncement(models.Model):
contest = models.ForeignKey(Contest)
contest = models.ForeignKey(Contest, on_delete=models.CASCADE)
title = models.TextField()
content = RichTextField()
created_by = models.ForeignKey(User)
created_by = models.ForeignKey(User, on_delete=models.CASCADE)
visible = models.BooleanField(default=True)
create_time = models.DateTimeField(auto_now_add=True)

View File

@ -45,7 +45,7 @@ class ContestAdminAPITest(APITestCase):
response_data = response.data["data"]
for k in data.keys():
if isinstance(data[k], datetime):
continue
continue
self.assertEqual(response_data[k], data[k])
def test_get_contests(self):

View File

@ -121,7 +121,7 @@ class ContestRankAPI(APIView):
def get(self, request):
download_csv = request.GET.get("download_csv")
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:
serializer = OIContestRankSerializer
else:

View File

@ -1,19 +1,34 @@
django==1.11.4
djangorestframework==3.4.0
pillow
otpauth
flake8-quotes
pytz
coverage
python-dateutil
celery
Envelopes
qrcode
flake8-coding
requests
django-redis
psycopg2-binary
gunicorn
jsonfield
XlsxWriter
raven
amqp==2.4.2
billiard==3.5.0.5
celery==4.2.1
certifi==2019.3.9
chardet==3.0.4
coverage==4.5.3
Django==2.1.7
django-redis==4.10.0
djangorestframework==3.8.2
entrypoints==0.3
Envelopes==0.4
flake8==3.7.7
flake8-coding==1.3.1
flake8-quotes==1.0.0
gunicorn==19.9.0
idna==2.8
jsonfield==2.0.2
kombu==4.4.0
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

View File

@ -49,7 +49,7 @@ LOCAL_APPS = (
INSTALLED_APPS = VENDOR_APPS + LOCAL_APPS
MIDDLEWARE_CLASSES = (
MIDDLEWARE = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',

View File

@ -28,7 +28,7 @@ class ProblemDifficulty(object):
class Problem(models.Model):
# display ID
_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
is_public = models.BooleanField(default=False)
title = models.TextField()
@ -47,7 +47,7 @@ class Problem(models.Model):
create_time = models.DateTimeField(auto_now_add=True)
# we can not use auto_now here
last_update_time = models.DateTimeField(null=True)
created_by = models.ForeignKey(User)
created_by = models.ForeignKey(User, on_delete=models.CASCADE)
# ms
time_limit = models.IntegerField()
# MB

View File

@ -25,7 +25,7 @@ class PickOneAPI(APIView):
class ProblemAPI(APIView):
@staticmethod
def _add_problem_status(request, queryset_values):
if request.user.is_authenticated():
if request.user.is_authenticated:
profile = request.user.userprofile
acm_problems_status = profile.acm_problems_status.get("problems", {})
oi_problems_status = profile.oi_problems_status.get("problems", {})
@ -81,7 +81,7 @@ class ProblemAPI(APIView):
class ContestProblemAPI(APIView):
def _add_problem_status(self, request, queryset_values):
if request.user.is_authenticated():
if request.user.is_authenticated:
profile = request.user.userprofile
if self.contest.rule_type == ContestRuleType.ACM:
problems_status = profile.acm_problems_status.get("contest_problems", {})

View File

@ -22,8 +22,8 @@ class JudgeStatus:
class Submission(models.Model):
id = models.TextField(default=rand_str, primary_key=True, db_index=True)
contest = models.ForeignKey(Contest, null=True)
problem = models.ForeignKey(Problem)
contest = models.ForeignKey(Contest, null=True, on_delete=models.CASCADE)
problem = models.ForeignKey(Problem, on_delete=models.CASCADE)
create_time = models.DateTimeField(auto_now_add=True)
user_id = models.IntegerField(db_index=True)
username = models.TextField()

View File

@ -46,6 +46,6 @@ class SubmissionListSerializer(serializers.ModelSerializer):
def get_show_link(self, obj):
# 没传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 obj.check_user_permission(self.user)

View File

@ -198,6 +198,6 @@ class SubmissionExistsAPI(APIView):
def get(self, request):
if not request.GET.get("problem_id"):
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"],
user_id=request.user.id).exists())

View File

@ -1,4 +1,4 @@
from django.core.urlresolvers import reverse
from django.urls import reverse
from django.test.testcases import TestCase
from rest_framework.test import APIClient