def smoke(request): """User smokes""" # check if packs are in parameters to fill fields with actual packs packs = Paquet.objects.filter(user=request.user, display=True) context = {'packs': packs} tz_offset = get_client_offset(request) update_dt_user_model_field(request.user, tz_offset) if packs: smoke_form = SmokeForm(request.user, tz_offset) if request.method == 'POST': smoke_form = SmokeForm(request.user, tz_offset, request.POST) if smoke_form.is_valid(): smoky = SmokeManager(request.user, smoke_form.cleaned_data, tz_offset) smoky.create_conso_cig() return redirect('QuitSoonApp:today') context['smoke_form'] = smoke_form smoke_conso = ConsoCig.objects.filter( user=request.user).order_by('-datetime_cig') context['smoke'] = smoke_conso context['nb_smoke_today'] = smoke_conso.filter( datetime_cig__date=timezone.now().date()).count() if smoke_conso: last = smoke_conso.latest('datetime_cig').datetime_cig context['lastsmoke'] = get_delta_last_event(last) return render(request, 'QuitSoonApp/smoke.html', context)
def test_SmokeForm_is_not_valid(self): """test invalid SmokeForm, datas missing""" form = SmokeForm(self.usertest, {}) self.assertFalse(form.is_valid()) self.assertEqual(form.errors, { 'date_smoke': ['Ce champ est obligatoire.'], 'time_smoke': ['Ce champ est obligatoire.'], 'type_cig_field': ['Ce champ est obligatoire.'], })
def test_clean_inf_today(self): """Function testing if form is invalide for days after today""" data = { 'date_smoke': datetime.date(2020, 6, 18), 'time_smoke': datetime.time(00, 21), 'type_cig_field': 'IND', 'ind_pack_field': self.db_pack_ind.id, 'rol_pack_field': self.db_pack_rol.id, 'nb_pack_field': self.db_pack_nb.id, 'given_field': False, } form = SmokeForm(self.usertest, -120, data) self.assertTrue(form.is_valid())
def test_smokeForm_is_not_valid(self): """test invalid SmokeForm, data missing""" form = SmokeForm(self.usertest, -120, {}) self.assertFalse(form.is_valid()) for error in form.errors: print(error) self.assertEqual( form.errors, { 'date_smoke': ['Ce champ est obligatoire.'], 'time_smoke': ['Ce champ est obligatoire.'], 'type_cig_field': ['Ce champ est obligatoire.'], '__all__': ["Données temporelles incorrectes"] })
def test_smoke_last_none(self): """ test last_smoke method with last one given=True, before exists given=False """ db_smoke_0 = ConsoCig.objects.create( user=self.usertest, date_cig=datetime.date(2020, 6, 16), time_cig=datetime.time(10, 15), paquet=self.db_pack_ind2, ) db_smoke_1 = ConsoCig.objects.create( user=self.usertest, date_cig=datetime.date(2020, 6, 17), time_cig=datetime.time(10, 15), paquet=self.db_pack_nb, ) db_smoke_2 = ConsoCig.objects.create( user=self.usertest, date_cig=datetime.date(2020, 6, 17), time_cig=datetime.time(13, 15), paquet=None, ) db_smoke_3 = ConsoCig.objects.create( user=self.usertest, date_cig=datetime.date(2020, 6, 17), time_cig=datetime.time(13, 15), paquet=None, ) form = SmokeForm(self.usertest, self.valid_datas) self.assertEqual(form.last_smoke, self.db_pack_nb)
def test_smoke_last_none_first_smoke(self): """ test last_smoke method with last smoke given=False smoke """ db_smoke = ConsoCig.objects.create( user=self.usertest, date_cig=datetime.date(2020, 6, 17), time_cig=datetime.time(13, 15), paquet=self.db_pack_ind2, ) form = SmokeForm(self.usertest, self.valid_datas) self.assertEqual(form.last_smoke, self.db_pack_ind2)
def test_config_field(self): """ test config_field method """ db_smoke_1 = ConsoCig.objects.create( user=self.usertest, date_cig=datetime.date(2020, 6, 17), time_cig=datetime.time(10, 15), paquet=self.db_pack_ind2, ) db_smoke_2 = ConsoCig.objects.create( user=self.usertest, date_cig=datetime.date(2020, 6, 17), time_cig=datetime.time(13, 15), given=True, ) db_smoke_3 = ConsoCig.objects.create( user=self.usertest, date_cig=datetime.date(2020, 6, 17), time_cig=datetime.time(13, 15), given=True, ) form = SmokeForm(self.usertest, self.valid_datas) pack1 = self.db_pack_ind pack2 = self.db_pack_ind2 pack3 = self.db_pack_nb self.assertTrue( (pack1.id, "{} /{}{}".format(pack1.brand, pack1.qt_paquet, pack1.unit)) in form.config_field('ind_pack_field','IND') ) self.assertTrue( (pack2.id, "{} /{}{}".format(pack2.brand, pack2.qt_paquet, pack2.unit)) in form.config_field('ind_pack_field','IND') ) self.assertTrue( (pack3.id, "{} /{}{}".format(pack3.brand, pack3.qt_paquet, pack3.unit)) in form.config_field('ind_pack_field','IND') ) self.assertEqual( form.initial['ind_pack_field'], (pack2.id, "{} /{}{}".format(pack2.brand, pack2.qt_paquet, pack2.unit)) ) pack3 = self.db_pack_rol self.assertEqual( form.config_field('rol_pack_field', 'ROL'), ((pack3.id, "{} /{}{}".format(pack3.brand, pack3.qt_paquet, pack3.unit)),) )
def test_clean_gt_today(self): """Function testing if form is invalide for days after today""" data = { 'date_smoke': datetime.date(2020, 6, 18), 'time_smoke': datetime.time(3, 21), 'type_cig_field': 'IND', 'ind_pack_field': self.db_pack_ind.id, 'rol_pack_field': self.db_pack_rol.id, 'nb_pack_field': self.db_pack_nb.id, 'given_field': False, } form = SmokeForm(self.usertest, -120, data) self.assertFalse(form.is_valid()) self.assertRaises(ValidationError) self.assertEqual( form.errors, { '__all__': [ "Vous ne pouvez pas enregistrer de craquage pour les jours à venir" ], })
def test_last_smoke_only_given(self): """ test get last paquet while user only smoked given cig """ ConsoCig.objects.create( user=self.usertest, datetime_cig=datetime.datetime(2020, 6, 16, 10, 15, tzinfo=pytz.utc), paquet=None, ) form = SmokeForm(self.usertest, -120) self.assertEqual(form.last_smoke, self.db_pack_nb)
def test_smoke_last_none_first_smoke(self): """ test last_smoke method with last smoke given=False smoke """ ConsoCig.objects.create( user=self.usertest, datetime_cig=datetime.datetime(2020, 6, 17, 13, 15, tzinfo=pytz.utc), paquet=self.db_pack_ind2, ) form = SmokeForm(self.usertest, -120) self.assertEqual(form.last_smoke, self.db_pack_ind2)
def test_smoke_last_none(self): """ test last_smoke method with last one given=True, before exists given=False """ ConsoCig.objects.create( user=self.usertest, datetime_cig=datetime.datetime(2020, 6, 16, 10, 15, tzinfo=pytz.utc), paquet=self.db_pack_nb, ) ConsoCig.objects.create( user=self.usertest, datetime_cig=datetime.datetime(2020, 6, 17, 10, 15, tzinfo=pytz.utc), paquet=self.db_pack_ind2, ) ConsoCig.objects.create( user=self.usertest, datetime_cig=datetime.datetime(2020, 6, 17, 13, 15, tzinfo=pytz.utc), paquet=None, ) ConsoCig.objects.create( user=self.usertest, datetime_cig=datetime.datetime(2020, 6, 17, 13, 15, tzinfo=pytz.utc), paquet=None, ) form = SmokeForm(self.usertest, -120, self.valid_smoking_datas) self.assertEqual(form.last_smoke, self.db_pack_ind2)
def test_last_smoke_first_smoke(self): """ test last_smoke method first user ConsoCig""" form = SmokeForm(self.usertest, self.valid_datas) self.assertEqual(form.last_smoke, self.db_pack_nb)
def test_SmokeForm_is_valid(self): """test valid SmokeForm""" form = SmokeForm(self.usertest, self.valid_datas) self.assertTrue(form.is_valid())