如何通过Django FormView CBV在SetPasswordForm中初始化用户?

时间:2019-04-19 19:11:36

标签: python django django-forms

我有以下视图,该视图利用了Django的内置SetPasswordForm。表单本身要求您在创建表单时使用用户(settings.AUTH_USER_MODEL)对其进行初始化。

该视图验证uid / token get参数(或允许经过身份验证的用户),然后向用户显示一个表单。我有GET请求可以正确创建表单,但是有更好的方法吗?

设置POST请求中当前为self.init_user的最佳方法是什么:将user.pk作为隐藏字段添加到SetPasswordForm或设置会话对象?

from django.views.generic.edit import FormView
from django.contrib.auth.forms import SetPasswordForm

class PasswordResetView(FormView):
    """
    Reset user password. Either with provided uid/token get parameters
    or if the user is logged in.
    """
    template_name = 'users/password_reset.html'
    form_class = SetPasswordForm
    success_url = 'home'
    init_user = None

    def corrupt_link_redirect(self, request):
        messages.error(
            self.request,
            user_strings.PASSWORD_RESET_INVALID_LINK
        )
        return redirect('forgot_password')

    def get_form(self):
        form_class = self.get_form_class()
        if not self.init_user:
            return Http404() ## Refine this
        return form_class(self.init_user, **self.get_form_kwargs())

    def post(self, request, *args, **kwargs):
        ## *****************************
        ## EITHER NEED TO INCLUDE THE USER.PK IN THE SetPasswordForm OR 
        ## PUT IT IN A SESSION SO WE CAN SET self.init_user TO PREVENT
        ## THE get_form() METHOD FROM 404'ING
        ## *****************************
        return super(PasswordResetView, self).post(request, *args, **kwargs)

    def get(self, request, *args, **kwargs):
        if request.user.is_authenticated:
            ## User is logged in so present them with the password reset form
            self.init_user = request.user
            return super(PasswordResetView, self).get(request, *args, **kwargs)
        try:
            uidb64 = request.GET.get('uid')
            token = request.GET.get('token')
            if uidb64 is None or token is None:
                return self.corrupt_link_redirect(request) ## Failure redirect
            uid = force_text(urlsafe_base64_decode(uidb64))
            user = User.objects.get(pk=uid)
        except (TypeError, ValueError, OverflowError, User.DoesNotExist):
            return self.corrupt_link_redirect(request) ## Failure redirect

        if user is not None and PasswordResetTokenGenerator().check_token(user, token):
            self.init_user = user
            return super(PasswordResetView, self).get(request, *args, **kwargs)
        return self.corrupt_link_redirect(request) ## Failure redirect

    def form_valid(self, form):
        try:
            form.save(self.request)
        except User.DoesNotExist:
            pass
        messages.success(
            self.request,
            user_strings.PASSWORD_RESET_SUCCESS + \
            user_strings.PASSWORD_RESET_SUCCESS_LOGIN_REMINDER if not self.request.user.is_authenticated else ''
        )
        return super(PasswordResetView, self).is_valid(form)

0 个答案:

没有答案