[前台]修改修改密码功能,api添加@login_requrie,去掉界面和api中的username字段,这个从request可以拿到,要用户输入反而不好弄了,也没必要,顺便改了测试[CI不能SKIP]
This commit is contained in:
@@ -25,7 +25,6 @@ class UserRegisterSerializer(serializers.Serializer):
|
|||||||
|
|
||||||
|
|
||||||
class UserChangePasswordSerializer(serializers.Serializer):
|
class UserChangePasswordSerializer(serializers.Serializer):
|
||||||
username = serializers.CharField(max_length=30)
|
|
||||||
old_password = serializers.CharField()
|
old_password = serializers.CharField()
|
||||||
new_password = serializers.CharField(max_length=30, min_length=6)
|
new_password = serializers.CharField(max_length=30, min_length=6)
|
||||||
|
|
||||||
|
|||||||
@@ -123,22 +123,13 @@ class UserChangePasswordAPITest(APITestCase):
|
|||||||
user = User.objects.create(username="test")
|
user = User.objects.create(username="test")
|
||||||
user.set_password("aaabbb")
|
user.set_password("aaabbb")
|
||||||
user.save()
|
user.save()
|
||||||
|
self.client.login(username="test",password="aaabbb")
|
||||||
|
|
||||||
def test_error_old_password(self):
|
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)
|
response = self.client.post(self.url, data=data)
|
||||||
self.assertEqual(response.data, {"code": 1, "data": u"密码不正确,请重新修改!"})
|
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):
|
def test_success_change_password(self):
|
||||||
data = {"username": "test", "old_password": "aaabbb", "new_password": "aaaccc"}
|
data = {"username": "test", "old_password": "aaabbb", "new_password": "aaaccc"}
|
||||||
response = self.client.post(self.url, data=data)
|
response = self.client.post(self.url, data=data)
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ class UserLoginAPIView(APIView):
|
|||||||
else:
|
else:
|
||||||
return serializer_invalid_response(serializer)
|
return serializer_invalid_response(serializer)
|
||||||
|
|
||||||
|
@login_required
|
||||||
def logout(request):
|
def logout(request):
|
||||||
auth.logout(request)
|
auth.logout(request)
|
||||||
return http.HttpResponseRedirect("/")
|
return http.HttpResponseRedirect("/")
|
||||||
@@ -69,6 +69,7 @@ class UserRegisterAPIView(APIView):
|
|||||||
|
|
||||||
|
|
||||||
class UserChangePasswordAPIView(APIView):
|
class UserChangePasswordAPIView(APIView):
|
||||||
|
@login_required
|
||||||
def post(self, request):
|
def post(self, request):
|
||||||
"""
|
"""
|
||||||
用户修改密码json api接口
|
用户修改密码json api接口
|
||||||
@@ -78,7 +79,8 @@ class UserChangePasswordAPIView(APIView):
|
|||||||
serializer = UserChangePasswordSerializer(data=request.data)
|
serializer = UserChangePasswordSerializer(data=request.data)
|
||||||
if serializer.is_valid():
|
if serializer.is_valid():
|
||||||
data = serializer.data
|
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:
|
if user:
|
||||||
user.set_password(data["new_password"])
|
user.set_password(data["new_password"])
|
||||||
user.save()
|
user.save()
|
||||||
|
|||||||
@@ -2,13 +2,12 @@ require(["jquery", "bsAlert", "csrfToken", "validator"], function ($, bsAlert, c
|
|||||||
|
|
||||||
$('form').validator().on('submit', function (e) {
|
$('form').validator().on('submit', function (e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
var username = $("#username").val();
|
|
||||||
var newPassword = $("#new_password ").val();
|
var newPassword = $("#new_password ").val();
|
||||||
var password = $("#password").val();
|
var password = $("#password").val();
|
||||||
$.ajax({
|
$.ajax({
|
||||||
beforeSend: csrfTokenHeader,
|
beforeSend: csrfTokenHeader,
|
||||||
url: "/api/change_password/",
|
url: "/api/change_password/",
|
||||||
data: {username: username, new_password: newPassword, old_password: password},
|
data: {new_password: newPassword, old_password: password},
|
||||||
dataType: "json",
|
dataType: "json",
|
||||||
method: "post",
|
method: "post",
|
||||||
success: function (data) {
|
success: function (data) {
|
||||||
|
|||||||
@@ -5,11 +5,6 @@
|
|||||||
<h2 class="text-center">修改密码</h2>
|
<h2 class="text-center">修改密码</h2>
|
||||||
|
|
||||||
<form id="change_password-form">
|
<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">
|
<div class="form-group">
|
||||||
<label for="password">旧密码</label>
|
<label for="password">旧密码</label>
|
||||||
<input type="password" class="form-control input-lg" id="password" name="password" placeholder="密码" data-error="请填写旧密码" maxlength="30" required>
|
<input type="password" class="form-control input-lg" id="password" name="password" placeholder="密码" data-error="请填写旧密码" maxlength="30" required>
|
||||||
|
|||||||
Reference in New Issue
Block a user