Ticket #8798: ticket8798-r8849.diff

File ticket8798-r8849.diff, 5.0 KB (added by Jannis Leidel, 16 years ago)

Added DEPhoneNumberField to German localflavor

  • django/contrib/localflavor/de/forms.py

    diff --git a/django/contrib/localflavor/de/forms.py b/django/contrib/localflavor/de/forms.py
    index 7a1b7c0..cdd64f5 100644
    a b DE-specific Form helpers  
    44
    55from django.forms import ValidationError
    66from django.forms.fields import Field, RegexField, Select, EMPTY_VALUES
     7from django.utils.encoding import smart_unicode
    78from django.utils.translation import ugettext_lazy as _
    89import re
    910
    1011id_re = re.compile(r"^(?P<residence>\d{10})(?P<origin>\w{1,3})[-\ ]?(?P<birthday>\d{7})[-\ ]?(?P<validity>\d{7})[-\ ]?(?P<checksum>\d{1})$")
     12phone_digits_re= re.compile(r"^(((\+\d{2,3})?\s\(([^0]{1}\d{1,6}\)|0\)\d{1,5})|\(\d{1,2}(\s?\d{1,2}){1,2}\))\s(\d{1,2}(\s?\d{1,2}){1,2})((-(\d{1,4}))?))|(\+\d{2,3}\s?\d{1,5}|0\d{1,6})\s?(\d\s)?\d+(-?\d{1,5})?$")
    1113
    1214class DEZipCodeField(RegexField):
    1315    default_error_messages = {
    class DEZipCodeField(RegexField):  
    1719        super(DEZipCodeField, self).__init__(r'^\d{5}$',
    1820            max_length=None, min_length=None, *args, **kwargs)
    1921
     22class DEPhoneNumberField(RegexField):
     23    """
     24    A form field that validates telephone numbers following several standards
     25    used in Austria and Germany (national and international):
     26
     27        * DIN 5008 (new)
     28        * DIN 5008 (old)
     29        * E.123
     30        * Informal convention
     31    """
     32    default_error_messages = {
     33        'invalid': _('Enter the phone number in a supported format: DIN 5008, E.123, Microsoft/TAPI.'),
     34    }
     35    def __init__(self, *args, **kwargs):
     36        super(DEPhoneNumberField, self).__init__(phone_digits_re, *args, **kwargs)
     37
     38    def clean(self, value):
     39        value = super(DEPhoneNumberField, self).clean(value)
     40        if value in EMPTY_VALUES:
     41            return u''
     42        return value
     43
    2044class DEStateSelect(Select):
    2145    """
    2246    A Select widget that uses a list of DE states as its choices.
  • docs/ref/contrib/localflavor.txt

    diff --git a/docs/ref/contrib/localflavor.txt b/docs/ref/contrib/localflavor.txt
    index 014ef71..9d50d31 100644
    a b Germany (``de``)  
    286286
    287287    A ``Select`` widget that uses a list of German states as its choices.
    288288
     289.. class:: de.forms.DEPhoneNumberField
     290
     291    A form field that validates input as a telephone number following
     292    `several standards`_ used in Austria and Germany (national and
     293    international notation):
     294
     295        * DIN 5008 (new)
     296        * DIN 5008 (old)
     297        * E.123
     298        * Informal
     299
     300.. _several standards: http://de.wikipedia.org/wiki/Rufnummer#Schreibweisen
     301
    289302Holland (``nl``)
    290303================
    291304
  • tests/regressiontests/forms/localflavor/de.py

    diff --git a/tests/regressiontests/forms/localflavor/de.py b/tests/regressiontests/forms/localflavor/de.py
    index e02323f..3d290df 100644
    a b Traceback (most recent call last):  
    1313...
    1414ValidationError: [u'Enter a zip code in the format XXXXX.']
    1515
     16# DEPhoneDIN5008Field #############################################################
     17
     18>>> from django.contrib.localflavor.de.forms import DEPhoneNumberField
     19>>> f = DEPhoneNumberField()
     20>>> f.clean('030 12345-67') # new DIN 5008
     21u'030 12345-67'
     22>>> f.clean('030 1234567') # new DIN 5008
     23u'030 1234567'
     24>>> f.clean('0301234567') # lazy DIN 5008
     25u'0301234567'
     26>>> f.clean('(0 30) 1 23 45-67') # old DIN 5008
     27u'(0 30) 1 23 45-67'
     28>>> f.clean('(0 30) 1 23 456') # old DIN 5008
     29u'(0 30) 1 23 456'
     30>>> f.clean('0900 5 123456') # DIN 5008 Premium rate numbers
     31u'0900 5 123456'
     32>>> f.clean('(030) 123 45 67') # E.123
     33u'(030) 123 45 67'
     34>>> f.clean('+49 (30) 1234567') # Microsoft/TAPI
     35u'+49 (30) 1234567'
     36>>> f.clean('+49 (0)30 12345-67') # Informal standard in Germany and Austria
     37u'+49 (0)30 12345-67'
     38>>> f.clean('+49 30 12345-67') # DIN 5008
     39u'+49 30 12345-67'
     40>>> f.clean('+49 30 1234567') # E.123
     41u'+49 30 1234567'
     42>>> f.clean('+49 30 12345-67') # E.123
     43u'+49 30 12345-67'
     44>>> f.clean('+49301234567') # Lazy E.123
     45u'+49301234567'
     46>>> f.clean('+49 30 12345--67')
     47Traceback (most recent call last):
     48...
     49ValidationError: [u'Enter the phone number in a supported format: DIN 5008, E.123, Microsoft/TAPI.']
     50>>> f.clean('+49 (030) 1234567')
     51Traceback (most recent call last):
     52...
     53ValidationError: [u'Enter the phone number in a supported format: DIN 5008, E.123, Microsoft/TAPI.']
     54>>> f.clean('(030) (123) 45 67')
     55Traceback (most recent call last):
     56...
     57ValidationError: [u'Enter the phone number in a supported format: DIN 5008, E.123, Microsoft/TAPI.']
     58>>> f.clean('abcdefg')
     59Traceback (most recent call last):
     60ValidationError: [u'Enter the phone number in a supported format: DIN 5008, E.123, Microsoft/TAPI.']
     61>>> f.clean(None)
     62Traceback (most recent call last):
     63...
     64ValidationError: [u'This field is required.']
     65>>> f = DEPhoneNumberField(required=False)
     66>>> f.clean(None)
     67u''
     68>>> f.clean('')
     69u''
     70
     71
    1672# DEStateSelect #############################################################
    1773
    1874>>> from django.contrib.localflavor.de.forms import DEStateSelect
Back to Top