Skip to content

Commit

Permalink
Small refactor + improved UX in views.py (#192)
Browse files Browse the repository at this point in the history
Let the message and commits speak for themselves.
  • Loading branch information
christofergartner authored Jun 12, 2024
1 parent df2b7cc commit d222e6b
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions invoices/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,33 @@ def new_invoice(request):
if request.method == 'GET':
return render(request, 'invoices/new.html', {'budget_url': settings.BUDGET_URL})

invdate = request.POST['invoice-date'] if re.match('[0-9]{4}-[0-9]{2}-[0-9]{2}', request.POST['invoice-date']) else None
duedate = request.POST['invoice-due-date'] if re.match('[0-9]{4}-[0-9]{2}-[0-9]{2}', request.POST['invoice-due-date']) else None

# Beneficial to not return per check, because then several errors can be reported at once
valid = True

if len((request.FILES.getlist('files'))) < 1:
messages.error(request, 'Du måste ladda upp minst en fil med fakturan')
return HttpResponseRedirect(reverse('invoices-new'))

invdate = request.POST['invoice-date'] if re.match('[0-9]{4}-[0-9]{2}-[0-9]{2}', request.POST['invoice-date']) else None
duedate = request.POST['invoice-due-date'] if re.match('[0-9]{4}-[0-9]{2}-[0-9]{2}', request.POST['invoice-due-date']) else None
valid = False

if any(map(lambda x: float(x) <= 0, request.POST.getlist('amount[]'))) > 0:
messages.error(request, 'Du har angivit en icke-positiv summa i någon av fakturadelarna')
return HttpResponseRedirect(reverse('invoices-new'))
valid = False

if len(request.POST.getlist('amount[]')) != len(request.POST.getlist('budgetLine[]')):
messages.error(request, 'Sluta fippla')
return HttpResponseRedirect(reverse('invoices-new'))
valid = False

if len(request.POST.getlist('budgetLine[]')) == 0:
messages.error(request, 'Du måste lägga till minst en del på kvittot')
return HttpResponseRedirect(reverse('invoices-new'))
valid = False

if invdate > duedate:
messages.error(request, 'Fakturadatumet är efter förfallodatumet')
valid = False

if not valid:
return HttpResponseRedirect(reverse('invoices-new'))


Expand Down Expand Up @@ -89,7 +94,9 @@ def new_invoice(request):
@require_GET
@login_required
def invoice_new_confirmation(request, pk):
try: invoice = Invoice.objects.get(pk=int(pk))
try:
invoice = Invoice.objects.get(pk=int(pk))

except ObjectDoesNotExist:
messages.error(request, 'Ett fel uppstod och kvittot skapades inte.')
return HttpResponseRedirect(reverse('invoices-new'))
Expand Down

0 comments on commit d222e6b

Please sign in to comment.