[前台]修改修改密码功能,api添加@login_requrie,去掉界面和api中的username字段,这个从request可以拿到,要用户输入反而不好弄了,也没必要,顺便改了测试[CI不能SKIP]

This commit is contained in:
esp 2015-09-01 19:04:07 +08:00
parent 850815a49f
commit 0e322da9d6
5 changed files with 7 additions and 21 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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()

View File

@ -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) {

View File

@ -5,11 +5,6 @@
<h2 class="text-center">修改密码</h2>
<form id="change_password-form">
<div class="form-group">
<label for="username">用户名</label>
<input type="text" class="form-control input-lg" id="username" name="username" placeholder="用户名" data-error="请填写用户名" maxlength="30" autofocus required>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="password">旧密码</label>
<input type="password" class="form-control input-lg" id="password" name="password" placeholder="密码" data-error="请填写旧密码" maxlength="30" required>