Ticket #9066: cs_localflavor.patch

File cs_localflavor.patch, 4.9 KB (added by mathwizard, 16 years ago)

Patch containing initial implementation of Czech localflavor

  • django/contrib/localflavor/cs/cs_regions.py

     
     1#coding=utf-8
     2"""
     3An alphabetical list of regions for use as `choices`
     4in a formfield.
     5"""
     6
     7REGION_CHOICES = (
     8    (u'PHA', u'Praha'),
     9    (u'STC', u'Středočeský'),
     10    (u'JHC', u'Jihočeský'),
     11    (u'ULK', u'Ústecký'),
     12    (u'PLK', u'Plzeňský'),
     13    (u'MSK', u'Moravsko-Slezský'),
     14    (u'JHM', u'Jihomoravský'),
     15    (u'KVK', u'Karlovarský'),
     16    (u'LBK', u'Liberecký'),
     17    (u'HKK', u'Králové-Hradecký'),
     18    (u'PAK', u'Pardubický'),
     19    (u'VYS', u'Vysočina'),
     20    (u'ZLK', u'Zlínský'),
     21    (u'OLK', u'Olomoucký'),
     22)
     23 No newline at end of file
  • django/contrib/localflavor/cs/models.py

     
     1from django.conf import settings
     2from django.db.models.fields import Field
     3
     4class CSRegionField(Field):
     5    def get_internal_type(self):
     6        return "CSRegionField"
     7
     8    def db_type(self):
     9        if settings.DATABASE_ENGINE == 'oracle':
     10            return 'CHAR(3)'
     11        else:
     12            return 'varchar(3)'
     13
     14    def formfield(self, **kwargs):
     15        from django.contrib.localflavor.cs.forms import CSRegionSelect
     16        defaults = {'widget': CSRegionSelect}
     17        defaults.update(kwargs)
     18        return super(CSRegionField, self).formfield(**defaults)
     19
     20class CSPhoneNumberField(Field):
     21    def get_internal_type(self):
     22        return "CSPhoneNumberField"
     23
     24    def db_type(self):
     25        if settings.DATABASE_ENGINE == 'oracle':
     26            return 'VARCHAR2(11)'
     27        else:
     28            return 'varchar(11)'
     29
     30    def formfield(self, **kwargs):
     31        from django.contrib.localflavor.cs import forms
     32        defaults = {'form_class': forms.CSPhoneNumberField}
     33        defaults.update(kwargs)
     34        return super(CSPhoneNumberField, self).formfield(**defaults)
     35
     36class CSZipCodeField(Field):
     37    def get_internal_type(self):
     38        return "CSZipCodeField"
     39
     40    def db_type(self):
     41        if settings.DATABASE_ENGINE == 'oracle':
     42            return 'VARCHAR2(5)'
     43        else:
     44            return 'varchar(5)'
     45
     46    def formfield(self, **kwargs):
     47        from django.contrib.localflavor.cs import forms
     48        defaults = {'form_class': forms.CSZipCodeField}
     49        defaults.update(kwargs)
     50        return super(CSZipCodeField, self).formfield(**defaults)
  • django/contrib/localflavor/cs/forms.py

     
     1"""
     2Czech-specific Form helpers
     3"""
     4
     5from django.forms import ValidationError
     6from django.forms.fields import Field, RegexField, Select, EMPTY_VALUES
     7from django.forms.util import smart_unicode
     8from django.utils.translation import ugettext_lazy as _
     9import re
     10
     11phone_digits_re = re.compile(r'(\d{3})-?\s?(\d{3})-?\s?(\d{3})$')
     12
     13class CSZipCodeField(RegexField):
     14    """Czech postal code field."""
     15    default_error_messages = {
     16        'invalid': _(u'Enter a postal code in the format XXX XX.'),
     17    }
     18
     19    def __init__(self, *args, **kwargs):
     20        super(CSZipCodeField, self).__init__(r'\d{3}\s?\d{2}$',
     21            max_length=None, min_length=None, *args, **kwargs)
     22
     23class CSBirthNumberField(RegexField):
     24    """Czech Birth number field."""
     25    default_error_messages = {
     26        'invalid': _(u'Enter a social security number in the format XXXXXX/XXXX.'),
     27    }
     28
     29    def __init__(self, *args, **kwargs):
     30        super(CSBirthNumberField, self).__init__(r'\d{6}/\d{3,4}$',
     31            max_length=None, min_length=None, *args, **kwargs)
     32
     33class CSPhoneNumberField(Field):
     34    """Czech phone number field."""
     35    default_error_messages = {
     36        'invalid': _(u'Phone numbers must be in XXX-XXX-XXX format.'),
     37    }
     38
     39    def clean(self, value):
     40        """Validate a phone number.
     41        """
     42        super(CSPhoneNumberField, self).clean(value)
     43        if value in EMPTY_VALUES:
     44            return u''
     45        m = phone_digits_re.search(value)
     46        if m:
     47            return u'%s-%s-%s' % (m.group(1), m.group(2), m.group(3))
     48        raise ValidationError(self.error_messages['invalid'])
     49
     50class CSRegionSelect(Select):
     51    """
     52    A Select widget that uses a list of Czech regions as its choices.
     53    """
     54    def __init__(self, attrs=None):
     55        from cs_regions import REGION_CHOICES # relative import
     56        super(CSRegionSelect, self).__init__(attrs, choices=REGION_CHOICES)
Back to Top