33 lines
1.3 KiB
Python
33 lines
1.3 KiB
Python
"""Expenses claim form."""
|
|
|
|
from django import forms
|
|
from string import ascii_uppercase
|
|
|
|
|
|
class ExpensesClaim(forms.Form):
|
|
"""Expenses claim form."""
|
|
|
|
name = forms.CharField(label='Nimi', max_length=100)
|
|
iban = forms.CharField(label='IBAN', max_length=100)
|
|
email = forms.EmailField(label='Sähköposti', max_length=100)
|
|
amount = forms.DecimalField(label='Yhteensä', decimal_places=2)
|
|
|
|
def clean_iban(self):
|
|
"""Validate IBAN."""
|
|
orig_iban = self.cleaned_data['iban']
|
|
# Remove spaces.
|
|
cleaned_iban = orig_iban.replace(' ', '')
|
|
# Move first 4 symbols to the end of the string.
|
|
cleaned_iban = cleaned_iban[4:] + cleaned_iban[0:4]
|
|
LETTERS = {letter: str(index) for index,
|
|
letter in enumerate(ascii_uppercase, start=10)}
|
|
cleaned_iban = cleaned_iban.upper()
|
|
# Replace all letters with numbers, so that A=10, B=11, ..., Z=35.
|
|
cleaned_iban = [LETTERS[char] if char in LETTERS
|
|
else char for char in cleaned_iban]
|
|
cleaned_iban = ''.join(cleaned_iban)
|
|
# If cleaned_iban modulo 97 != 1 the IBAN number is invalid.
|
|
if int(cleaned_iban) % 97 != 1:
|
|
raise forms.ValidationError('Invalid IBAN number!')
|
|
return orig_iban
|