def test_invalid_data_update(self): """ is validation massages is display """ form_data = {'date_of_birth': '1010-1010', 'email': 'test', 'jabber': 'test'} form = PersonEditForm(data=form_data) self.assertFalse(form.is_valid()) self.assertIn(u'Enter a valid date', str(form['date_of_birth'].errors)) self.assertIn(u'Enter a valid email address', str(form['email'].errors)) self.assertIn(u'Enter a valid email address', str(form['jabber'].errors))
def test_resize_image(self): """ is image save, resize """ person = Person.objects.first() photo = create_img() data = {'first_name': 'first_name', 'last_name': 'last_name', 'date_of_birth': '1990-10-09', 'other': 'other', 'bio': 'bio', 'email': '*****@*****.**', 'jabber': '*****@*****.**', 'skype': 'skype'} photo = SimpleUploadedFile(photo.name, photo.read()) form = PersonEditForm(data, dict(photo=photo), instance=person) self.assertTrue(form.is_valid()) form.save() person = Person.objects.first() self.assertLessEqual(person.photo.width/person.photo.height, 500/250) self.assertTrue(person.photo.width and person.photo.height <= 200)
def edit_person(request): contact = Person.objects.first() form = PersonEditForm(instance=contact) if request.POST and request.is_ajax(): form = PersonEditForm(request.POST, request.FILES, instance=contact) response = {} if form.is_valid(): contact = form.save(commit=False) contact.save() response['photo'] = str(contact.photo) response['success'] = 'true' else: if form.errors: errs = {} for error in form.errors: e = form.errors[error] errs[error] = unicode(e) response['success'] = 'false' response['errors'] = errs return HttpResponse(json.dumps(response), content_type='application/json') return render(request, 'hello/edit_person.html', {'form': form, 'contact': contact})
def edit(request, pk): person = Person.objects.get(id=pk) form = PersonEditForm(instance=person) photo = person.photo.url if person.photo else None if request.method == 'POST' and request.is_ajax(): form = PersonEditForm(request.POST, request.FILES, instance=person) response_data = dict() if form.is_valid(): form.save() else: response_data['errs'] = process_form_err(form) logger.info('Errors of form saving!' + str(response_data['errs'])) form.photo = person.photo.url if person.photo else None response_data['form'] = (RemoteForm(form)).as_dict() response_data['form']['fields']['photo']['initial'] = None response_data['photo'] = photo return HttpResponse(json.dumps(response_data), content_type='application/javascript') return render(request, 'hello/edit.html', {'form': form, 'person': person, 'photo': photo})