示例#1
0
def new(request, indicator_id):
    try:
        indicator = Indicator.objects.get(id=indicator_id)
        formula_form = FormulaForm(indicator=indicator)
        new_formula_url = '/indicators/%s/formula/new/' % indicator_id

        if request.method == 'POST':
            formula_form = FormulaForm(indicator=indicator, data=request.POST)
            _process_new_request(request, formula_form, new_formula_url,
                                 indicator)

        form_title = 'Formula for Indicator %s' % indicator.name
        request.breadcrumbs([
            ('Indicator List', reverse('list_indicator_page')),
        ])
        return render(
            request, 'formula/new.html', {
                'action': new_formula_url,
                'cancel_url': reverse('list_indicator_page'),
                'formula_form': formula_form,
                'button_label': 'Create',
                'title': form_title,
                'existing_formula': indicator.formula.all(),
                'indicator': indicator
            })
    except Indicator.DoesNotExist:
        error_message = "The indicator requested does not exist."
        messages.error(request, error_message)
        return HttpResponseRedirect("/indicators/")
示例#2
0
    def test_valid_if_percentage_indicator_and_formula_with_same_numerator_and_denominator_does_not_exists(self):
        form_data = {'numerator': self.question_1.id,
                     'denominator': self.question_2.id,
                     'denominator_type': 'QUESTION'}

        formula_form = FormulaForm(indicator=self.percentage_indicator, data=form_data)
        self.assertTrue(formula_form.is_valid())
示例#3
0
    def test_valid_if_percentage_indicator_and_formula_with_same_numerator_and_denominator_does_not_exists(self):
        form_data = {'numerator': self.question_1.id,
                     'denominator': self.question_2.id,
                     'denominator_type': 'QUESTION'}

        formula_form = FormulaForm(indicator=self.percentage_indicator, data=form_data)
        self.assertFalse(formula_form.is_valid())
示例#4
0
    def test_should_not_be_valid_if_count_indicator_and_formula_with_same_count_questions_exists(self):

        form_data = {'count': self.question_1.id,
                     'denominator_type': 'QUESTION'}

        Formula.objects.create(count=self.question_1, indicator=self.count_indicator)

        formula_form = FormulaForm(indicator=self.count_indicator, data=form_data)
        self.assertFalse(formula_form.is_valid())
        self.assertIn('Formula already exist for indicator %s.' % self.percentage_indicator.name, formula_form.errors['__all__'])
示例#5
0
    def test_should_be_valid_if_count_indicator_and_formula_with_same_count_questions_does_not_exist(
            self):
        form_data = {
            'count': self.question_1.id,
            'denominator_type': 'QUESTION'
        }

        formula_form = FormulaForm(indicator=self.count_indicator,
                                   data=form_data)
        self.assertTrue(formula_form.is_valid())
示例#6
0
    def test_should_not_be_valid_if_count_indicator_and_formula_with_same_count_questions_exists(self):

        form_data = {'count': self.question_1.id,
                     'denominator_type': 'QUESTION'}

        Formula.objects.create(count=self.question_1, indicator=self.count_indicator)

        formula_form = FormulaForm(indicator=self.count_indicator, data=form_data)
        self.assertFalse(formula_form.is_valid())
        self.assertIn('Formula already exist for indicator %s.' % self.percentage_indicator.name, formula_form.errors['__all__'])
示例#7
0
    def test_should_not_be_valid_if_count_indicator_and_formula_with_same_groups_exists(self):

        form_data = {'numerator': self.question_1.id,
                     'groups': self.group.id,
                     'denominator_type': 'GROUP'}

        Formula.objects.create(numerator=self.question_1, groups=self.group, indicator=self.count_indicator)

        formula_form = FormulaForm(indicator=self.count_indicator, data=form_data)
        self.assertFalse(formula_form.is_valid())
        self.assertIn('Formula already exist for indicator %s.' % self.count_indicator.name, formula_form.errors['__all__'])
示例#8
0
    def test_should_not_be_valid_if_percentage_indicator_and_formula_with_same_groups_exists(self):

        form_data = {'numerator': self.question_1.id,
                     'groups': self.group.id,
                     'denominator_type': 'GROUP'}

        Formula.objects.create(numerator=self.question_1, groups=self.group, indicator=self.percentage_indicator)

        formula_form = FormulaForm(indicator=self.percentage_indicator, data=form_data)
        self.assertFalse(formula_form.is_valid())
        self.assertIn('Formula already exist for indicator %s.' % self.percentage_indicator.name, formula_form.errors['__all__'])
示例#9
0
    def test_should_have_only_questions_for_batch_in_numerator_and_denominator_for_percentage_indicator_in_module(self):
        formula_form = FormulaForm(indicator=self.percentage_indicator)

        all_batch_questions = [self.question_1, self.question_2]
        questions_not_in_batch = [self.question_3, self.question_4]

        self.assertIn((self.group.id, self.group.name), formula_form.fields['groups'].choices)
示例#10
0
    def test_should_have_count_fields_if_indicator_is_percentage_indicator(self):

        formula_form = FormulaForm(indicator=self.count_indicator)

        fields = ['count', 'denominator_type', 'groups', 'denominator_options']
        deleted_fields = ['numerator', 'numerator_options', 'denominator']

        [self.assertIn(field, formula_form.fields) for field in fields]
        [self.assertNotIn(field, formula_form.fields) for field in deleted_fields]
示例#11
0
    def test_should_have_only_questions_for_batch_in_count_for_count_indicator(
            self):
        formula_form = FormulaForm(indicator=self.count_indicator)

        all_batch_questions = [self.question_1, self.question_2]
        questions_not_in_batch = [self.question_3, self.question_4]
        [
            self.assertIn((question.id, question.text),
                          formula_form.fields['count'].choices)
            for question in all_batch_questions
        ]
        [
            self.assertNotIn((question.id, question.text),
                             formula_form.fields['count'].choices)
            for question in questions_not_in_batch
        ]
示例#12
0
    def test_should_be_valid_if_count_indicator_and_formula_with_same_count_questions_does_not_exist(self):
        form_data = {'count': self.question_1.id,
                     'denominator_type': 'QUESTION'}

        formula_form = FormulaForm(indicator=self.count_indicator, data=form_data)
        self.assertTrue(formula_form.is_valid())