def new_parameters(request): """View changing user smoking habits when starting using app""" if request.method == 'POST': # make request.POST mutable data = dict(request.POST) for field in data: data[field] = data[field][0] # check wich form (existing packs or new pack) try: if data['ref_pack']: existing_pack = True except KeyError: existing_pack = False # if new pack, createpack with PaquetFormCreation if not existing_pack: paquet_form = PaquetFormCreation(request.user, data) if paquet_form.is_valid(): new_pack = PackManager(request.user, paquet_form.cleaned_data) # define new_pack as the first pack (ref to use for stats) new_pack.init_first() # create pack new_db_object = new_pack.create_pack() # include field ref_pack with new pack id in data for ParametersForm data['ref_pack'] = new_db_object.id # reset parameters parameter_form = ParametersForm(request.user, data) if parameter_form.is_valid(): reset = ProfileManager(request.user, parameter_form.cleaned_data) reset.new_profile() return redirect('QuitSoonApp:profile') raise Http404()
def test_form_post(self): """test ParametersForm""" data = { 'date_start': '2020-06-04', 'starting_nb_cig': '20', 'packs': str(self.pack.id) } form = ParametersForm(self.usertest, data) self.assertTrue(form.is_valid())
def test_form_post_missing_data(self): """test form with no data required fields""" form = ParametersForm(self.usertest, {}) self.assertEqual(form.errors, { 'date_start': ['Ce champ est obligatoire.'], 'starting_nb_cig': ['Ce champ est obligatoire.'], })
def profile(request): """User profile page with authentication infos and smoking habits""" context = { 'userprofile': None, 'user_packs': None, } parameter_form = ParametersForm(request.user, request.POST) paquet_form = PaquetFormCreation(request.user) context['parameter_form'] = parameter_form context['paquet_form'] = paquet_form try: userprofile = UserProfile.objects.get(user=request.user) except ObjectDoesNotExist: userprofile = None try: paquet_ref = Paquet.objects.get(user=request.user, first=True) except ObjectDoesNotExist: paquet_ref = None # if nor ref_pack, profile is incomplete userprofile = None # !!!!!!!!! to do!!!!!!!!!except pb more then one first? should not happened but in case if userprofile: context['userprofile'] = userprofile context['paquet_ref'] = paquet_ref packs = Paquet.objects.filter(user=request.user, display=True) if packs.exists: context['user_packs'] = packs return render(request, 'QuitSoonApp/profile.html', context)
def test_form_post_first_connection(self): """test ParametersForm while request.POST include newpack creation""" newpack = Paquet.objects.create( user=self.usertest, type_cig='ROL', brand='BRANDTEST', qt_paquet=50, price=30, first=True, ) data = { 'date_start': '2020-05-17', 'starting_nb_cig': 20, 'type_cig':'ROL', 'brand':'BRANDTEST', 'qt_paquet':'50', 'price':'30', 'ref_pack': newpack.id } form = ParametersForm(self.usertest, data) self.assertTrue(form.is_valid())
def test_form_get(self): """test get form with choices field""" form = ParametersForm(self.usertest) self.assertEqual(len(form.fields['ref_pack'].choices), 2) self.assertTrue((self.pack.id, 'CAMEL /20U') in form.fields['ref_pack'].choices) self.assertTrue((self.pack2.id, '1637 /30G') in form.fields['ref_pack'].choices)
def test_form(self): """test ParametersForm""" data = {'user':self.user, 'date_start':'2020-05-17', 'starting_nb_cig':'4'} form = ParametersForm(data=data) self.assertTrue(form.is_valid())