def test_valid_invalid_form(self): ''' Check form validation ''' # login as admin to access student edit form self.client.login(username='******', password='******') # test valid form with all required fields added person = Contact.objects.create( name=self.data['name'], last_name=self.data['last_name'], jabber=self.data['jabber'], skype=self.data['skype']) form = ContactForm(data={ 'name': person.name, 'last_name': person.last_name, 'jabber': person.jabber, 'skype': person.skype}) self.assertTrue(form.is_valid()) Contact.objects.all().delete() # test valid form with one required fields missed person = Contact.objects.create( name=self.data['name'], last_name=self.data['last_name'], jabber=self.data['jabber']) form = ContactForm(data={ 'name': person.name, 'last_name': person.last_name, 'jabber': person.jabber}) self.assertFalse(form.is_valid())
def contact_form(request): if request.user.is_authenticated(): if request.POST: form = ContactForm(request.POST, request.FILES, instance=request.user) if form.is_valid(): for team in request.user.team_set.all(): team.user.remove(request.user) for team in form.cleaned_data['teams']: team.user.add(request.user) form.save() else: if request.is_ajax(): errors_dict = {} if form.errors: for error in form.errors: e = form.errors[error] errors_dict[error] = unicode(e) return HttpResponseBadRequest(json.dumps(errors_dict)) else: form = ContactForm( instance=request.user, initial={'teams': [team.id for team in request.user.team_set.all()]}) return render(request, "contact_form.html", {'form': form}) raise PermissionDenied
def test_invalid_form(self): """ Check that form must be invalid without required parameters """ form_data = { 'username': '******', 'email': '*****@*****.**', 'first_name': 'test_name', } form = ContactForm(data=form_data) self.assertFalse(form.is_valid())
def test_valid_invalid_form(self): ''' Check form validation ''' # login as admin to access student edit form self.client.login(username='******', password='******') # test valid form with all required fields added person = Contact.objects.create(name=self.data['name'], last_name=self.data['last_name'], jabber=self.data['jabber'], skype=self.data['skype']) form = ContactForm( data={ 'name': person.name, 'last_name': person.last_name, 'jabber': person.jabber, 'skype': person.skype }) self.assertTrue(form.is_valid()) Contact.objects.all().delete() # test valid form with one required fields missed person = Contact.objects.create(name=self.data['name'], last_name=self.data['last_name'], jabber=self.data['jabber']) form = ContactForm( data={ 'name': person.name, 'last_name': person.last_name, 'jabber': person.jabber }) self.assertFalse(form.is_valid())