def test_HealthCheckupForm_valid(self): data = { "student_id": 1, "appointment_date": "2017-01-01", "appointment_type": "CHECKUP", } form = HealthCheckupForm(data) self.assertTrue(form.is_valid())
def test_HealthCheckupForm_invalid(self): data = { "appointment_date": "2017-01-01", "appointment_type": "CHECKUP", } form = HealthCheckupForm(data) self.assertFalse(form.is_valid()) self.assertEqual(form.errors, {'student_id': [u'This field is required.']})
def health_form(request, student_id=0, appointment_date=TODAY, appointment_type=None): next_url = request.GET.get('next') if request.method == 'POST': form = HealthForm(request.POST) instance, created = Health.objects.get_or_create( student_id=IntakeSurvey.objects.get(pk=form.data['student_id']), appointment_date=form.data['appointment_date'], appointment_type=form.data['appointment_type']) form = HealthForm(request.POST, instance=instance) if form.is_valid(): #process instance = form.save() action = 'Input ' if created else 'Updated ' message = (action + instance.appointment_type + ' for ' + instance.student_id.name) log = NotificationLog(user=request.user, text=message, font_awesome_icon='fa-medkit') log.save() #then return return HttpResponseRedirect(next_url + '#health') else: if student_id > 0 and appointment_type: try: instance = Health.objects.get( student_id=IntakeSurvey.objects.get(pk=student_id), appointment_date=appointment_date, appointment_type=appointment_type) if appointment_type == 'DENTAL': form = HealthDentalForm(instance=instance) else: form = HealthCheckupForm(instance=instance) except ObjectDoesNotExist: form = HealthForm({ 'student_id': student_id, 'appointment_date': appointment_date, 'appointment_type': appointment_type }) else: if student_id > 0: form = HealthForm({ 'student_id': student_id, 'appointment_date': appointment_date }) else: form = HealthForm() context = {'form': form, 'student_id': student_id, 'next_url': next_url} return render(request, 'mande/healthform.html', context)