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 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 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_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 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)