0

这个问题与Django 有关:仅更新已在 UpdateView 中更改的字段

我正在尝试为用户提供一种更改电子邮件的方法。在将新电子邮件保存为默认电子邮件之前,我想向新提供的地址发送一封电子邮件以确保其有效。新提供的电子邮件将保存在数据库中的临时表中,但之前的(旧)电子邮件将保持不变,直到经过验证。

我遇到的问题是,旧电子邮件在经过验证之前就被新电子邮件覆盖了。如何使用 UpdateView 防止这种情况发生?下面是我的代码:

class AccountUpdate(UpdateView):
    """Updates an account"""

    context_object_name = 'account'
    form_class = UpdateForm
    template_name = 'accounts/update_account.html'
    success_url = '/accounts/home'

    def get_object(self, queryset=None):
        return self.request.user

    @sensitive_variables('new_password')
    @sensitive_post_parameters('new_password')
    def form_valid(self, form):
        account = Account.objects.get(pk=self.request.user.id)
        clean = form.cleaned_data
        new_password = clean.get('new_password')
        old_email = account.email
        new_email = clean.get('email')

        # check for new password and encrypt it before saving
        if new_password:
            #encrypt plain password
            form.instance.password = hash_password(clean['new_password'])

        # check for new email address and save it in temporary field until verified
        # ensure that new email is different and is currently not taken
        if old_email != new_email:
            try:
                # make sure someone else has not used this email as their new email
                temp_taken = Account.objects.get(new_email=new_email)
                if temp_taken:
                    raise ValidationError('An account with this email exist.')

            # save new email and send verification message
            # make sure we set 'DoesNotExist' on the 'Account' object itself
            # it prevents 'str' object has no attribute 'DoesNotExist' error
            except Account.DoesNotExist:
                verifystring = get_random_string()
                self.object.new_email = new_email
                self.object.new_email_verifystring = verifystring
                message = "Hey %s! " \
                      "Please click the link below to verify your new email:" \
                      "<a href='link_here'>Verify Email!</a>" %\
                      (clean['first_name'])
                self.object.email_user(subject='email verification.',
                                   message=message,
                                   from_email='no-reply@localhost')
        else:
            context = {}
            self.object = context.update(first_name=clean.get('first_name'),
                                     last_name=clean.get('last_name'),
                                     username=clean.get('username'),
                                     force_update=False)
        return super(AccountUpdate, self).form_valid(form)

如果其他地方已经存在答案,请指出它。

4

2 回答 2

1

这与基于类的字段没有太大关系,但与不丢失已知有效地址的问题有关。

我建议你在你的Account模型中添加一个字段,叫做new_emailorunverified_email什么的;当帐户更改时,实际email字段保持不变,并将新地址存储到该新字段中,并向其发送验证电子邮件。

只有在验证过程完成后,新地址才会存储到电子邮件字段中,覆盖旧地址。在更新表单中警告用户在他们验证新地址之前将使用他们的旧地址也是谨慎的做法。

于 2013-08-26T19:44:00.677 回答
0

The problem I'm running into is, the old email gets over-written by the new one before it has been verified.

这没有回答如何防止使用 UpdateView 保存字段的问题,但它解决了上述问题。

# views.py snippet
# because we are using the same 'email' variable to add new email addresses
# 'self.object.email' will be saved with the new provided value
# to prevent this from happening, manually set the 'email' variable to the value
# retrieved from the DB
except Account.DoesNotExist:
            verifystring = get_random_string()
            self.object.email = old_email
            self.object.new_email = new_email
于 2013-08-26T17:21:45.297 回答