VAT field with a field to select the country and a free field for the code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | from django import forms
from django.forms.widgets import HiddenInput
import re
EMPTY_VALUES = (None, '')
VAT_CHOICES = (
('','----------'),
('AT','AT-Austria'),
('BE','BE-Belgium'),
('BG','BG-Bulgaria'),
('CY','CY-Cyprus'),
('CZ','CZ-Czech Republic'),
('DE','DE-Germany'),
('DK','DK-Denmark'),
('EE','EE-Estonia'),
('EL','EL-Greece'),
('ES','ES-Spain'),
('FI','FI-Finland'),
('FR','FR-France '),
('GB','GB-United Kingdom'),
('HU','HU-Hungary'),
('IE','IE-Ireland'),
('IT','IT-Italy'),
('LT','LT-Lithuania'),
('LU','LU-Luxembourg'),
('LV','LV-Latvia'),
('MT','MT-Malta'),
('NL','NL-The Netherlands'),
('PL','PL-Poland'),
('PT','PT-Portugal'),
('RO','RO-Romania'),
('SE','SE-Sweden'),
('SI','SI-Slovenia'),
('SK','SK-Slovakia'),
)
class VatWidget(forms.MultiWidget):
"""docstring for VatWidget"""
def __init__(self,choices=VAT_CHOICES, attrs=None):
widgets = (
forms.Select(choices=choices),
forms.TextInput()
)
super(VatWidget, self).__init__(widgets, attrs)
def value_from_datadict(self, data, files, name):
value = [widget.value_from_datadict(data, files, name + '_%s' % i) for i, widget in enumerate(self.widgets)]
try:
country, code = value
#the spaces and the dots are removed
code=code.replace(".","").replace(" ","")
except:
return data.get(name, None)
if code not in EMPTY_VALUES:
if country in EMPTY_VALUES:
try:
# ex. code="FR09443710785", country="".
empty, country, code = re.split('([a-zA-Z]+)',code )
except:
return ['', code]
else:
#ex. code ="FR09443710785", country="FR".
re_code = re.compile(r'^%s(\d+)$' % country)
if re_code.match(code):
code = code.replace(country,"",1)
try: country = country.upper()
except:pass
return [country, code]
def format_output(self, rendered_widgets):
return "%s %s" % (rendered_widgets[0], rendered_widgets[1])
def decompress(self, value):
if value:
try:country, code = value
except:
country = None
code = value
if country in EMPTY_VALUES:
try:empty, country, code = re.split('([a-zA-Z]+)', code)
except:pass
return [country, code]
return [None, None]
class VatHiddenWidget(VatWidget):
"""
A Widget that splits vat input into two <input type="hidden"> inputs.
"""
def __init__(self, attrs=None):
widgets = (HiddenInput(attrs=attrs), HiddenInput(attrs=attrs))
super(VatWidget, self).__init__(widgets, attrs)
class VatField(forms.MultiValueField):
"""docstring for VatField"""
hidden_widget = VatHiddenWidget
def __init__(self, choices=VAT_CHOICES, *args, **kwargs):
# Set 'required' to False on the individual fields, because the
# required validation will be handled by MultiValueField, not by those
# individual fields.
fields = (
forms.ChoiceField(required=True, choices=choices),
forms.CharField(required=True),
)
for f in fields:
f.required = False
widget = VatWidget(choices=choices)
super(VatField, self).__init__(widget=widget, fields=fields, *args, **kwargs)
def compress(self, data_list):
if data_list:
return "".join(data_list)
return None
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Please login first before commenting.