def calc_cri_simple(request): """Calculates simple CRI dosages based on weight. GET parameters: weight: weight in kgs Context: rx: calculated dosages rounded to 3 decimal places """ meds = CRI.objects.filter(calc_type='ez') form = CRISimpleForm() rx = dict() bolus = dict() # zipped list of rates to dosage with default displayed dosages of 0.0 mL for med in meds: rx[med] = list(zip([rate for rate in med.rates], [0.0 * rate for rate in med.rates])) if request.method == 'GET' and request.is_ajax(): form = CRISimpleForm(data=request.GET) if form.is_valid(): weight = float(request.GET['weight']) for med in meds: rx[med] = list(zip([rate for rate in med.rates], [round(weight * med.factor * rate, 3) for rate in med.rates])) # bolus is calculated for diltiazem bolus = {'mg': round(weight * 0.25, 3), 'mL': round(weight * 0.05, 3)} return render(request, 'calc/cri_simple.html', {'navbar': 'calc', 'form': form, 'rx': rx, 'bolus': bolus})
def test_form_rejects_empty_string(self): form = CRISimpleForm(data={'weight': ''}) self.assertFalse(form.is_valid())
def test_form_rejects_non_numeric_input(self): form = CRISimpleForm(data={'weight': 'a string'}) self.assertFalse(form.is_valid()) self.assertEqual(form.errors['weight'], [INVALID_INPUT_ERROR])