Fixed IBAN validation and amount field in exenses claim and added some tests
This commit is contained in:
+21
-34
@@ -1,43 +1,30 @@
|
||||
"""Expenses claim form."""
|
||||
|
||||
from django import forms
|
||||
|
||||
|
||||
class IBANField(forms.CharField):
|
||||
"""Field that validates Bank Account numbers
|
||||
acording to the IBAN standard."""
|
||||
|
||||
def to_python(self, data):
|
||||
"""1. Remove spaces
|
||||
2. Move the country code and two check digits to the end
|
||||
3. Replace all letters so that A=10, B=11 ... Z=35"""
|
||||
|
||||
# TODO: Field validation not working?
|
||||
if not data:
|
||||
return data
|
||||
print(data)
|
||||
data.replace(" ", "")
|
||||
data = data[4:] + data[0:4]
|
||||
LETTERS = {letter: str(index) for index,
|
||||
letter in enumerate(ascii_uppercase, start=10)}
|
||||
data = data.upper()
|
||||
data = [LETTERS[char] for char in data if char in LETTERS]
|
||||
print(data)
|
||||
return ''.join(data)
|
||||
|
||||
def validate(self, data):
|
||||
"""Validate value constructed by normalize"""
|
||||
|
||||
super().validate(data)
|
||||
if int(data) % 97 != 1:
|
||||
raise forms.ValidationError("Invalid IBAN number!")
|
||||
|
||||
return data
|
||||
from string import ascii_uppercase
|
||||
|
||||
|
||||
class ExpensesClaim(forms.Form):
|
||||
"""Expenses claim form"""
|
||||
|
||||
name = forms.CharField(label='Nimi', max_length=100)
|
||||
iban = IBANField(label='IBAN', max_length=100)
|
||||
amount = forms.CharField(label="Summa", max_length=100)
|
||||
iban = forms.CharField(label='IBAN', max_length=100)
|
||||
amount = forms.DecimalField(label="Summa", decimal_places=2)
|
||||
|
||||
def clean_iban(self):
|
||||
"""Validate IBAN."""
|
||||
data = self.cleaned_data['iban']
|
||||
# Remove spaces.
|
||||
data = data.replace(" ", "")
|
||||
# Move first 4 symbols to the end of the string.
|
||||
data = data[4:] + data[0:4]
|
||||
LETTERS = {letter: str(index) for index,
|
||||
letter in enumerate(ascii_uppercase, start=10)}
|
||||
data = data.upper()
|
||||
# Replace all letters with numbers, so that A=10, B=11, ..., Z=35.
|
||||
data = [LETTERS[char] if char in LETTERS else char for char in data]
|
||||
data = ''.join(data)
|
||||
# If data modulo 97 != 1 the IBAN number is invalid.
|
||||
if int(data) % 97 != 1:
|
||||
raise forms.ValidationError("Invalid IBAN number!")
|
||||
return data
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
Dis is claim
|
||||
|
||||
<form action="" method="get">
|
||||
<form action="" method="post">
|
||||
{{ form }}
|
||||
<input type="submit" value="Submit">
|
||||
</form>
|
||||
{% csrf_token %}
|
||||
</form>
|
||||
|
||||
+52
-1
@@ -1,3 +1,54 @@
|
||||
from django.test import TestCase
|
||||
from .forms import ExpensesClaim
|
||||
|
||||
# Create your tests here.
|
||||
|
||||
class ExpensesClaimTest(TestCase):
|
||||
"""Test expenses claim form."""
|
||||
|
||||
def test_valid_data1(self):
|
||||
form = ExpensesClaim({
|
||||
'name': "John Doe",
|
||||
'iban': "FI37 1590 3000 0007 76",
|
||||
'amount': 12.54
|
||||
})
|
||||
self.assertTrue(form.is_valid())
|
||||
|
||||
def test_valid_data2(self):
|
||||
form = ExpensesClaim({
|
||||
'name': "John Cena",
|
||||
'iban': "AL35202111090000000001234567",
|
||||
'amount': 12
|
||||
})
|
||||
self.assertTrue(form.is_valid())
|
||||
|
||||
def test_valid_data3(self):
|
||||
form = ExpensesClaim({
|
||||
'name': "John Wayne",
|
||||
'iban': "BR1500000000000010932840814P2",
|
||||
'amount': 12.0
|
||||
})
|
||||
self.assertTrue(form.is_valid())
|
||||
|
||||
def test_invalid_iban(self):
|
||||
form = ExpensesClaim({
|
||||
'name': "John Lennon",
|
||||
'iban': "FI3734 1590 3000 0007 76",
|
||||
'amount': 12.54
|
||||
})
|
||||
self.assertFalse(form.is_valid())
|
||||
|
||||
def test_invalid_amount(self):
|
||||
form = ExpensesClaim({
|
||||
'name': "John Kenedy",
|
||||
'iban': "FI37 1590 3000 0007 76",
|
||||
'amount': "asd"
|
||||
})
|
||||
self.assertFalse(form.is_valid())
|
||||
|
||||
def test_invalid_amount_decimal_places(self):
|
||||
form = ExpensesClaim({
|
||||
'name': "John Travolta",
|
||||
'iban': "FI37 1590 3000 0007 76",
|
||||
'amount': 12.544
|
||||
})
|
||||
self.assertFalse(form.is_valid())
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
|
||||
from django.shortcuts import render
|
||||
from django.views.decorators.http import require_http_methods
|
||||
from django.http import HttpResponse
|
||||
from .forms import ExpensesClaim
|
||||
|
||||
|
||||
# Allow only GET or POST
|
||||
@require_http_methods(["GET", "POST"])
|
||||
def claim(request):
|
||||
|
||||
Generated
+649
-649
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user