增加修改密码的验证码
This commit is contained in:
@@ -27,6 +27,7 @@ class UserRegisterSerializer(serializers.Serializer):
|
|||||||
class UserChangePasswordSerializer(serializers.Serializer):
|
class UserChangePasswordSerializer(serializers.Serializer):
|
||||||
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)
|
||||||
|
captcha = serializers.CharField(max_length=4, min_length=4)
|
||||||
|
|
||||||
|
|
||||||
class UserSerializer(serializers.ModelSerializer):
|
class UserSerializer(serializers.ModelSerializer):
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ from django.db.models import Q
|
|||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
|
|
||||||
from utils.shortcuts import serializer_invalid_response, error_response, success_response, paginate
|
from utils.shortcuts import serializer_invalid_response, error_response, success_response, paginate
|
||||||
|
from utils.captcha import Captcha
|
||||||
|
|
||||||
from .decorators import login_required
|
from .decorators import login_required
|
||||||
from .models import User
|
from .models import User
|
||||||
@@ -79,6 +80,9 @@ 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
|
||||||
|
captcha = Captcha(request)
|
||||||
|
if not captcha.check(data["captcha"]):
|
||||||
|
return error_response(u"验证码错误")
|
||||||
username = request.user.username
|
username = request.user.username
|
||||||
user = auth.authenticate(username=username, password=data["old_password"])
|
user = auth.authenticate(username=username, password=data["old_password"])
|
||||||
if user:
|
if user:
|
||||||
|
|||||||
@@ -112,4 +112,5 @@ urlpatterns = [
|
|||||||
url(r'^help/$', TemplateView.as_view(template_name="utils/help.html"), name="help_page"),
|
url(r'^help/$', TemplateView.as_view(template_name="utils/help.html"), name="help_page"),
|
||||||
|
|
||||||
url(r'^api/submission/share/$', SubmissionShareAPIView.as_view(), name="submission_share_api"),
|
url(r'^api/submission/share/$', SubmissionShareAPIView.as_view(), name="submission_share_api"),
|
||||||
|
url(r'^captcha/$', "utils.captcha.views.show_captcha", name="show_captcha"),
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -1,13 +1,22 @@
|
|||||||
require(["jquery", "bsAlert", "csrfToken", "validator"], function ($, bsAlert, csrfTokenHeader) {
|
require(["jquery", "bsAlert", "csrfToken", "validator"], function ($, bsAlert, csrfTokenHeader) {
|
||||||
|
|
||||||
|
function refresh_captcha(){
|
||||||
|
this.src = "/captcha/?" + Math.random();
|
||||||
|
$("#captcha")[0].value = "";
|
||||||
|
}
|
||||||
|
$("#captcha-img").click(function(){
|
||||||
|
refresh_captcha();
|
||||||
|
});
|
||||||
|
|
||||||
$('form').validator().on('submit', function (e) {
|
$('form').validator().on('submit', function (e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
var newPassword = $("#new_password ").val();
|
var newPassword = $("#new_password ").val();
|
||||||
var password = $("#password").val();
|
var password = $("#password").val();
|
||||||
|
var captcha = $("#captcha").val();
|
||||||
$.ajax({
|
$.ajax({
|
||||||
beforeSend: csrfTokenHeader,
|
beforeSend: csrfTokenHeader,
|
||||||
url: "/api/change_password/",
|
url: "/api/change_password/",
|
||||||
data: {new_password: newPassword, old_password: password},
|
data: {new_password: newPassword, old_password: password, captcha: captcha},
|
||||||
dataType: "json",
|
dataType: "json",
|
||||||
method: "post",
|
method: "post",
|
||||||
success: function (data) {
|
success: function (data) {
|
||||||
@@ -15,6 +24,7 @@ require(["jquery", "bsAlert", "csrfToken", "validator"], function ($, bsAlert, c
|
|||||||
window.location.href = "/login/";
|
window.location.href = "/login/";
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
refresh_captcha();
|
||||||
bsAlert(data.data);
|
bsAlert(data.data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,13 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="confirm_password">确认密码</label>
|
<label for="confirm_password">确认密码</label>
|
||||||
<input type="password" class="form-control input-lg" id="confirm_password" name="confirm_password" placeholder="确认密码" maxlength="30" data-match="#new_password" data-match-error="两个密码不一致" require>
|
<input type="password" class="form-control input-lg" id="confirm_password" name="confirm_password" placeholder="确认密码" maxlength="30" data-match="#new_password" data-match-error="两个密码不一致" required>
|
||||||
|
<div class="help-block with-errors"></div>
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="confirm_password">验证码</label>
|
||||||
|
<img src="/captcha/" id="captcha-img">
|
||||||
|
<input type="text" class="form-control input-lg" id="captcha" name="captcha" placeholder="验证码" maxlength="4" required>
|
||||||
<div class="help-block with-errors"></div>
|
<div class="help-block with-errors"></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
|
|||||||
8
utils/captcha/views.py
Normal file
8
utils/captcha/views.py
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
# coding=utf-8
|
||||||
|
from django.http import HttpResponse
|
||||||
|
|
||||||
|
from utils.captcha import Captcha
|
||||||
|
|
||||||
|
|
||||||
|
def show_captcha(request):
|
||||||
|
return HttpResponse(Captcha(request).display(), content_type="image/gif")
|
||||||
Reference in New Issue
Block a user