def test_create_new_alternative_already_in_db(self): """ test SavePack.create_alternative method if type_alternative == 'Su' and alternative already in db """ Alternative.objects.create( user=self.usertest, type_alternative='Su', substitut='P24', nicotine=2.0, display=False, ) data = { 'type_alternative': 'Su', 'substitut': 'P24', 'nicotine': 2, } alternative = AlternativeManager(self.usertest, data) alternative.create_alternative() db_create_alternative = Alternative.objects.filter( user=self.usertest, type_alternative='Su', substitut='P24', nicotine=2.0, ) self.assertFalse(db_create_alternative.count() == 2) self.assertEqual(db_create_alternative.count(), 1) self.assertEqual(db_create_alternative[0].display, True)
def test_create_new_alternative(self): """test SavePack.create_alternative method if type_alternative != 'Su'""" data = { 'type_alternative': 'Ac', 'type_activity': 'Sp', 'activity': 'COURSE', } alternative = AlternativeManager(self.usertest, data) alternative.create_alternative() db_create_alternative = Alternative.objects.filter( user=self.usertest, type_alternative='Ac', type_activity='Sp', activity='COURSE', ) self.assertTrue(db_create_alternative.exists())
def test_create_new_alternative_substitut(self): """test SavePack.create_alternative method if type_alternative == 'Su'""" data = { 'type_alternative': 'Su', 'substitut': 'P24', 'nicotine': 2, } alternative = AlternativeManager(self.usertest, data) alternative.create_alternative() db_create_alternative = Alternative.objects.filter( user=self.usertest, type_alternative='Su', substitut='P24', nicotine=2.0, ) self.assertTrue(db_create_alternative.exists())
def test_create_new_alternative_substitut_already_in_db(self): """test SavePack.create_alternative method if type_alternative == 'Su'""" Alternative.objects.create( user=self.usertest, type_alternative='Su', substitut='ECIG', nicotine=2.0, ) data = { 'type_alternative': 'Su', 'substitut': 'ECIG', 'nicotine': 2, } alternative = AlternativeManager(self.usertest, data) alternative.create_alternative() db_create_alternative = Alternative.objects.filter( user=self.usertest, type_alternative='Su', substitut='ECIG', nicotine=2.0, ) self.assertFalse(db_create_alternative.count() == 2)
def alternatives(request): """Healthy parameters, user different activities or substitutes""" context = {} alternative_form = TypeAlternativeForm(request.user) activity_form = ActivityForm(request.user) substitut_form = SubstitutForm(request.user) if request.method == 'POST': # get wich type of alternative form_data = {'type_alternative': request.POST['type_alternative']} alternative_form = TypeAlternativeForm(request.user, form_data) if alternative_form.is_valid(): final_data = { 'type_alternative': alternative_form.cleaned_data['type_alternative'] } if alternative_form.cleaned_data['type_alternative'] == 'Ac': form_data = { 'type_activity': request.POST['type_activity'], 'activity': request.POST['activity'] } activity_form = ActivityForm(request.user, form_data) if activity_form.is_valid(): # keep only fields from alternative_form & activity_form final_data['type_activity'] = activity_form.cleaned_data[ 'type_activity'] final_data['activity'] = activity_form.cleaned_data[ 'activity'] # Create a AlternativeManager object and create a new alternative new_alternative = AlternativeManager( request.user, final_data) new_alternative.create_alternative() alternative_form = TypeAlternativeForm(request.user) activity_form = ActivityForm(request.user) substitut_form = SubstitutForm(request.user) elif alternative_form.cleaned_data['type_alternative'] == 'Su': form_data = { 'substitut': request.POST['substitut'], 'nicotine': request.POST['nicotine'] } substitut_form = SubstitutForm(request.user, form_data) if substitut_form.is_valid(): # keep only fields from alternative_form & substitut_form final_data['substitut'] = substitut_form.cleaned_data[ 'substitut'] final_data['nicotine'] = substitut_form.cleaned_data[ 'nicotine'] # Create a AlternativeManager object and create a new alternative new_alternative = AlternativeManager( request.user, final_data) new_alternative.create_alternative() alternative_form = TypeAlternativeForm(request.user) activity_form = ActivityForm(request.user) substitut_form = SubstitutForm(request.user) # select users packs for display in paquets page displayed_alt = Alternative.objects.filter(user=request.user, display=True) context = { 'alternative_form': alternative_form, 'activity_form': activity_form, 'substitut_form': substitut_form, # get packs per type 'Sp': displayed_alt.filter(type_activity='Sp'), 'Lo': displayed_alt.filter(type_activity='Lo'), 'So': displayed_alt.filter(type_activity='So'), 'Su': displayed_alt.filter(type_alternative='Su'), } return render(request, 'QuitSoonApp/alternatives.html', context)