Python django.contrib.auth.forms 模块,SetPasswordForm() 实例源码

我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用django.contrib.auth.forms.SetPasswordForm()

项目:mhacks-admin    作者:mhacks    | 项目源码 | 文件源码
def update_password(request, uid, token):
    user = validate_signed_token(uid, token)
    if not user:
        return HttpResponseForbidden()  # Just straight up forbid this request, looking fishy already!
    if request.method == 'POST':
        form = SetPasswordForm(user, data=request.POST)
        if form.is_valid():
            form.save()
            Token.objects.filter(user_id__exact=user.pk).delete()
            return redirect(reverse('mhacks-login') + '?username=' + user.email)
    elif request.method == 'GET':
        form = SetPasswordForm(user)
    else:
        return HttpResponseNotAllowed(permitted_methods=['GET', 'POST'])
    form.fields['new_password2'].label = 'Confirm New Password'
    form.fields['new_password2'].longest = True
    return render(request, 'password_reset.html', {'form': form, 'type': 'reset', 'uid': uid, 'token': token})
项目:simplemooc    作者:paulopinda    | 项目源码 | 文件源码
def password_reset_confirm(request, key):
    template_name = 'accounts/password_reset_confirm.html'
    context = {}

    reset = get_object_or_404(PasswordReset, key=key)
    form = SetPasswordForm(user=reset.user, data=request.POST or None)

    if form.is_valid():
        form.save()
        context['success'] = True

    context['form'] = form
    return render(request, template_name, context)
项目:elephant-sms-ews    作者:stultus    | 项目源码 | 文件源码
def __init__(self, *args, **kwargs):
        super(SetPasswordForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()

        self.helper.layout = Layout(
            Field('new_password1', placeholder="Enter new password",
                  autofocus=""),
            Field('new_password2', placeholder="Enter new password (again)"),
            Submit('pass_change', 'Change Password', css_class="btn-warning"),
            )
项目:no-design-slack-clone    作者:metsavaht    | 项目源码 | 文件源码
def __init__(self, user, *args, **kwargs):
        super(SetPasswordForm, self).__init__(user, *args, **kwargs)

        del self.fields['new_password2']
项目:intel-manager-for-lustre    作者:intel-hpdd    | 项目源码 | 文件源码
def is_valid(self, bundle, request=None):
        data = bundle.data or {}
        if request.method == "PUT":
            errors = {}
            try:
                user = get_object_or_404(User, pk=data['id'])
            except KeyError:
                errors['id'] = ['id attribute is mandatory']
            else:
                change_pw_fields = ['new_password1', 'new_password2']
                if any((True for k in change_pw_fields if data[k] is not None)):
                    from django.contrib.auth.forms import PasswordChangeForm, SetPasswordForm
                    # Non-superusers always require old_password
                    # Superusers require old_password when editing themselves
                    if not request.user.is_superuser or request.user.id == user.id:
                        form = PasswordChangeForm(user, data)
                    else:
                        form = SetPasswordForm(user, data)

                    if not form.is_valid():
                        errors.update(form.errors)

                    return errors

                form = ChromaUserChangeForm(data, instance=user)
                if not form.is_valid():
                    errors.update(form.errors)

            return errors
        elif request.method == "POST":
            form = UserCreationForm(data)

            if form.is_valid():
                return {}
            else:
                return form.errors
        else:
            raise NotImplementedError
项目:django-project-template    作者:thorgate    | 项目源码 | 文件源码
def __init__(self, user, *args, **kwargs):
        super(SetPasswordForm, self).__init__(user, *args, **kwargs)

        del self.fields['new_password2']
项目:waves-demo    作者:lirmm    | 项目源码 | 文件源码
def __init__(self, *args, **kwargs):
        super(SetPasswordForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()

        self.helper.layout = Layout(
            Field('new_password1', placeholder="Enter new password",
                  autofocus=""),
            Field('new_password2', placeholder="Enter new password (again)"),
            Submit('pass_change', 'Change Password', css_class="btn-warning"),
        )
项目:djangocms-onepage-example    作者:thorgate    | 项目源码 | 文件源码
def __init__(self, user, *args, **kwargs):
        super(SetPasswordForm, self).__init__(user, *args, **kwargs)

        del self.fields['new_password2']
项目:tissuelab    作者:VirtualPlants    | 项目源码 | 文件源码
def password_reset_confirm(request, uidb64=None, token=None,
                           template_name='registration/password_reset_confirm.html',
                           token_generator=default_token_generator,
                           set_password_form=SetPasswordForm,
                           post_reset_redirect=None,
                           current_app=None, extra_context=None):
    """
    View that checks the hash in a password reset link and presents a
    form for entering a new password.
    """
    UserModel = get_user_model()
    assert uidb64 is not None and token is not None  # checked by URLconf
    if post_reset_redirect is None:
        post_reset_redirect = reverse('password_reset_complete')
    else:
        post_reset_redirect = resolve_url(post_reset_redirect)
    try:
        uid = urlsafe_base64_decode(uidb64)
        user = UserModel._default_manager.get(pk=uid)
    except (TypeError, ValueError, OverflowError, UserModel.DoesNotExist):
        user = None

    if user is not None and token_generator.check_token(user, token):
        validlink = True
        if request.method == 'POST':
            form = set_password_form(user, request.POST)
            if form.is_valid():
                form.save()
                return HttpResponseRedirect(post_reset_redirect)
        else:
            form = set_password_form(None)
    else:
        validlink = False
        form = None
    context = {
        'form': form,
        'validlink': validlink,
    }
    if extra_context is not None:
        context.update(extra_context)
    return TemplateResponse(request, template_name, context,
                            current_app=current_app)