def test_init_first_with_non_displayed_pack(self):
     """test PackManager init_first method with non displayed pack"""
     db_pack = Paquet.objects.create(
         user=self.usertest,
         type_cig='ROL',
         brand='TEST AUTRE',
         qt_paquet=50,
         price=30,
         display=False,
         first=False,
     )
     data = {
         'type_cig': 'ROL',
         'brand': 'TEST AUTRE',
         'qt_paquet': 50,
         'price': 30,
     }
     pack = PackManager(self.usertest, data)
     pack.init_first()
     pack.create_pack()
     filter_pack = Paquet.objects.filter(user=self.usertest,
                                         display=True,
                                         first=True)
     self.assertTrue(filter_pack.exists())
     self.assertEqual(filter_pack.count(), 1)
     self.assertEqual(filter_pack[0].id, db_pack.id)
Beispiel #2
0
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_init_first(self):
     """test PackManager init_first method"""
     data = {
         'type_cig': 'IND',
         'brand': 'Camel',
         'qt_paquet': 20,
         'price': 10,
     }
     pack = PackManager(self.usertest, data)
     pack.init_first()
     self.assertTrue(pack.first)
     self.assertEqual(
         Paquet.objects.filter(user=self.usertest, first=True).count(), 0)
     pack.create_pack()
     self.assertEqual(
         Paquet.objects.filter(user=self.usertest, first=True).count(), 1)