def test_form_save_does_not_add_new_payment_when_given_instance(self): users = [ User.objects.create_user( username='******'.format(str(i))) for i in range(3) ] group = Group.objects.create(name='Test Group') group.user_set.add(*users) payment = Payment.objects.create( date=datetime.strptime('02 February 2011', '%d %B %Y'), amount='99.99', description='bill', who_paid=users[0], ) payment.who_for.add(*users) form = NewPaymentForm(group_id=group.id, instance=payment, data={ 'icon': 'person_pin', 'date': datetime.strptime('11 March 2014', '%d %B %Y'), 'amount': '22.50', 'description': 'New Info', 'who_paid': 3, 'who_for': [2, 3], }) self.assertTrue(form.is_valid()) form.save() self.assertEqual(Payment.objects.count(), 1)
def test_form_save_adds_payment_to_group(self): users = [ User.objects.create_user( username='******'.format(str(i)), email='user{}@email.com'.format(str(i))) for i in range(3) ] group = Group.objects.create(name='Test Group') group.user_set.add(*users) form = NewPaymentForm(group_id=group.id, data={ 'icon': 'person_pin', 'date': datetime.strptime('02 February 2011', '%d %B %Y'), 'amount': '99.99', 'description': 'bill', 'who_paid': 1, 'who_for': [1, 2], }) form.save() payment = Payment.objects.first() self.assertEqual(payment.group, group)
def edit_payment(request, group_id, payment_id): group = confirm_user_in_group(request.user, group_id) if not group: raise Http404 try: payment = Payment.objects.get(id=payment_id) except ObjectDoesNotExist: raise Http404 form = None if request.method == 'POST': who_for = [] if request.POST.get('who_for'): who_for = [user_id for user_id in request.POST.getlist('who_for')] form = NewPaymentForm(group_id=group_id, instance=payment, data={ 'icon': request.POST.get('icon'), 'date': datetime.strptime( request.POST.get('date'), '%d %B, %Y'), 'amount': request.POST.get('amount'), 'description': request.POST.get('description'), 'who_paid': request.POST.get('who_paid'), 'who_for': who_for }) if form.is_valid(): payment = form.save() Edit.objects.create( group=group, user=request.user, payment=payment, change='edit' ) return redirect('view_group', group_id) if not form: form = NewPaymentForm(group_id=group_id, instance=payment) delete_form = DeletePaymentForm(group_id=group_id, initial={'payment': payment}) restore_form = RestorePaymentForm(group_id=group_id, initial={'payment': payment}) context = { 'group_id': group_id, 'form': form, 'delete_form': delete_form, 'restore_form': restore_form, 'active': payment.is_active } return render(request, 'quits/edit_payment.html', context)
def new_payment(request, group_id): group = confirm_user_in_group(request.user, group_id) if not group: raise Http404 form = None if request.method == 'POST': who_for = [] if request.POST.get('who_for'): who_for = [user_id for user_id in request.POST.getlist('who_for')] try: date = datetime.strptime(request.POST.get('date'), '%d %B, %Y') except ValueError: date = datetime.today() form = NewPaymentForm(group_id=group_id, data={ 'icon': request.POST.get('icon'), 'date': date, 'amount': request.POST.get('amount'), 'description': request.POST.get('description'), 'who_paid': request.POST.get('who_paid'), 'who_for': who_for }) if form.is_valid(): payment = form.save() Edit.objects.create( group=group, user=request.user, payment=payment, change='create' ) return redirect('view_group', group_id) if not form: form = NewPaymentForm(group_id=group_id, initial={ 'who_paid': request.user, 'who_for': group.user_set.all(), }) context = {'group_id': group_id, 'form': form} return render(request, 'quits/new_payment.html', context)
def test_form_only_renders_who_paid_and_who_for_options_for_group(self): owner = User.objects.create_user(username='******', email='*****@*****.**') member = User.objects.create_user(username='******', email='*****@*****.**') not_member = User.objects.create_user(username='******', email='*****@*****.**') group = Group.objects.create(name='Members only') group.user_set.add(owner, member) form = NewPaymentForm(group_id=group.id) self.assertIn('*****@*****.**', form.as_p()) self.assertEqual(form.as_p().count('*****@*****.**'), 2) self.assertIn('*****@*****.**', form.as_p()) self.assertEqual(form.as_p().count('*****@*****.**'), 2) self.assertNotIn('*****@*****.**', form.as_p())
def test_form_save_changes_attributes_for_existing_payment(self): users = [ User.objects.create_user( username='******'.format(str(i))) for i in range(3) ] group = Group.objects.create(name='Test Group') group.user_set.add(*users) payment = Payment.objects.create( date=datetime.strptime('02 February 2011', '%d %B %Y'), amount='99.99', description='bill', who_paid=users[0], ) payment.who_for.add(*users) form = NewPaymentForm(group_id=group.id, instance=payment, data={ 'icon': 'person_pin', 'date': datetime.strptime('11 March 2014', '%d %B %Y'), 'amount': '22.50', 'description': 'New Info', 'who_paid': 3, 'who_for': [2, 3], }) self.assertTrue(form.is_valid()) form.save() self.assertEqual(payment.date, datetime.strptime('11 March 2014', '%d %B %Y').date()) self.assertEqual(payment.amount, 22.50) self.assertEqual(payment.description, 'New Info') self.assertEqual(payment.who_paid, users[2]) self.assertListEqual(list(payment.who_for.all()), [users[1], users[2]])
def setUp(self): group = Group.objects.create(name='Test') self.form = NewPaymentForm(group_id=group.id)