示例#1
0
def update_password(request):
    token = get_token_data(request)
    username = token['username']

    post_data = json.loads(request.body.decode('utf-8'))
    new_password = post_data['password']
    old_password = post_data['oldPassword']

    try:
        validate_password(new_password)
    except ValidationError as e:
        return JsonResponse({'status': 'fail', 'message': str(e)}, status=500)

    # check old password and get user object
    u = authenticate(username=username, password=old_password)
    if u is not None:
        u.set_password(new_password)
        try:
            u.save()
        except:
            return JsonResponse(
                {
                    'status': 'fail',
                    'message': 'There was an error while updating the password'
                },
                status=500)

        return JsonResponse({'status': 'success'})
    else:
        return JsonResponse({'status': 'fail'}, status=401)
示例#2
0
 def clean_password(self):
     password = self.cleaned_data.get('password', None)
     validate_password(password)
     return password
示例#3
0
def register(request):
    if request.method != 'POST':
        return JsonResponse({
            "status": "fail",
            "message": "必须是POST"
        })
        pass

    try:
        # 先通過json取值,如果數據不是json的就取POST參數中的
        post_data = json.loads(request.body.decode('utf-8'))
    except Exception as e:
        post_data = request.POST
    username = post_data['username']
    try:
        # 先查找用戶名是否存在
        u = UserProfile.objects.get(username=username)
    except UserProfile.DoesNotExist:
        # 用戶名不存在,可以注冊
        nickname = post_data['nickname']
        email = post_data['email']
        password = post_data['password']
        confirm_password = post_data['confirm_password']
        gender = None
        if 'gender' in post_data:
            gender = post_data['gender']
        phone = None
        if 'phone' in post_data:
            phone = post_data['phone']
        birday = None
        if 'birday' in post_data:
            birday = post_data['birday']
        address = None
        if 'address' in post_data:
            address = post_data['address']

        avatar = None
        if 'avatar' in request.FILES:
            avatar = request.FILES.get('avatar', None)
        if password != confirm_password:
            return JsonResponse({
                'status': 'fail',
                'message': '兩次密碼不一致'
            }, status=500)
        try:
            validate_password(password)
            validate_email(email)
        except ValidationError as e:
            return JsonResponse({
                'status': 'fail',
                'message': str(e)
            }, status=500)

        # 注冊用戶
        try:
            u = UserProfile.objects.create_user(
                username=username, nickname=nickname,
                password=password, email=email,
                gender=gender, phone=phone,
                birday=birday, avatar=avatar,
                address=address
            )
            u.save()
        except Exception as e:
            return JsonResponse({
                'status': 'fail',
                'message': '注冊出錯:' + str(e)
            }, status=500)

        # 跳轉向到登錄界面
        # return login(request=request, redirect_after_registration=True, redirect_user=u, registration_data={'username': username, 'email': email})
        return JsonResponse({
            'status': 'success',
            'data': {
                'username': username,
                'email': email
            }
        })

    # 用戶名已存在,無法完成注冊, 並告訴客戶端
    return JsonResponse({
        'status': 'fail',
        'message': 'username exists',
    })
示例#4
0
文件: forms.py 项目: fullctl/aaactl
 def clean_password_new(self):
     password = self.cleaned_data["password_new"]
     validate_password(password)
     return password
示例#5
0
文件: forms.py 项目: fullctl/aaactl
 def clean_password(self):
     password = self.cleaned_data["password"]
     return validate_password(password)
示例#6
0
文件: serializers.py 项目: Bukee/mes
 def validate_password(self, password):
     validate_password(password)
     return password
示例#7
0
def test_validate_password():
    assert validators.validate_password("12345678") == "12345678"
    with pytest.raises(validators.ValidationError):
        validators.validate_password("test")