def health(request): """User do a healthy activity or uses substitutes""" context = {} tz_offset = get_client_offset(request) # check if alternatives are in parameters to fill fields with actual alternatives displayed_alt = Alternative.objects.filter(user=request.user, display=True) update_dt_user_model_field(request.user, tz_offset) context['alternatives'] = displayed_alt if displayed_alt: form = HealthForm(request.user, tz_offset) if request.method == 'POST': form = HealthForm(request.user, tz_offset, request.POST) if form.is_valid(): new_health = HealthManager(request.user, form.cleaned_data, tz_offset) new_health.create_conso_alternative() return redirect('QuitSoonApp:today') context['form'] = form healthy = ConsoAlternative.objects.filter( user=request.user).order_by('-datetime_alter') context['health'] = healthy if healthy: last = healthy.latest('datetime_alter').datetime_alter context['lasthealth'] = get_delta_last_event(last) return render(request, 'QuitSoonApp/health.html', context)
def test_get_ecig_none(self): data = { 'date_health': datetime.date(2020, 5, 26), 'time_health': datetime.time(12, 56), 'type_alternative_field': 'Su', 'su_field': self.db_alternative_substitut_ecig.id, 'ecig_vape_or_start': [] } form = HealthForm(self.usertest, data) self.assertFalse(form.is_valid()) self.assertTrue("Vous avez sélectionné la cigarette électronique" in form.errors['__all__'][0])
def test_required_fields(self): form = HealthForm(self.usertest, {}) self.assertFalse(form.is_valid()) self.assertRaises(ValidationError) self.assertEqual( form.errors, { 'date_health': ['Ce champ est obligatoire.'], 'time_health': ['Ce champ est obligatoire.'], 'type_alternative_field': ['Ce champ est obligatoire.'], '__all__': ["Vous n'avez pas renseigné de durée pour cette activité"], })
def test_valid_data(self): data = { 'date_health': datetime.date(2020, 5, 26), 'time_health': datetime.time(12, 56), 'duration_hour': 1, 'duration_min': 30, 'type_alternative_field': 'So', 'sp_field': self.db_alternative_activity_sp.id, 'so_field': self.db_alternative_activity_so.id, 'su_field': self.db_alternative_substitut_p24.id, } form = HealthForm(self.usertest, data) self.assertTrue(form.is_valid())
def test_date_form_inf_today(self): """test HealthForm with valid date""" data = { 'date_health': datetime.date(2020, 5, 25), 'time_health': datetime.time(2, 56), 'duration_hour': 1, 'duration_min': 30, 'type_alternative_field': 'So', 'sp_field': self.db_alternative_activity_sp.id, 'so_field': self.db_alternative_activity_so.id, 'su_field': self.db_alternative_substitut_p24.id, } form = HealthForm(self.usertest, -120, data) self.assertTrue(form.is_valid())
def test_duration_0(self): data = { 'date_health': datetime.date(2020, 5, 26), 'time_health': datetime.time(12, 56), 'duration_hour': 0, 'duration_min': 0, 'type_alternative_field': 'So', 'sp_field': self.db_alternative_activity_sp.id, 'so_field': self.db_alternative_activity_so.id, 'su_field': self.db_alternative_substitut_p24.id, } form = HealthForm(self.usertest, data) self.assertFalse(form.is_valid()) self.assertRaises(ValidationError) self.assertEqual( form.errors, { '__all__': ["Vous n'avez pas renseigné de durée pour cette activité"], })