From 6b7654a0c32664fb1344d1a9e14121842166ad41 Mon Sep 17 00:00:00 2001 From: virusdefender Date: Mon, 11 Mar 2019 11:25:10 +0800 Subject: [PATCH] update to django 2.0 --- .flake8 | 1 + account/decorators.py | 8 +++--- account/middleware.py | 4 +-- account/models.py | 2 +- account/tests.py | 12 ++++----- account/views/oj.py | 4 +-- announcement/models.py | 2 +- contest/models.py | 12 ++++----- contest/tests.py | 2 +- contest/views/oj.py | 2 +- deploy/requirements.txt | 53 +++++++++++++++++++++++++-------------- oj/settings.py | 2 +- problem/models.py | 4 +-- problem/views/oj.py | 4 +-- submission/models.py | 4 +-- submission/serializers.py | 2 +- submission/views/oj.py | 2 +- utils/api/tests.py | 2 +- 18 files changed, 69 insertions(+), 53 deletions(-) diff --git a/.flake8 b/.flake8 index d64dbd5c..0fc03185 100644 --- a/.flake8 +++ b/.flake8 @@ -4,6 +4,7 @@ exclude = */migrations/, *settings.py */apps.py + venv/ max-line-length = 180 inline-quotes = " no-accept-encodings = True diff --git a/account/decorators.py b/account/decorators.py index e57b3312..08aaa811 100644 --- a/account/decorators.py +++ b/account/decorators.py @@ -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 diff --git a/account/middleware.py b/account/middleware.py index 8834b153..91eed27e 100644 --- a/account/middleware.py +++ b/account/middleware.py @@ -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"}) diff --git a/account/models.py b/account/models.py index eac1a62d..afd644ed 100644 --- a/account/models.py +++ b/account/models.py @@ -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" diff --git a/account/tests.py b/account/tests.py index 65e765da..cc9dc865 100644 --- a/account/tests.py +++ b/account/tests.py @@ -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 diff --git a/account/views/oj.py b/account/views/oj.py index d7e2a646..095f1e40 100644 --- a/account/views/oj.py +++ b/account/views/oj.py @@ -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) diff --git a/announcement/models.py b/announcement/models.py index 37b8d597..441c4c18 100644 --- a/announcement/models.py +++ b/announcement/models.py @@ -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) diff --git a/contest/models.py b/contest/models.py index 77dcf8e2..5d6be507 100644 --- a/contest/models.py +++ b/contest/models.py @@ -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) diff --git a/contest/tests.py b/contest/tests.py index 8a21977d..4c160049 100644 --- a/contest/tests.py +++ b/contest/tests.py @@ -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): diff --git a/contest/views/oj.py b/contest/views/oj.py index 985507fa..6626badb 100644 --- a/contest/views/oj.py +++ b/contest/views/oj.py @@ -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: diff --git a/deploy/requirements.txt b/deploy/requirements.txt index 75827ffd..4feaa4d5 100644 --- a/deploy/requirements.txt +++ b/deploy/requirements.txt @@ -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 diff --git a/oj/settings.py b/oj/settings.py index 6ba39871..2cfa9c8d 100644 --- a/oj/settings.py +++ b/oj/settings.py @@ -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', diff --git a/problem/models.py b/problem/models.py index 29e317bd..7d5b989e 100644 --- a/problem/models.py +++ b/problem/models.py @@ -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 diff --git a/problem/views/oj.py b/problem/views/oj.py index ed39dda9..b091ff31 100644 --- a/problem/views/oj.py +++ b/problem/views/oj.py @@ -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", {}) diff --git a/submission/models.py b/submission/models.py index 261052e9..181c9098 100644 --- a/submission/models.py +++ b/submission/models.py @@ -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() diff --git a/submission/serializers.py b/submission/serializers.py index b814bc7a..5e48f3e7 100644 --- a/submission/serializers.py +++ b/submission/serializers.py @@ -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) diff --git a/submission/views/oj.py b/submission/views/oj.py index 88a794b5..8501efb4 100644 --- a/submission/views/oj.py +++ b/submission/views/oj.py @@ -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()) diff --git a/utils/api/tests.py b/utils/api/tests.py index b47ceae9..0f0a79b8 100644 --- a/utils/api/tests.py +++ b/utils/api/tests.py @@ -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