From 0e322da9d6e37e7fa5ed95d6fc1c7128d62b0837 Mon Sep 17 00:00:00 2001 From: esp Date: Tue, 1 Sep 2015 19:04:07 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=89=8D=E5=8F=B0]=E4=BF=AE=E6=94=B9=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E5=AF=86=E7=A0=81=E5=8A=9F=E8=83=BD,api=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0@login=5Frequrie,=E5=8E=BB=E6=8E=89=E7=95=8C=E9=9D=A2?= =?UTF-8?q?=E5=92=8Capi=E4=B8=AD=E7=9A=84username=E5=AD=97=E6=AE=B5,?= =?UTF-8?q?=E8=BF=99=E4=B8=AA=E4=BB=8Erequest=E5=8F=AF=E4=BB=A5=E6=8B=BF?= =?UTF-8?q?=E5=88=B0,=E8=A6=81=E7=94=A8=E6=88=B7=E8=BE=93=E5=85=A5?= =?UTF-8?q?=E5=8F=8D=E8=80=8C=E4=B8=8D=E5=A5=BD=E5=BC=84=E4=BA=86,?= =?UTF-8?q?=E4=B9=9F=E6=B2=A1=E5=BF=85=E8=A6=81,=E9=A1=BA=E4=BE=BF?= =?UTF-8?q?=E6=94=B9=E4=BA=86=E6=B5=8B=E8=AF=95[CI=E4=B8=8D=E8=83=BDSKIP]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- account/serializers.py | 1 - account/tests.py | 13 ++----------- account/views.py | 6 ++++-- static/src/js/app/oj/account/change_password.js | 3 +-- template/src/oj/account/change_password.html | 5 ----- 5 files changed, 7 insertions(+), 21 deletions(-) diff --git a/account/serializers.py b/account/serializers.py index 9a43457..179d28d 100644 --- a/account/serializers.py +++ b/account/serializers.py @@ -25,7 +25,6 @@ class UserRegisterSerializer(serializers.Serializer): class UserChangePasswordSerializer(serializers.Serializer): - username = serializers.CharField(max_length=30) old_password = serializers.CharField() new_password = serializers.CharField(max_length=30, min_length=6) diff --git a/account/tests.py b/account/tests.py index f8cc9d0..bbdf1e9 100644 --- a/account/tests.py +++ b/account/tests.py @@ -123,22 +123,13 @@ class UserChangePasswordAPITest(APITestCase): user = User.objects.create(username="test") user.set_password("aaabbb") user.save() + self.client.login(username="test",password="aaabbb") def test_error_old_password(self): - data = {"username": "test", "old_password": "aaaccc", "new_password": "aaaddd"} + data = {"old_password": "aaaccc", "new_password": "aaaddd"} response = self.client.post(self.url, data=data) self.assertEqual(response.data, {"code": 1, "data": u"密码不正确,请重新修改!"}) - def test_invalid_data_format(self): - data = {"old_password": "aaa", "new_password": "aaaddd"} - response = self.client.post(self.url, data=data) - self.assertEqual(response.data["code"], 1) - - def test_username_does_not_exist(self): - data = {"username": "test1", "old_password": "aaabbb", "new_password": "aaaddd"} - response = self.client.post(self.url, data=data) - self.assertEqual(response.data["code"], 1) - def test_success_change_password(self): data = {"username": "test", "old_password": "aaabbb", "new_password": "aaaccc"} response = self.client.post(self.url, data=data) diff --git a/account/views.py b/account/views.py index 89197ae..1da8967 100644 --- a/account/views.py +++ b/account/views.py @@ -35,7 +35,7 @@ class UserLoginAPIView(APIView): else: return serializer_invalid_response(serializer) - +@login_required def logout(request): auth.logout(request) return http.HttpResponseRedirect("/") @@ -69,6 +69,7 @@ class UserRegisterAPIView(APIView): class UserChangePasswordAPIView(APIView): + @login_required def post(self, request): """ 用户修改密码json api接口 @@ -78,7 +79,8 @@ class UserChangePasswordAPIView(APIView): serializer = UserChangePasswordSerializer(data=request.data) if serializer.is_valid(): data = serializer.data - user = auth.authenticate(username=data["username"], password=data["old_password"]) + username = request.user.username + user = auth.authenticate(username=username, password=data["old_password"]) if user: user.set_password(data["new_password"]) user.save() diff --git a/static/src/js/app/oj/account/change_password.js b/static/src/js/app/oj/account/change_password.js index ba88f5f..608114f 100644 --- a/static/src/js/app/oj/account/change_password.js +++ b/static/src/js/app/oj/account/change_password.js @@ -2,13 +2,12 @@ require(["jquery", "bsAlert", "csrfToken", "validator"], function ($, bsAlert, c $('form').validator().on('submit', function (e) { e.preventDefault(); - var username = $("#username").val(); var newPassword = $("#new_password ").val(); var password = $("#password").val(); $.ajax({ beforeSend: csrfTokenHeader, url: "/api/change_password/", - data: {username: username, new_password: newPassword, old_password: password}, + data: {new_password: newPassword, old_password: password}, dataType: "json", method: "post", success: function (data) { diff --git a/template/src/oj/account/change_password.html b/template/src/oj/account/change_password.html index 039cb73..dad9d14 100644 --- a/template/src/oj/account/change_password.html +++ b/template/src/oj/account/change_password.html @@ -5,11 +5,6 @@

修改密码

-
- - -
-