示例#1
0
 def test_BillFormset_creation_invalid(self):
     print('Testing bills.forms.BillFormset() creation invalid')
     formset = BillFormset({
         'form-INITIAL_FORMS': '0',
         'form-TOTAL_FORMS': '2',
         'form-0-date': '',
         'form-0-type': '',
         'form-0-amount': -100,
         'form-0-file': None
     })
     self.assertFalse(formset.is_valid())
示例#2
0
def sandbox(request):
    if request.method == 'POST':
        print(request.POST)
        formset = BillFormset(request.POST)
        if formset.is_valid():
            for form in formset:
                print(form.cleaned_data)
        else:
            print('invalid')
    formset = BillFormset()
    return render(request, 'main/sandbox.html', {'formset': formset})
示例#3
0
 def test_BillFormset_creation_valid(self):
     print('Testing bills.forms.BillFormset() creation valid')
     formset = BillFormset({
         'form-INITIAL_FORMS': '0',
         'form-TOTAL_FORMS': '1',
         'form-0-date': '2019-11-04',
         'form-0-type': 'ELEC',
         'form-0-amount': 299.99,
         'form-0-file': None
     })
     self.assertTrue(formset.is_valid())
示例#4
0
def house_bill_add(request, pk):
    house = get_object_or_404(House, pk=pk)
    if request.user.id != house.user.id:
        raise Http404

    if request.method == 'POST':
        formset = BillFormset(request.POST, request.FILES)
        if formset.is_valid():
            for form in formset:
                if not check_format(form.cleaned_data['date']):
                    return render(request, 'houses/house_bill_add.html', {'error': 'You have entered the date in an incorrect format! Use yyyy-mm-dd', 'house': house, 'formset': formset})

                bill = Bill()
                bill.user = request.user
                bill.type = form.cleaned_data['type']
                bill.amount = form.cleaned_data['amount']
                bill.date = form.cleaned_data['date']

                if isinstance(form.cleaned_data['date'], datetime.date):
                    parsed_date = form.cleaned_data['date']
                else:
                    parsed_date = datetime.datetime.strptime(form.cleaned_data['date'], '%Y-%m-%d')

                month = parsed_date.month
                year = parsed_date.year

                existing_billset = BillSet.objects.filter(house=house).filter(year=year).filter(month=month)
                if existing_billset.count() == 0:
                    new_billset = BillSet()
                    new_billset.house = house
                    new_billset.month = month
                    new_billset.year = year
                    new_billset.save()
                    bill.set = new_billset
                else:
                    bill.set = existing_billset.first()
                bill.save()

                if form.cleaned_data['file'] is not None:
                    billfile = BillFile()
                    billfile.user = request.user
                    billfile.file = form.cleaned_data['file']
                    billfile.bill = bill
                    billfile.save()
                send_bill_email(house, bill)

            return redirect('house_detail', house.pk)
        else:
            return render(request, 'houses/house_bill_add.html', {'error': 'You have entered invalid bill data', 'house': house, 'formset': formset})
    else:
        formset = BillFormset()
        return render(request, 'houses/house_bill_add.html', {'house': house, 'formset': formset})