def edit(request, survey_id): try: survey = Survey.objects.get(id=survey_id) survey_form = SurveyForm(instance=survey) if request.method == 'POST': survey_form = SurveyForm(instance=survey, data=request.POST) if survey_form.is_valid(): Survey.save_sample_size(survey_form) messages.success(request, 'Survey successfully edited.') return HttpResponseRedirect(reverse('survey_list_page')) elif survey_form.errors.get('__all__'): messages.error(request, survey_form.errors['__all__']) context = { 'survey_form': survey_form, 'title': "Edit Survey", 'button_label': 'Save', 'id': 'edit-survey-form', 'cancel_url': request.META.get('HTTP_REFERER') or reverse('survey_list_page'), 'action': reverse('edit_survey_page', args=(survey_id, )) } request.breadcrumbs([ ('Surveys', reverse('survey_list_page')), ]) return render(request, 'surveys/new.html', context) except Survey.DoesNotExist: messages.error(request, 'survey does not exists') return HttpResponseRedirect(reverse('survey_list_page'))
def new(request): response = None survey_form = SurveyForm() if request.method == 'POST': survey_form = SurveyForm(request.POST) if survey_form.is_valid(): Survey.save_sample_size(survey_form) messages.success(request, 'Survey successfully added.') response = HttpResponseRedirect(reverse('survey_list_page')) elif survey_form.errors.get('__all__'): messages.error(request, survey_form.errors['__all__']) context = { 'survey_form': survey_form, 'title': "New Survey", 'button_label': 'Create', 'id': 'add-survey-form', 'action': ".", 'cancel_url': request.META.get('HTTP_REFERER') or reverse('survey_list_page'), } request.breadcrumbs([ ('Surveys', reverse('survey_list_page')), ]) return response or render(request, 'surveys/new.html', context)
def edit(request, survey_id): try: survey = Survey.objects.get(id=survey_id) survey_form = SurveyForm(instance=survey) if request.method == 'POST': survey_form = SurveyForm(instance=survey, data=request.POST) if survey_form.is_valid(): Survey.save_sample_size(survey_form) messages.success(request, 'Survey successfully edited.') return HttpResponseRedirect(reverse('survey_list_page')) elif survey_form.errors.get('__all__'): messages.error(request, survey_form.errors['__all__']) context = {'survey_form': survey_form, 'title': "Edit Survey", 'button_label': 'Save', 'id': 'edit-survey-form', 'cancel_url': request.META.get('HTTP_REFERER') or reverse('survey_list_page'), 'action': reverse('edit_survey_page', args=(survey_id, )) } request.breadcrumbs([ ('Surveys', reverse('survey_list_page')), ]) return render(request, 'surveys/new.html', context) except Survey.DoesNotExist: messages.error(request, 'survey does not exists') return HttpResponseRedirect(reverse('survey_list_page'))
def test_should_be_valid_if_has_sampling_is_false_and_sample_size_is_blank(self): form_data = {'name': 'xyz', 'description': 'survey description', 'has_sampling': False, 'sample_size': 0 } survey_form = SurveyForm(data=form_data) is_valid = survey_form.is_valid()
def test_should_be_invalid_if_has_sampling_is_true_and_sample_size_is_not_provided(self): form_data = {'name': 'xyz', 'description': 'survey description', 'has_sampling': True } error_message = 'Sample size must be specified if has sampling is selected.' survey_form = SurveyForm(data=form_data) self.assertFalse(survey_form.is_valid()) self.assertIn(error_message, survey_form.errors['__all__'])
def test_form_is_not_valid_if_survey_with_same_name_exists(self): form_data = {'name': 'xyz', 'description': 'survey description', 'sample_size': 10 } Survey.objects.create(**form_data) form_data['description'] = 'survey description edited' form_data['sample_size'] = 20 survey_form = SurveyForm(data=form_data) self.assertFalse(survey_form.is_valid())
def test_should_be_valid_if_all_fields_are_given(self): form_data = {'name': 'xyz', 'description': 'survey description', 'has_sampling': True, 'sample_size': 10, 'type': True, } survey_form = SurveyForm(data=form_data) self.assertTrue(survey_form.is_valid())
def test_should_be_valid_if_all_fields_are_given(self): form_data = { 'name': 'xyz', 'description': 'survey description', 'has_sampling': True, 'sample_size': 10 } survey_form = SurveyForm(data=form_data) self.assertTrue(survey_form.is_valid())
def test_should_be_valid_if_editing_with_same_name(self): form_data = { 'name': 'xyz', 'description': 'survey description', 'has_sampling': True, 'sample_size': 10 } survey = Survey.objects.create(**form_data) survey_form = SurveyForm(instance=survey, data=form_data) self.assertTrue(survey_form.is_valid())
def test_should_be_valid_if_editing_with_same_name(self): form_data = {'name': 'xyz', 'description': 'survey description', 'has_sampling': True, 'sample_size': 10, 'type': True, } survey = Survey.objects.create(**form_data) survey_form = SurveyForm(instance=survey, data=form_data) self.assertTrue(survey_form.is_valid())
def test_should_be_invalid_if_editing_with_the_name_of_another_survey(self): form_data = {'name': 'xyz', 'description': 'survey description', 'has_sampling': True, 'sample_size': 10 } survey = Survey.objects.create(**form_data) form_data['name'] = 'Survey 1' form_data['description'] = 'Survey 1 details' Survey.objects.create(**form_data) survey_form = SurveyForm(instance=survey, data=form_data) self.assertFalse(survey_form.is_valid()) self.assertIn("Survey with name %s already exist." % form_data['name'], survey_form.errors['name'])
def index(request): surveys = Survey.objects.all().order_by('created') context = { 'surveys': surveys, 'request': request, 'survey_form': SurveyForm() } return render(request, 'surveys/index.html', context)
def edit(request, survey_id): survey = Survey.objects.get(id=survey_id) survey_form = SurveyForm(instance=survey) if request.method == 'POST': survey_form = SurveyForm(instance=survey, data=request.POST) if survey_form.is_valid(): Survey.save_sample_size(survey_form) messages.success(request, 'Survey successfully edited.') return HttpResponseRedirect('/surveys/') context = {'survey_form': survey_form, 'title': "Edit Survey", 'button_label': 'Save', 'id': 'edit-survey-form', 'cancel_url': '/surveys/', 'action': '/surveys/%s/edit/' %survey_id } return render(request, 'surveys/new.html', context)
def edit(request, survey_id): survey = Survey.objects.get(id=survey_id) survey_form = SurveyForm(instance=survey) if request.method == 'POST': survey_form = SurveyForm(instance=survey, data=request.POST) if survey_form.is_valid(): Survey.save_sample_size(survey_form) messages.success(request, 'Survey successfully edited.') return HttpResponseRedirect('/surveys/') context = { 'survey_form': survey_form, 'title': "Edit Survey", 'button_label': 'Save', 'id': 'edit-survey-form', 'cancel_url': '/surveys/', 'action': '/surveys/%s/edit/' % survey_id } return render(request, 'surveys/new.html', context)
def new(request): response = None survey_form = SurveyForm() if request.method == 'POST': survey_form = SurveyForm(request.POST) if survey_form.is_valid(): Survey.save_sample_size(survey_form) messages.success(request, 'Survey successfully added.') response = HttpResponseRedirect('/surveys/') context = {'survey_form': survey_form, 'title': "New Survey", 'button_label': 'Create', 'id': 'add-survey-form', 'action': "/surveys/new/", 'cancel_url': '/surveys/', } return response or render(request, 'surveys/new.html', context)
def new(request): response = None survey_form = SurveyForm() if request.method == 'POST': survey_form = SurveyForm(request.POST) if survey_form.is_valid(): Survey.save_sample_size(survey_form) messages.success(request, 'Survey successfully added.') response = HttpResponseRedirect('/surveys/') context = { 'survey_form': survey_form, 'title': "New Survey", 'button_label': 'Create', 'id': 'add-survey-form', 'action': "/surveys/new/", 'cancel_url': '/surveys/', } return response or render(request, 'surveys/new.html', context)
def test_saves_survey_with_sample_size_zero_if_has_sampling_is_false(self): form_data = { 'name': 'survey rajni', 'description': 'survey description rajni', 'has_sampling': False, 'sample_size': 10, 'type': True, } survey_form = SurveyForm(data=form_data) Survey.save_sample_size(survey_form) saved_survey = Survey.objects.filter( name=form_data['name'], has_sampling=form_data['has_sampling']) self.failUnless(saved_survey) self.assertEqual(0, saved_survey[0].sample_size)
def new(request): response = None survey_form = SurveyForm() if request.method == 'POST': survey_form = SurveyForm(request.POST) if survey_form.is_valid(): Survey.save_sample_size(survey_form) messages.success(request, 'Survey successfully added.') response = HttpResponseRedirect(reverse('survey_list_page') ) elif survey_form.errors.get('__all__'): messages.error(request, survey_form.errors['__all__']) context = {'survey_form': survey_form, 'title': "New Survey", 'button_label': 'Create', 'id': 'add-survey-form', 'action': ".", 'cancel_url': request.META.get('HTTP_REFERER') or reverse('survey_list_page'), } request.breadcrumbs([ ('Surveys', reverse('survey_list_page')), ]) return response or render(request, 'surveys/new.html', context)
def index(request): surveys = Survey.objects.all().order_by('created') search_fields = ['name', 'description'] if request.GET.has_key('q'): surveys = get_filterset(surveys, request.GET['q'], search_fields) if request.GET.has_key('type'): surveys = surveys.filter(type=ast.literal_eval(request.GET['type'])) if request.GET.has_key('isopen'): surveys = surveys.filter(type=ast.literal_eval(request.GET['isopen'])) context = { 'surveys': surveys, 'request': request, 'placeholder': 'name, description', 'survey_form': SurveyForm() } return render(request, 'surveys/index.html', context)
def test_should_not_be_valid_if_name_field_is_empty_or_blank(self): survey_form = SurveyForm() self.assertFalse(survey_form.is_valid())
def test_should_have_name_description_type_and_sample_size_fields(self): survey_form = SurveyForm() fields = ['name', 'description', 'sample_size', 'has_sampling'] [self.assertIn(field, survey_form.fields) for field in fields]