def test_invalid(self): form_data = { 'name': 'test', 'description': 'description goes here', } batch_form = BatchForm(form_data) self.assertFalse(batch_form.is_valid())
def test_valid(self): form_data = { 'name': 'Batch 1', 'description': 'description goes here', } batch_form = BatchForm(form_data) self.assertTrue(batch_form.is_valid())
def test_valid(self): self.country = LocationType.objects.create(name='Country', slug='country') self.africa = Location.objects.create(name='Africa', type=self.country) self.city_ea = EnumerationArea.objects.create(name="CITY EA") self.city_ea.locations.add(self.africa) self.investigator_1 = Interviewer.objects.create( name="Investigator", ea=self.city_ea, gender='1', level_of_education='Primary', language='Eglish', weights=0) odk = ODKAccess.objects.create(interviewer=self.investigator_1, user_identifier='Test', is_active=True, reponse_timeout=1000, duration='H', odk_token='Test') form_data = { 'name': 'Batch 1', 'description': 'description goes here', } batch_form = BatchForm(form_data) self.assertFalse(batch_form.is_valid())
def _process_form(request, batchform, action_str='added'): if request.method == 'POST': batchform = BatchForm(data=request.POST, instance=batchform.instance) if batchform.is_valid(): batch = batchform.save() _add_success_message(request, action_str) batch_list_url = '/surveys/%s/batches/' % str(batch.survey.id) return HttpResponseRedirect(batch_list_url), batchform return None, batchform
def test_form_should_be_valid_if_name_already_exists_and_there_is_an_instance(self): survey = Survey.objects.create(name="very fast") form_data = { 'name': 'Batch A', 'description': 'description goes here', } batch = Batch.objects.create(survey=survey, name=form_data['name'], description='description') batch_form = BatchForm(data=form_data, instance=batch) self.assertTrue(batch_form.is_valid())
def test_form_should_be_invalid_if_name_already_exists_on_the_same_survey(self): survey = Survey.objects.create(name="very fast") Batch.objects.create(survey=survey, name='Batch A', description='description') form_data = { 'name': 'Batch A', 'description': 'description goes here', } batch_form = BatchForm(data=form_data, instance=Batch(survey=survey)) self.assertFalse(batch_form.is_valid())
def test_form_should_be_valid_if_name_already_exists_on_a_different_survey(self): survey = Survey.objects.create(name="very fast") form_data = { 'name': 'Batch A', 'description': 'description goes here', } Batch.objects.create(survey=survey, name=form_data['name'], description='description') different_survey = Survey.objects.create(name="different") batch_form = BatchForm(data=form_data, instance=Batch(survey=different_survey)) self.assertTrue(batch_form.is_valid())
def _process_form(request, survey_id, instance=None, action_str='edited'): batch_form = BatchForm(instance=instance) response = None if request.method == 'POST': batch_form = BatchForm(data=request.POST, instance=instance) if batch_form.is_valid(): batch_form.save(**request.POST) messages.success(request, 'Question successfully %sed.' % action_str) response = HttpResponseRedirect(reverse('batch_index_page', args=(survey_id,))) else: messages.error(request, 'Question was not %sed.' % action_str) return response, batch_form
def test_form_should_be_valid_if_name_already_exists_and_there_is_an_instance( self): survey = Survey.objects.create(name="very fast") form_data = { 'name': 'Batch A', 'description': 'description goes here', } batch = Batch.objects.create(survey=survey, name=form_data['name'], description='description') batch_form = BatchForm(data=form_data, instance=batch) self.assertTrue(batch_form.is_valid())
def test_form_should_be_valid_if_name_already_exists_on_a_different_survey( self): survey = Survey.objects.create(name="very fast") form_data = { 'name': 'Batch A', 'description': 'description goes here', } Batch.objects.create(survey=survey, name=form_data['name'], description='description') different_survey = Survey.objects.create(name="different") batch_form = BatchForm(data=form_data, instance=Batch(survey=different_survey)) self.assertTrue(batch_form.is_valid())
def edit(request, survey_id, batch_id): batch = Batch.objects.get(id=batch_id, survey__id=survey_id) response, batchform = _process_form(request=request, batchform=(BatchForm(instance=batch)), action_str='edited') context = {'batchform': batchform, 'button_label': "Save", 'id': 'edit-batch-form', 'title': 'New Batch', 'action': '/surveys/%s/batches/%s/edit/' % (batch.survey.id, batch.id)} return response or render(request, 'batches/new.html', context)
def new(request, survey_id): batch = Batch(survey=Survey.objects.get(id=survey_id)) response, batchform = _process_form(request=request, batchform=(BatchForm(instance=batch)), action_str='added') context = {'batchform': batchform, 'button_label': "Create", 'id': 'add-batch-form', 'title': 'New Batch', 'action': '/surveys/%s/batches/new/' % survey_id, 'cancel_url': '/surveys/'} return response or render(request, 'batches/new.html', context)
def test_valid(self): self.country = LocationType.objects.create( name='Country', slug='country') self.africa = Location.objects.create(name='Africa', type=self.country) self.city_ea = EnumerationArea.objects.create(name="CITY EA") self.city_ea.locations.add(self.africa) self.investigator_1 = Interviewer.objects.create(name="Investigator", ea=self.city_ea, gender='1', level_of_education='Primary', language='Eglish', weights=0) odk = ODKAccess.objects.create(interviewer=self.investigator_1, user_identifier='Test', is_active=True, reponse_timeout=1000, duration='H', odk_token='Test') form_data = { 'name': 'Batch 1', 'description': 'description goes here', } batch_form = BatchForm(form_data) self.assertFalse(batch_form.is_valid())
def index(request, survey_id): survey = Survey.objects.get(id=survey_id) batches = Batch.objects.filter(survey__id=survey_id) if request.is_ajax(): batches = batches.values('id', 'name').order_by('name') json_dump = json.dumps(list(batches), cls=DjangoJSONEncoder) return HttpResponse(json_dump, mimetype='application/json') context = {'batches': batches, 'survey': survey, 'request': request, 'batchform': BatchForm(instance=Batch(survey=survey)), 'action': '/surveys/%s/batches/new/' % survey_id, } return render(request, 'batches/index.html', context)
def _process_form(request, survey_id, instance=None, action_str='edited'): batch_form = BatchForm(instance=instance) response = None if request.method == 'POST': batch_form = BatchForm(data=request.POST, instance=instance) if batch_form.is_valid(): batch_form.save(**request.POST) messages.success(request, 'Question successfully %sed.' % action_str) response = HttpResponseRedirect( reverse('batch_index_page', args=(survey_id, ))) else: messages.error(request, 'Question was not %sed.' % action_str) return response, batch_form
def new(request, survey_id): survey = Survey.objects.get(id=survey_id) batch_form = BatchForm(initial={'survey': survey}) response, batchform = _process_form(request, survey_id, action_str='added') request.breadcrumbs([ ('Surveys', reverse('survey_list_page')), (survey.name, reverse('batch_index_page', args=(survey.pk, ))), # (_('%s %s') % (action.title(),model.title()),'/crud/%s/%s' % (model,action)), ]) context = { 'batchform': batchform, 'button_label': "Create", 'id': 'add-batch-form', 'title': 'New Batch', 'action': '/surveys/%s/batches/new/' % survey_id, 'cancel_url': '/surveys/', 'survey': survey } return response or render(request, 'batches/new.html', context)
def index(request, survey_id): survey = Survey.objects.get(id=survey_id) batches = Batch.objects.filter(survey__id=survey_id) # if request.is_ajax(): # batches = batches.values('id', 'name').order_by('name') # json_dump = json.dumps(list(batches), cls=DjangoJSONEncoder) # return HttpResponse(json_dump, mimetype='application/json') request.breadcrumbs([ ('Surveys', reverse('survey_list_page')), ]) can_delete = survey.survey_house_listings.exists( ) is False #if there has been no household listing yet context = { 'batches': batches, 'survey': survey, 'request': request, 'batchform': BatchForm(instance=Batch(survey=survey)), 'action': '/surveys/%s/batches/new/' % survey_id, 'can_delete': can_delete } return render(request, 'batches/index.html', context)
def test_field_required(self): data={'name': '', 'description': ''} batch_form = BatchForm(data) self.assertFalse(batch_form.is_valid()) self.assertEqual(['This field is required.'], batch_form.errors['name'])
def test_field_required(self): data = {'name': '', 'description': ''} batch_form = BatchForm(data) self.assertFalse(batch_form.is_valid()) self.assertEqual(['This field is required.'], batch_form.errors['name'])