Example #1
0
    def test_correct_formtypes(self):
        forms_one = construct_section_forms(None, self.culture, self.section_one)
        assert len(forms_one) == 2
        assert type(forms_one[0]) == IntegerResponseForm
        assert type(forms_one[1]) == FloatResponseForm

        forms_two = construct_section_forms(None, self.culture, self.section_two)
        assert len(forms_two) == 1
        assert type(forms_two[0]) == TextResponseForm
Example #2
0
    def test_section_filtering(self):
        """test that construct_section_forms filters the right questions"""
        forms_one = construct_section_forms(None, self.culture, self.section_one)
        assert len(forms_one) == 2
        assert forms_one[0].initial['question'].number == 1
        assert forms_one[1].initial['question'].number == 2

        forms_two = construct_section_forms(None, self.culture, self.section_two)
        assert len(forms_two) == 1
        assert forms_two[0].initial['question'].number == 3
Example #3
0
def SurveySectionEdit(request, culture, section):
    """Editing of Survey Section"""
    culture_obj = get_object_or_404(Culture, slug=culture)
    section_obj = get_object_or_404(Section, slug=section)
    forms = construct_section_forms(post_data=request.POST or None,
                                    culture_obj=culture_obj,
                                    section_obj=section_obj)

    status = []
    # save if necessary
    if request.method == 'POST':
        all_valid = True
        for form in forms:
            if form.is_valid():
                obj = form.save(commit=False)
                obj.author = request.user
                obj.culture = culture_obj
                obj.save()
                status.append(
                    "Saved Response to question {0}.{1}".format(
                        section_obj.id, obj.question.number))
            else:
                all_valid = False
                # if all forms are valid. Return to survey culture index
        if all_valid:
            return HttpResponseRedirect(
                reverse('survey-culture-index', kwargs={"slug": culture}))
            # if not, fall through to the survey section edit again.
    return render_to_response('survey/survey_section_edit.html', {
        'forms': forms, 'culture': culture_obj, 'section': section_obj, 'status': status,
    }, context_instance=RequestContext(request))
Example #4
0
def SurveySectionEdit(request, culture, section):
    """Editing of Survey Section"""
    culture_obj = get_object_or_404(Culture, slug=culture)
    section_obj = get_object_or_404(Section, slug=section)
    forms = construct_section_forms(post_data=request.POST or None,
                                    culture_obj=culture_obj,
                                    section_obj=section_obj)

    status = []
    # save if necessary
    if request.method == 'POST':
        all_valid = True
        for form in forms:
            if form.is_valid():
                obj = form.save(commit=False)
                obj.author = request.user
                obj.culture = culture_obj
                obj.save()
                status.append("Saved Response to question {0}.{1}".format(
                    section_obj.id, obj.question.number))
            else:
                all_valid = False
                # if all forms are valid. Return to survey culture index
        if all_valid:
            return HttpResponseRedirect(
                reverse('survey-culture-index', kwargs={"slug": culture}))
            # if not, fall through to the survey section edit again.
    return render_to_response('survey/survey_section_edit.html', {
        'forms': forms,
        'culture': culture_obj,
        'section': section_obj,
        'status': status,
    },
                              context_instance=RequestContext(request))
Example #5
0
 def test_attrs_get_set(self):
     """test that the various attrs needed in templates are set on the form"""
     for section in (self.section_one, self.section_two):
         forms = construct_section_forms(None, self.culture, section)
         for f in forms:
             assert hasattr(f, 'qnumber')
             assert f.qnumber == f.initial['question'].number
             assert hasattr(f, 'qinformation')
             assert f.qinformation == f.initial['question'].information
             assert hasattr(f, 'qtext')
             assert f.qtext == f.initial['question'].question
Example #6
0
 def test_args(self):
     with self.assertRaises(AssertionError):
         construct_section_forms(None, None, self.section_one)
     with self.assertRaises(AssertionError):
         construct_section_forms(None, self.culture, None)
     with self.assertRaises(AssertionError):
         construct_section_forms(None, None, None)
Example #7
0
 def test_load_post_data_into_correct_object(self):
     postdata = {
         '1-source': 1,
         '1-codersnotes': u'note',
         '1-response': u'77',
         '1-question': self.question_int.id,
         '1-culture': self.culture.id,
     }
     forms = construct_section_forms(postdata, self.culture, self.section_one)
     assert len(forms) == 2
     # Second form has id 2 - should NOT have changed!
     with self.assertRaises(ValueError):
         forms[1].save(commit=False)
     assert forms[1].cleaned_data['response'] is None
Example #8
0
    def test_cant_override_question(self):
        postdata = {
            '3-source1': 1,
            '3-codersnotes': u'note',
            '3-response': u'77',
            '3-question': self.question_int.id,  # NOTE not question_text
            '3-culture': self.culture.id,
        }
        forms = construct_section_forms(postdata, self.culture, self.section_two)

        assert len(forms) == 1
        assert forms[0].is_valid()
        resp = forms[0].save(commit=False)
        assert resp.question_id == self.question_text.id
        assert resp.question_id != self.question_int.id
Example #9
0
 def test_loads_existing_info_in_database(self):
     """Does info that's already in the database get loaded?"""
     # section two has self.question_text
     resp = TextResponse.objects.create(
         author=self.editor,
         question=self.question_text,
         culture=self.culture,
         source1=self.source,
         codersnotes="Dummy answer",
         response='Lorem ipsum...'
     )
     forms = construct_section_forms(None, self.culture, self.section_two)
     assert len(forms) == 1
     assert forms[0].instance == resp
     assert forms[0].initial['codersnotes'] == resp.codersnotes
     assert forms[0].initial['response'] == resp.response
Example #10
0
 def test_loads_post_data(self):
     """Does POST information get injected?"""
     postdata = {
         '1-source1': 1,
         '1-codersnotes': u'note',
         '1-response': u'77',
         '1-question': self.question_int.id,
         '1-culture': self.culture.id,
     }
     forms = construct_section_forms(postdata, self.culture, self.section_one)
     assert len(forms) == 2
     assert forms[0].is_valid()
     resp = forms[0].save(commit=False)
     assert resp.source1_id == 1
     self.assertEqual(resp.codersnotes, postdata['1-codersnotes'])
     assert resp.response == int(postdata['1-response'])
     assert resp.question_id == self.question_int.id
     assert resp.culture_id == self.culture.id
Example #11
0
    def test_cant_override_culture(self):
        other_culture = Culture.objects.create(
            culture='French', slug='french', editor=self.editor)
        postdata = {
            '3-source1': 1,
            '3-codersnotes': u'note',
            '3-response': u'77',
            '3-question': self.question_text.id,
            '3-culture': other_culture.id,
        }
        forms = construct_section_forms(
            postdata,
            self.culture,
            # correct - this comes from the view.
            self.section_two)

        assert len(forms) == 1
        self.assert_(forms[0].is_valid())
        resp = forms[0].save(commit=False)
        assert resp.culture_id == self.culture.id
        assert resp.culture_id != other_culture.id
Example #12
0
 def test_form_invalid(self):
     form = construct_section_forms(None, self.culture, self.section)[0]
     for invalid in (2, 3, 4, 'fudge', ['a', 'b']):
         self.assertRaises(ValidationError, form.fields['response'].clean, invalid)
Example #13
0
 def test_form_valid(self):
     form = construct_section_forms(None, self.culture, self.section)[0]
     for valid in ('?', '0', '1', 0, 1):
         assert str(valid) == form.fields['response'].clean(valid)
Example #14
0
 def test_form_choices(self):
     form = construct_section_forms(None, self.culture, self.section)[0]
     assert form.fields['response'].choices == form.initial['question'].get_choices(
         with_empty=True)
     assert form.fields['response'].choices == self.question.get_choices(
         with_empty=True)