开发者

View code for email validation

开发者 https://www.devze.com 2023-03-11 15:09 出处:网络
I have an \'account\' view, where a user can change his email, name, and password. For changing his email, he has to confirm his email address by clicking a confirmation link. My view code looks prett

I have an 'account' view, where a user can change his email, name, and password. For changing his email, he has to confirm his email address by clicking a confirmation link. My view code looks pretty bloated, and I was wondering how I could improve it and make it more concise. Here is what I currently have --

In the model:

class EmailChangeAuth(models.Model):
    auth_key = models.CharField(max_length=42)
    user = models.ForeignKey(User)
    new_email = models.CharField(max_length=256)

And in the view:

@login_required
def account(request, changed_email=''):
    old_email = User.objects.get(username=request.user.username).email
    name_message = password_message = email_message = ''
    change_name_form = ChangeNameForm(instance=request.user)
    change_password_form = PasswordChangeForm(user=request.user)
    change_email_form = ChangeEmailForm(instance=request.user)
    if request.method == "POST":
        if "change_name" in request.POST:
            change_name_form = ChangeNameForm(data=request.POST or None, instance=request.user)
            if change_name_form.is_valid():
                change_name_form.save()
                name_message = 'Your name has been changed.'
        if "change_password" in request.POST:
            change_password_form = PasswordChangeForm(data=request.POST or None, user = request.user)
            if change_password_form.is_valid():
                change_password_form.save()        
                password_message = 'Your password has been changed.'                    
        if "change_email" in request.POST:
            change_email_form = ChangeEmailForm(data=request.POST or None, instance=request.user)
            if change_email_form.is_valid() and request.POST['email'] != old_email:
                auth_key = uuid.uuid4()
                email_auth = EmailChangeAuth(auth_key=auth_key, new_email=request.POST['email'], user=request.user)
                email_auth.save()
                subject = "Please confirm your email address"
                link = "http://127.0.0.1:8000" + reverse('change_email') + str(auth_key) +'/'
                send_mail(
                'Please confirm your email address',
                'Please click the following link to confirm your email address: \n \n%s' %(link),
                settings.DEFAULT_FROM_EMAIL,
                [request.POST['email']]
            )
                email_message = "Please comfirm your email to complete changes."

    return render_to_response('userprofile/account.html', 
                       {
                        'change_name_form': change_name_form,
                        'old_email': old_email,
                        'change_email_form': change_email_form, 
                        'change_password_form': change_password_form,
                        'changed_email': changed_email,
                        'password_message': password_message,
                        'name_message': name_message,
                        'email_message': email_message,}, 
                        context_instance=RequestContext(request))

d开发者_JS百科ef change_email(request, auth_key):
    try:
        email_object = EmailChangeAuth.objects.get(auth_key=auth_key)
        user = email_object.user
        user.email = email_object.new_email
        user.save()
        changed_email = "Your email has been changed."
        email_object.delete()
        return account(request, changed_email=changed_email)
    except EmailChangeAuth.DoesNotExist:
        return account(request)

Where would be a good place to start to improve this massive view code?


Well, you could start out by splitting up the code into several views. like def change_email(request, user_id): and def change_password(request, user_id): and so on, instead of having one account view.

0

精彩评论

暂无评论...
验证码 换一张
取 消