def edit_profile(request):
    profile_form = None
    email_form = None
    user = request.user
    if request.method == 'POST':
        post_email = request.POST.get('email')
        profile_form = EditProfileForm(instance=user, data=request.POST)
        if post_email != user.email:
            email_form = AddEmailForm(user=user, data=request.POST)
        if profile_form.is_valid() and (not email_form or email_form.is_valid()):
            profile_form.save()
            if email_form:
                email_form.save(request=request)
                return redirect('account_email_verification_sent')
            return redirect('view_profile')

    if not profile_form:
        profile_form = EditProfileForm(instance=user)
    if not email_form:
        email_form = AddEmailForm(user=user, initial={'email': user.email})
    context = {'edit_profile_form': profile_form, 'add_email_form': email_form}
    return render(request, 'quits_users/edit_profile.html', context)
 def test_last_name_required(self):
     form = EditProfileForm(data={
         'first_name': 'Amy',
         'last_name': '',
     })
     self.assertFalse(form.is_valid())
 def test_last_name_required(self):
     form = EditProfileForm(data={
         'first_name': 'Amy',
         'last_name': '',
     })
     self.assertFalse(form.is_valid())
 def test_form_renders_last_name_input(self):
     form = EditProfileForm()
     self.assertIn('id="id_last_name"', form.as_p())
 def test_form_renders_last_name_input(self):
     form = EditProfileForm()
     self.assertIn('id="id_last_name"', form.as_p())