Exemplo n.º 1
0
def password_change(request):
    form = PasswordChangeForm(request.POST or None, instance=request.user)
    if form.is_valid():
        form.save()
        messages.success(request, PASSWORD_CHANGE_MESSAGE)
        return redirect(reverse('accounts:password_change'))
    context = {'form': form}
    return render(request, 'accounts/password_change.html', context)
Exemplo n.º 2
0
def PasswordChangeView(request, slug):
    user = get_object_or_404(UserModel, slug=slug)
    if request.method == 'POST':
        password_form = PasswordChangeForm(data=request.POST)
        if password_form.is_valid():
            current_password = password_form.cleaned_data.get(
                'current_password')
            logged_in = authenticate(request,
                                     username=user.username,
                                     password=current_password)

            if logged_in is not None:
                new_password = password_form.cleaned_data.get('new_password')
                try:
                    validate_password(new_password,
                                      user=user,
                                      password_validators=None)
                    user.set_password(new_password)
                    user.save()
                    if user.is_superuser:
                        return HttpResponseRedirect(
                            reverse('accounts:profile',
                                    kwargs={'slug': user.slug}))
                    if user.is_masteruser:
                        return HttpResponseRedirect(
                            reverse('masteruser:profile',
                                    kwargs={'slug': user.slug}))
                    if user.is_sportclub:
                        return HttpResponseRedirect(
                            reverse('sportclub:profile',
                                    kwargs={'slug': user.slug}))
                    if user.is_commonuser:
                        return HttpResponseRedirect(
                            reverse('commonuser:profile',
                                    kwargs={'slug': user.slug}))
                except:
                    error1 = 'کلمه عبور باید بیش از 6 کاراکتر باشد'
                    error2 = 'کلمه عبور باید نمیتواند شامل نام کاربری باشد'
                    error3 = 'کلمه عبور نمیتواند خیلی ساده باشد'
                    return render(
                        request, 'accounts/passwordchange.html', {
                            'form': password_form,
                            'error1': error1,
                            'error2': error2,
                            'error3': error3
                        })
            else:
                error4 = 'رمزعبور وارد شده صحیح نیست'
                return render(request, 'accounts/passwordchange.html',
                              {'error4': error4})
    else:
        password_form = PasswordChangeForm()
        return render(request, 'accounts/passwordchange.html',
                      {'form': password_form})
Exemplo n.º 3
0
def change_password(request):
    form = PasswordChangeForm(request.user, request.POST or None)
    if form.is_valid():
        new_password = form.cleaned_data['new_password']
        User.objects.filter(id=request.user.id).update(password=make_password(new_password))
        messages.info(request, 'Your password has been change sucessfully.')
        return HttpResponseRedirect(reverse('accounts.views.change_password'))

    return render(request,
                  'accounts/change_password.html', {
                      'form': form,
                  }, )
Exemplo n.º 4
0
def password_change(request):
    form = PasswordChangeForm(current_user=request.user)
    if request.method == 'POST':
        form = PasswordChangeForm(request.POST, current_user=request.user)
        if form.is_valid():
            form.save()
            messages.success(request, "Successfully changed your password.")
    messages.warning(request, "<b>Make sure you want to do this!</b>"
                     " No, really. This action will change your password.",
                     extra_tags="hide")
    return {
        'form': form,
    }
Exemplo n.º 5
0
def password_change(request):
	form = PasswordChangeForm(current_user=request.user)
	if request.method == 'POST':
		form = PasswordChangeForm(request.POST, current_user=request.user)
		if form.is_valid():
			form.save()
			messages.success(request, "Successfully changed your password.")
	messages.warning(request, "<b>Make sure you want to do this!</b>"
		" No, really. This action will change your password.",
		extra_tags="hide")
	return {
		'form': form,
	}
Exemplo n.º 6
0
def password_change_view(request):
    if request.method == 'POST':
        form = PasswordChangeForm(data=request.POST, user=request.user)

        if form.is_valid():
            form.save()
            messages.success(request, 'You have changed your password...')
            return redirect('accounts:home')

    else:
        form = PasswordChangeForm(user=request.user)

    context = {'form': form}

    return render(request, 'accounts/password_change.html', context)
Exemplo n.º 7
0
def manage_account(request):
    """
    Updates the user's account information.
    If user is an oauth_user, user cannot update password.
    
    """
    user = request.user
    oauth_user = UserSocialAuth.objects.filter(user=user)
    if oauth_user.exists() and oauth_user.count() >= 1:
        oauth_user = oauth_user[0]
    else:
        oauth_user = None

    try:
        userprofile = UserProfile.objects.get(user=user)
    except UserProfile.DoesNotExist:
        userprofile = UserProfile(user=user)
        userprofile = userprofile.save()
    pass_form = PasswordChangeForm(user=user)
    userprof_form = UserProfileUpdateForm()
    user_form = UsernameChangeForm()
    context = {}
    if request.method == "POST":
        userprof_form = UserProfileUpdateForm(request.POST,
                                              instance=userprofile)
        user_form = UsernameChangeForm(request.POST, instance=user)
        pass_form = PasswordChangeForm(user=user, data=request.POST)
        if 'username_update' in request.POST:
            if user_form.is_valid():
                user_form.save()
                return redirect('account_info')
        elif 'userprofile' in request.POST:
            if userprof_form.is_valid():
                userprof_form.save()
                return redirect('account_info')
        elif 'change_password' in request.POST:
            if pass_form.is_valid():
                pass_form.save()
                return redirect('account_info')
    else:
        userprof_form = UserProfileUpdateForm(instance=userprofile)
        user_form = UsernameChangeForm(instance=user)
        pass_form = PasswordChangeForm(user=user)
    context['oauth_user'] = oauth_user
    context['pass_form'] = pass_form
    context['userprof_form'] = userprof_form
    context['user_form'] = user_form
    return render(request, 'account_manage.html', context)
Exemplo n.º 8
0
def manage_account(request):
    """
    Updates the user's account information.
    If user is an oauth_user, user cannot update password.
    
    """
    user = request.user
    oauth_user = UserSocialAuth.objects.filter(user=user)
    if oauth_user.exists() and oauth_user.count() >= 1:
        oauth_user = oauth_user[0]
    else:
        oauth_user = None

    try:
        userprofile = UserProfile.objects.get(user=user)
    except UserProfile.DoesNotExist:
        userprofile = UserProfile(user=user)
        userprofile = userprofile.save()
    pass_form = PasswordChangeForm(user=user)
    userprof_form = UserProfileUpdateForm()
    user_form = UsernameChangeForm()
    context = {}
    if request.method == "POST":
        userprof_form = UserProfileUpdateForm(request.POST, instance=userprofile)
        user_form = UsernameChangeForm(request.POST, instance=user)
        pass_form = PasswordChangeForm(user=user, data=request.POST)
        if 'username_update' in request.POST:
            if user_form.is_valid():
                user_form.save()
                return redirect('account_info')
        elif 'userprofile' in request.POST:
            if userprof_form.is_valid():
                userprof_form.save()
                return redirect('account_info')
        elif 'change_password' in request.POST:
            if pass_form.is_valid():
                pass_form.save()
                return redirect('account_info')
    else:
        userprof_form = UserProfileUpdateForm(instance=userprofile)
        user_form = UsernameChangeForm(instance=user)
        pass_form = PasswordChangeForm(user=user)
    context['oauth_user'] = oauth_user
    context['pass_form'] = pass_form
    context['userprof_form'] = userprof_form
    context['user_form'] = user_form
    return render(request, 'account_manage.html', context)
Exemplo n.º 9
0
def change_password(request):
    if request.method == 'POST':
        form = PasswordChangeForm(data=request.POST, user=request.user)

        if form.is_valid():
            form.save()
            update_session_auth_hash(request, form.user)
            messages.success(request, "Password has changed successfully!")
            return redirect(reverse('profile'))
        else:
            messages.error(
                request, "There was something wrong with the given passwords!")
            return redirect('change_password')
    else:
        form = PasswordChangeForm(user=request.user)
        args = {'form': form}
        return render(request, 'change-password.html', args)
Exemplo n.º 10
0
def password_change(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect(reverse('index'))

    if request.method == "POST":
        form = PasswordChangeForm(request.user, request.POST, label_suffix='')
        if form.is_valid():
            form.save()
            snapshot_user_details(request.user, password='******')
            return HttpResponseRedirect(reverse('password-changed'))
    else:
        form = PasswordChangeForm(request.user, label_suffix='')

    menu = {'parent': 'account'}
    return render(request, 'password_change.html', {
                  'menu': menu,
                  'form': form,
                  })
Exemplo n.º 11
0
    def post(self, request, *args, **kwargs):
        form = PasswordChangeForm(request.POST, instance=request.user)

        if form.is_valid():
            request.user.set_password(form.cleaned_data['password'])
            request.user.save()

            messages.success(request, 'Password updated!', extra_tags='success')
            return HttpResponseRedirect(reverse('profile_settings_password_view'))
        else:
            context = {
                'page_title': 'Update Password',
                'location': 'profile_settings',
                'sublocation': 'password',
                'form': form
            }

            return render(request, 'neo/profile/settings/password.html', context)
Exemplo n.º 12
0
def password_change(request):
    user = get_object_or_404(User, pk = request.user.pk)
    if request.method == "POST":
        form = PasswordChangeForm(request.POST, user=user)
        if form.is_valid():
            user.set_password(form.cleaned_data['password'])
            user.save()
            messages.success(request, u"Пароль изменен")
            return redirect('accounts_my_profile')
        else:
            messages.error(request, u"Пароль не изменен")
    else:
        form = PasswordChangeForm(user=user)
    
    return direct_to_template(
        request,
        template = "accounts/password_change_form.html",
        extra_context = {'form': form}
    )
Exemplo n.º 13
0
def password_change(request, user_id):
    if not request.user.is_authenticated:
        return JsonResponse({'code': 0, "message": "error"})
    curr_user = get_object_or_404(BlogUser, id=user_id)
    if request.method == 'POST':
        form = PasswordChangeForm(request.POST)
        if form.is_valid():
            password = form.cleaned_data.get('old_username')
            username = curr_user.username
            user = auth.authenticate(username=username, password=password)
            if user is not None:
                new_password = form.cleaned_data.get('password2')
                user.set_password(new_password)
                user.save()
                return JsonResponse({'code': 1, 'message': 'Password Change Success'})
            else:
                return render(request, 'accounts/password_change.html', {'form': form})
    else:
        form = PasswordChangeForm()
        return render(request, 'accounts/password_change.html', {'form': form})
Exemplo n.º 14
0
def password(request):
    """
    Password change / set form.
    """

    do_change = False

    if request.user.has_usable_password():
        if request.method == "POST":
            change_form = PasswordChangeForm(request.POST)
            if change_form.is_valid():
                cur_password = change_form.cleaned_data["password"]
                if request.user.check_password(cur_password):
                    do_change = True
                else:
                    messages.error(request, _("You have entered an invalid password."))
        else:
            change_form = PasswordChangeForm()
    else:
        do_change = True
        change_form = None

    if request.method == "POST":
        form = PasswordForm(request.POST)
        if form.is_valid() and do_change:

            # Clear flag forcing user to set password
            if "show_set_password" in request.session:
                del request.session["show_set_password"]

            request.user.set_password(form.cleaned_data["password1"])
            request.user.save()
            messages.info(request, _("Your password has been changed."))
            return redirect("profile")
    else:
        form = PasswordForm()

    return render_to_response(
        "accounts/password.html",
        RequestContext(request, {"title": _("Change password"), "change_form": change_form, "form": form}),
    )
Exemplo n.º 15
0
def PasswordChangeView(request,slug):
    user = get_object_or_404(UserModel,slug = slug)
    if request.method == 'POST':
        password_form = PasswordChangeForm(data = request.POST)
        if password_form.is_valid():
            current_password = password_form.cleaned_data.get('current_password')
            logged_in = authenticate(request, username=user.username, password=current_password)

            if logged_in is not None:
                if password_form.cleaned_data.get('new_password') == password_form.cleaned_data.get('confirm_password'):
                    new_password = password_form.cleaned_data.get('new_password')
                    try:
                        validate_password(new_password,user=user, password_validators=None)
                        user.set_password(new_password)
                        user.save()
                        return HttpResponseRedirect(reverse('login'))
                    except:
                        error1 ='کلمه عبور باید بیش از 6 کاراکتر باشد'
                        error2 ='کلمه عبور باید نمیتواند شامل نام کاربری باشد'
                        error3 ='کلمه عبور نمیتواند خیلی ساده باشد'
                        return render(request,'accounts/passwordchange.html',{'form':password_form,'error1':error1,'error2':error2,'error3':error3})
                else:
                    error4 = 'رمز های وارد شده با هم مطابقت ندارند'
                    password_form = PasswordChangeForm()
                    return  render(request,'accounts/passwordchange.html',
                                          {'error4':error4,'form':password_form})

            else:
                error4 = 'رمزعبور وارد شده صحیح نیست'
                password_form = PasswordChangeForm()
                return  render(request,'accounts/passwordchange.html',
                                      {'error4':error4,'form':password_form})
    else:
        password_form = PasswordChangeForm()
        return  render(request,'accounts/passwordchange.html',
                              {'form':password_form})
Exemplo n.º 16
0
 def test_form_error_not_required_fields(self):
     form = PasswordChangeForm(data={})
     self.assertFalse(form.is_valid())
Exemplo n.º 17
0
 def test_all_fields_are_required(self):
     form = PasswordChangeForm(self.user, data={'wrong': 'data'})
     form.is_valid()
     self.assertTrue(
         all([form.has_error(f, 'required') for f in form.fields]))
Exemplo n.º 18
0
 def test_password_change_form(self):
     user = User.objects.create_user('John', '*****@*****.**', 'johnpassword')
     user.save()
     data = {'old_password': "******", 'new_password1': "fdsgsdfgfdg", 'new_password2': "fdsgsdfgfdg"}
     form = PasswordChangeForm(user=user, data=data)
     self.assertTrue(form.is_valid())