增加修改密码的验证码

This commit is contained in:
virusdefender 2015-09-10 15:59:48 +08:00
parent fbeb745530
commit 12b6eae6e8
6 changed files with 32 additions and 2 deletions

View File

@ -27,6 +27,7 @@ class UserRegisterSerializer(serializers.Serializer):
class UserChangePasswordSerializer(serializers.Serializer):
old_password = serializers.CharField()
new_password = serializers.CharField(max_length=30, min_length=6)
captcha = serializers.CharField(max_length=4, min_length=4)
class UserSerializer(serializers.ModelSerializer):

View File

@ -7,6 +7,7 @@ from django.db.models import Q
from rest_framework.views import APIView
from utils.shortcuts import serializer_invalid_response, error_response, success_response, paginate
from utils.captcha import Captcha
from .decorators import login_required
from .models import User
@ -79,6 +80,9 @@ class UserChangePasswordAPIView(APIView):
serializer = UserChangePasswordSerializer(data=request.data)
if serializer.is_valid():
data = serializer.data
captcha = Captcha(request)
if not captcha.check(data["captcha"]):
return error_response(u"验证码错误")
username = request.user.username
user = auth.authenticate(username=username, password=data["old_password"])
if user:

View File

@ -112,4 +112,5 @@ urlpatterns = [
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'^captcha/$', "utils.captcha.views.show_captcha", name="show_captcha"),
]

View File

@ -1,13 +1,22 @@
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) {
e.preventDefault();
var newPassword = $("#new_password ").val();
var password = $("#password").val();
var captcha = $("#captcha").val();
$.ajax({
beforeSend: csrfTokenHeader,
url: "/api/change_password/",
data: {new_password: newPassword, old_password: password},
data: {new_password: newPassword, old_password: password, captcha: captcha},
dataType: "json",
method: "post",
success: function (data) {
@ -15,6 +24,7 @@ require(["jquery", "bsAlert", "csrfToken", "validator"], function ($, bsAlert, c
window.location.href = "/login/";
}
else {
refresh_captcha();
bsAlert(data.data);
}
}

View File

@ -17,7 +17,13 @@
</div>
<div class="form-group">
<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>
<div class="form-group">

8
utils/captcha/views.py Normal file
View 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")