def test_invalid_data(self): form = ProfileForm(data=self.invalid_data) self.assertEqual(form.errors['first_name'], [u"Ensure this value has at most 12 characters (it has 19)."]) self.assertEqual(form.errors['last_name'], [u"Ensure this value has at most 12 characters (it has 18)."]) self.assertEqual(form.errors['email'], [u"Enter a valid email address."]) self.assertEqual(form.errors['about_text'], [u"Ensure this value has at most 500 characters (it has 525)."]) self.assertEqual(form.errors['homepage'], [u"Enter a valid URL."]) self.assertEqual(form.errors['github'], [u"Ensure this value has at most 39 characters (it has 70)."]) self.assertEqual(form.errors['twitter'], [u"Ensure this value has at most 15 characters (it has 35)."]) self.assertFalse(form.is_valid())
def test_invalid_data(self): form = ProfileForm(data=self.invalid_data) self.assertEqual(form.errors["first_name"], ["Ensure this value has at most 12 characters (it has 19)."]) self.assertEqual(form.errors["last_name"], ["Ensure this value has at most 12 characters (it has 18)."]) self.assertEqual(form.errors["email"], ["Enter a valid email address."]) self.assertEqual(form.errors["about_text"], ["Ensure this value has at most 500 characters (it has 525)."]) self.assertEqual(form.errors["homepage"], ["Enter a valid URL."]) self.assertEqual(form.errors["github"], ["Ensure this value has at most 39 characters (it has 70)."]) self.assertEqual(form.errors["twitter"], ["Ensure this value has at most 15 characters (it has 35)."]) self.assertFalse(form.is_valid())
def test_empty_data(self): test_data = { 'first_name': '', 'last_name': '', 'email': '', 'display_picture': False, 'about_text': '', 'homepage': '', 'github': '', 'twitter': '', } form = ProfileForm(data=test_data) self.assertTrue(form.is_valid())
def test_empty_data(self): test_data = { "first_name": "", "last_name": "", "email": "", "display_picture": False, "about_text": "", "homepage": "", "github": "", "twitter": "", } form = ProfileForm(data=test_data) self.assertTrue(form.is_valid())
def edit_profile(request): user = RedditUser.objects.get(user=request.user) if request.method == 'GET': profile_form = ProfileForm(instance=user) elif request.method == 'POST': profile_form = ProfileForm(request.POST, instance=user) if profile_form.is_valid(): profile = profile_form.save(commit=False) profile.update_profile_data() profile.save() messages.success(request, "Profile settings saved") else: raise Http404 return render(request, 'private/edit_profile.html', {'form': profile_form})
def edit_profile(request): user = RedditUser.objects.get(user=request.user) if request.method == 'GET': profile_form = ProfileForm(instance=user) # clean this up. How to do it automatically? try: cohort = Cohort.objects.filter(reddit_users=user)[0] profile_form.initial['cohort'] = cohort.pk except: pass elif request.method == 'POST': profile_form = ProfileForm(request.POST, instance=user) if profile_form.is_valid(): profile = profile_form.save(commit=False) profile.update_profile_data() # update user object with email from reddituser profile update request.user.email = profile_form.cleaned_data['email'] request.user.save() # if cohort is changing if profile_form.cleaned_data['cohort'] != user.cohorts.all(): # if going from None to cohort if user.cohorts.all() == []: # add user to cohort in form user.cohorts.add(profile_form.cleaned_data['cohort']) # elif going from cohort to cohort: elif profile_form.cleaned_data['cohort'] is not None: # add user to cohort in form user.cohorts.add(profile_form.cleaned_data['cohort']) # elif going from cohort to none else: user.cohorts.remove(user.cohorts.all()[0]) profile.save() messages.success(request, "Profile settings saved") else: raise Http404 return render(request, 'private/edit_profile.html', {'form': profile_form})
def test_invalid_data(self): form = ProfileForm(data=self.invalid_data) self.assertEqual( form.errors['first_name'], [u"Ensure this value has at most 12 characters (it has 19)."]) self.assertEqual( form.errors['last_name'], [u"Ensure this value has at most 12 characters (it has 18)."]) self.assertEqual(form.errors['email'], [u"Enter a valid email address."]) self.assertEqual( form.errors['about_text'], [u"Ensure this value has at most 500 characters (it has 525)."]) self.assertEqual(form.errors['homepage'], [u"Enter a valid URL."]) self.assertEqual( form.errors['github'], [u"Ensure this value has at most 39 characters (it has 70)."]) self.assertEqual( form.errors['twitter'], [u"Ensure this value has at most 15 characters (it has 35)."]) self.assertFalse(form.is_valid())
def test_all_valid_data(self): form = ProfileForm(data=self.valid_data) self.assertTrue(form.is_valid())