Ticket #15013: 15013.ru-localflavor.diff

File 15013.ru-localflavor.diff, 16.8 KB (added by julien, 4 years ago)
  • new file django/contrib/localflavor/ru/forms.py

    diff --git a/django/contrib/localflavor/ru/__init__.py b/django/contrib/localflavor/ru/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/django/contrib/localflavor/ru/forms.py b/django/contrib/localflavor/ru/forms.py
    new file mode 100644
    index 0000000..d0d8a5e
    - +  
     1"""
     2Russian-specific forms helpers
     3"""
     4from django.core.validators import EMPTY_VALUES
     5from django.forms import ValidationError
     6from django.forms.fields import CharField, Select, RegexField
     7from django.utils.translation import ugettext_lazy as _
     8
     9import re
     10
     11phone_digits_re = re.compile(r'^(?:[78]-?)?(\d{3})[-\.]?(\d{3})[-\.]?(\d{4})$')
     12
     13class RUCountySelect(Select):
     14    """
     15    A Select widget that uses a list of Russian Counties as its choices.
     16    """
     17    def __init__(self, attrs=None):
     18        from ru_regions import RU_COUNTY_CHOICES
     19        super(RUCountySelect, self).__init__(attrs, choices=RU_COUNTY_CHOICES)
     20
     21class RURegionSelect(Select):
     22    """
     23    A Select widget that uses a list of Russian Regions as its choices.
     24    """
     25    def __init__(self, attrs=None):
     26        from ru_regions import RU_REGIONS_CHOICES
     27        super(RURegionSelect, self).__init__(attrs, choices=RU_REGIONS_CHOICES)
     28
     29class RUPostalCodeField(RegexField):
     30    """
     31    Russian Postal code field.
     32    Format: XXXXXX, where X is any digit, and first digit is not zero.
     33    """
     34    default_error_messages = {
     35        'invalid': _(u'Enter a postal code in the format XXXXXX.'),
     36    }
     37    def __init__(self, *args, **kwargs):
     38        super(RUPostalCodeField, self).__init__(r'^\d{6}$',
     39            max_length=None, min_length=None, *args, **kwargs)
     40
     41class RUPassportNumberField(RegexField):
     42    """
     43    Russian internal passport number format:
     44    XXXX XXXXXX where X - any digit.
     45    """
     46    default_error_messages = {
     47        'invalid': _(u'Enter a passport number in the format XXXX XXXXXX.'),
     48    }
     49    def __init__(self, *args, **kwargs):
     50        super(RUPassportNumberField, self).__init__(r'^\d{4} \d{6}$',
     51            max_length=None, min_length=None, *args, **kwargs)
     52
     53class RUAlienPassportNumberField(RegexField):
     54    """
     55    Russian alien's passport number format:
     56    XX XXXXXXX where X - any digit.
     57    """
     58    default_error_messages = {
     59        'invalid': _(u'Enter a passport number in the format XX XXXXXXX.'),
     60    }
     61    def __init__(self, *args, **kwargs):
     62        super(RUAlienPassportNumberField, self).__init__(r'^\d{2} \d{7}$',
     63            max_length=None, min_length=None, *args, **kwargs)
  • new file django/contrib/localflavor/ru/ru_regions.py

    diff --git a/django/contrib/localflavor/ru/ru_regions.py b/django/contrib/localflavor/ru/ru_regions.py
    new file mode 100644
    index 0000000..dd2150f
    - +  
     1"""
     2Sources:
     3    http://ru.wikipedia.org/wiki/%D0%9A%D0%BE%D0%B4%D1%8B_%D1%81%D1%83%D0%B1%D1%8A%D0%B5%D0%BA%D1%82%D0%BE%D0%B2_%D0%A0%D0%BE%D1%81%D1%81%D0%B8%D0%B9%D1%81%D0%BA%D0%BE%D0%B9_%D0%A4%D0%B5%D0%B4%D0%B5%D1%80%D0%B0%D1%86%D0%B8%D0%B8
     4    http://ru.wikipedia.org/wiki/%D0%A4%D0%B5%D0%B4%D0%B5%D1%80%D0%B0%D0%BB%D1%8C%D0%BD%D1%8B%D0%B5_%D0%BE%D0%BA%D1%80%D1%83%D0%B3%D0%B0_%D0%A0%D0%BE%D1%81%D1%81%D0%B8%D0%B9%D1%81%D0%BA%D0%BE%D0%B9_%D0%A4%D0%B5%D0%B4%D0%B5%D1%80%D0%B0%D1%86%D0%B8%D0%B8
     5"""
     6from django.utils.translation import ugettext_lazy as _
     7
     8RU_COUNTY_CHOICES = (
     9    ("Central Federal County", _("Central Federal County")),
     10    ("South Federal County", _("South Federal County")),
     11    ("North-West Federal County", _("North-West Federal County")),
     12    ("Far-East Federal County", _("Far-East Federal County")),
     13    ("Siberian Federal County", _("Siberian Federal County")),
     14    ("Ural Federal County", _("Ural Federal County")),
     15    ("Privolzhsky Federal County", _("Privolzhsky Federal County")),
     16    ("North-Caucasian Federal County", _("North-Caucasian Federal County"))
     17)
     18
     19RU_REGIONS_CHOICES = (
     20    ("77", _("Moskva")),
     21    ("78", _("Saint-Peterburg")),
     22    ("50", _("Moskovskaya oblast'")),
     23    ("01", _("Adygeya, Respublika")),
     24    ("02", _("Bashkortostan, Respublika")),
     25    ("03", _("Buryatia, Respublika")),
     26    ("04", _("Altay, Respublika")),
     27    ("05", _("Dagestan, Respublika")),
     28    ("06", _("Ingushskaya Respublika")),
     29    ("07", _("Kabardino-Balkarskaya Respublika")),
     30    ("08", _("Kalmykia, Respublika")),
     31    ("09", _("Karachaevo-Cherkesskaya Respublika")),
     32    ("10", _("Karelia, Respublika")),
     33    ("11", _("Komi, Respublika")),
     34    ("12", _("Mariy Ehl, Respublika")),
     35    ("13", _("Mordovia, Respublika")),
     36    ("14", _("Sakha, Respublika (Yakutiya)")),
     37    ("15", _("Severnaya Osetia, Respublika (Alania)")),
     38    ("16", _("Tatarstan, Respublika")),
     39    ("17", _("Tyva, Respublika (Tuva)")),
     40    ("18", _("Udmurtskaya Respublika")),
     41    ("19", _("Khakassiya, Respublika")),
     42    ("95", _("Chechenskaya Respublika")),
     43    ("21", _("Chuvashskaya Respublika")),
     44    ("22", _("Altayskiy Kray")),
     45    ("80", _("Zabaykalskiy Kray")),
     46    ("82", _("Kamchatskiy Kray")),
     47    ("23", _("Krasnodarskiy Kray")),
     48    ("24", _("Krasnoyarskiy Kray")),
     49    ("81", _("Permskiy Kray")),
     50    ("25", _("Primorskiy Kray")),
     51    ("26", _("Stavropol'siyy Kray")),
     52    ("27", _("Khabarovskiy Kray")),
     53    ("28", _("Amurskaya oblast'")),
     54    ("29", _("Arkhangel'skaya oblast'")),
     55    ("30", _("Astrakhanskaya oblast'")),
     56    ("31", _("Belgorodskaya oblast'")),
     57    ("32", _("Bryanskaya oblast'")),
     58    ("33", _("Vladimirskaya oblast'")),
     59    ("34", _("Volgogradskaya oblast'")),
     60    ("35", _("Vologodskaya oblast'")),
     61    ("36", _("Voronezhskaya oblast'")),
     62    ("37", _("Ivanovskaya oblast'")),
     63    ("38", _("Irkutskaya oblast'")),
     64    ("39", _("Kaliningradskaya oblast'")),
     65    ("40", _("Kaluzhskaya oblast'")),
     66    ("42", _("Kemerovskaya oblast'")),
     67    ("43", _("Kirovskaya oblast'")),
     68    ("44", _("Kostromskaya oblast'")),
     69    ("45", _("Kurganskaya oblast'")),
     70    ("46", _("Kurskaya oblast'")),
     71    ("47", _("Leningradskaya oblast'")),
     72    ("48", _("Lipeckaya oblast'")),
     73    ("49", _("Magadanskaya oblast'")),
     74    ("51", _("Murmanskaya oblast'")),
     75    ("52", _("Nizhegorodskaja oblast'")),
     76    ("53", _("Novgorodskaya oblast'")),
     77    ("54", _("Novosibirskaya oblast'")),
     78    ("55", _("Omskaya oblast'")),
     79    ("56", _("Orenburgskaya oblast'")),
     80    ("57", _("Orlovskaya oblast'")),
     81    ("58", _("Penzenskaya oblast'")),
     82    ("60", _("Pskovskaya oblast'")),
     83    ("61", _("Rostovskaya oblast'")),
     84    ("62", _("Rjazanskaya oblast'")),
     85    ("63", _("Samarskaya oblast'")),
     86    ("64", _("Saratovskaya oblast'")),
     87    ("65", _("Sakhalinskaya oblast'")),
     88    ("66", _("Sverdlovskaya oblast'")),
     89    ("67", _("Smolenskaya oblast'")),
     90    ("68", _("Tambovskaya oblast'")),
     91    ("69", _("Tverskaya oblast'")),
     92    ("70", _("Tomskaya oblast'")),
     93    ("71", _("Tul'skaya oblast'")),
     94    ("72", _("Tyumenskaya oblast'")),
     95    ("73", _("Ul'ianovskaya oblast'")),
     96    ("74", _("Chelyabinskaya oblast'")),
     97    ("76", _("Yaroslavskaya oblast'")),
     98    ("79", _("Evreyskaya avtonomnaja oblast'")),
     99    ("83", _("Neneckiy autonomnyy okrug")),
     100    ("86", _("Khanty-Mansiyskiy avtonomnyy okrug - Yugra")),
     101    ("87", _("Chukotskiy avtonomnyy okrug")),
     102    ("89", _("Yamalo-Neneckiy avtonomnyy okrug"))
     103)
     104
  • docs/ref/contrib/localflavor.txt

    diff --git a/docs/ref/contrib/localflavor.txt b/docs/ref/contrib/localflavor.txt
    index f54341e..4571aa7 100644
    a b Countries currently supported by :mod:`~django.contrib.localflavor` are: 
    6262    * Poland_
    6363    * Portugal_
    6464    * Romania_
     65    * Russia_
    6566    * Slovakia_
    6667    * `South Africa`_
    6768    * Spain_
    Here's an example of how to use them:: 
    111112.. _Poland: `Poland (pl)`_
    112113.. _Portugal: `Portugal (pt)`_
    113114.. _Romania: `Romania (ro)`_
     115.. _Russia: `Russia (ru)`_
    114116.. _Slovakia: `Slovakia (sk)`_
    115117.. _South Africa: `South Africa (za)`_
    116118.. _Spain: `Spain (es)`_
    Romania (``ro``) 
    710712
    711713    A form field that validates Romanian postal codes.
    712714
     715Russia (``ru``)
     716===============
     717
     718.. class:: ru.forms.RUPostalCodeField
     719
     720    Russian Postal code field. The valid format is XXXXXX, where X is any
     721    digit and the first digit is not zero.
     722
     723.. class:: ru.forms.RUCountySelect
     724
     725    A ``Select`` widget that uses a list of Russian Counties as its choices.
     726
     727.. class:: ru.forms.RURegionSelect
     728
     729    A ``Select`` widget that uses a list of Russian Regions as its choices.
     730
     731.. class:: ru.forms.RUPassportNumberField
     732
     733    Russian internal passport number. The valid format is XXXX XXXXXX, where X
     734    is any digit.
     735
     736.. class:: ru.forms.RUAlienPassportNumberField
     737
     738    Russian alien's passport number. The valid format is XX XXXXXXX, where X
     739    is any digit.
     740
    713741Slovakia (``sk``)
    714742=================
    715743
  • new file tests/regressiontests/forms/localflavor/ru.py

    diff --git a/tests/regressiontests/forms/localflavor/ru.py b/tests/regressiontests/forms/localflavor/ru.py
    new file mode 100644
    index 0000000..c8feba6
    - +  
     1from django.contrib.localflavor.ru.forms import *
     2 
     3from utils import LocalFlavorTestCase
     4
     5
     6class RULocalFlavorTests(LocalFlavorTestCase):
     7
     8    def test_RUPassportNumberField(self):
     9        error = [u'Enter a passport number in the format XXXX XXXXXX.']
     10        valid = {
     11            '1981 211204': '1981 211204',
     12            '0305 967876': '0305 967876',
     13        }
     14        invalid = {
     15            '1981 2112044': error,
     16            '1981 23220': error,
     17            '9981211201': error,
     18        }
     19        self.assertFieldOutput(RUPassportNumberField, valid, invalid)
     20
     21    def test_RUAlienPassportNumberField(self):
     22        error = [u'Enter a passport number in the format XX XXXXXXX.']
     23        valid = {
     24            '19 8111204': '19 8111204',
     25            '03 0567876': '03 0567876',
     26        }
     27        invalid = {
     28            '198 1112044': error,
     29            '19 81123220': error,
     30            '99 812112': error,
     31        }
     32        self.assertFieldOutput(RUAlienPassportNumberField, valid, invalid)
     33
     34    def test_RUPostalCodeField(self):
     35        error = [u'Enter a postal code in the format XXXXXX.']
     36        valid = {
     37            '987654': '987654',
     38            '123456': '123456'
     39        }
     40        invalid = {
     41            '123 34': error,
     42            '1234567': error,
     43            '12345': error
     44        }
     45        self.assertFieldOutput(RUPostalCodeField, valid, invalid)
     46
     47    def test_RUCountySelect(self):
     48        f = RUCountySelect()
     49        out = u'''<select name="county">
     50<option value="Central Federal County">Central Federal County</option>
     51<option value="South Federal County">South Federal County</option>
     52<option value="North-West Federal County">North-West Federal County</option>
     53<option value="Far-East Federal County">Far-East Federal County</option>
     54<option value="Siberian Federal County">Siberian Federal County</option>
     55<option value="Ural Federal County">Ural Federal County</option>
     56<option value="Privolzhsky Federal County">Privolzhsky Federal County</option>
     57<option value="North-Caucasian Federal County">North-Caucasian Federal County</option>
     58</select>'''
     59        self.assertEqual(f.render('county', None), out)
     60
     61    def test_RURegionSelect(self):
     62        f = RURegionSelect()
     63        out = u'''<select name="region">
     64<option value="77">Moskva</option>
     65<option value="78">Saint-Peterburg</option>
     66<option value="50">Moskovskaya oblast&#39;</option>
     67<option value="01">Adygeya, Respublika</option>
     68<option value="02">Bashkortostan, Respublika</option>
     69<option value="03">Buryatia, Respublika</option>
     70<option value="04">Altay, Respublika</option>
     71<option value="05">Dagestan, Respublika</option>
     72<option value="06">Ingushskaya Respublika</option>
     73<option value="07">Kabardino-Balkarskaya Respublika</option>
     74<option value="08">Kalmykia, Respublika</option>
     75<option value="09">Karachaevo-Cherkesskaya Respublika</option>
     76<option value="10">Karelia, Respublika</option>
     77<option value="11">Komi, Respublika</option>
     78<option value="12">Mariy Ehl, Respublika</option>
     79<option value="13">Mordovia, Respublika</option>
     80<option value="14">Sakha, Respublika (Yakutiya)</option>
     81<option value="15">Severnaya Osetia, Respublika (Alania)</option>
     82<option value="16">Tatarstan, Respublika</option>
     83<option value="17">Tyva, Respublika (Tuva)</option>
     84<option value="18">Udmurtskaya Respublika</option>
     85<option value="19">Khakassiya, Respublika</option>
     86<option value="95">Chechenskaya Respublika</option>
     87<option value="21">Chuvashskaya Respublika</option>
     88<option value="22">Altayskiy Kray</option>
     89<option value="80">Zabaykalskiy Kray</option>
     90<option value="82">Kamchatskiy Kray</option>
     91<option value="23">Krasnodarskiy Kray</option>
     92<option value="24">Krasnoyarskiy Kray</option>
     93<option value="81">Permskiy Kray</option>
     94<option value="25">Primorskiy Kray</option>
     95<option value="26">Stavropol&#39;siyy Kray</option>
     96<option value="27">Khabarovskiy Kray</option>
     97<option value="28">Amurskaya oblast&#39;</option>
     98<option value="29">Arkhangel&#39;skaya oblast&#39;</option>
     99<option value="30">Astrakhanskaya oblast&#39;</option>
     100<option value="31">Belgorodskaya oblast&#39;</option>
     101<option value="32">Bryanskaya oblast&#39;</option>
     102<option value="33">Vladimirskaya oblast&#39;</option>
     103<option value="34">Volgogradskaya oblast&#39;</option>
     104<option value="35">Vologodskaya oblast&#39;</option>
     105<option value="36">Voronezhskaya oblast&#39;</option>
     106<option value="37">Ivanovskaya oblast&#39;</option>
     107<option value="38">Irkutskaya oblast&#39;</option>
     108<option value="39">Kaliningradskaya oblast&#39;</option>
     109<option value="40">Kaluzhskaya oblast&#39;</option>
     110<option value="42">Kemerovskaya oblast&#39;</option>
     111<option value="43">Kirovskaya oblast&#39;</option>
     112<option value="44">Kostromskaya oblast&#39;</option>
     113<option value="45">Kurganskaya oblast&#39;</option>
     114<option value="46">Kurskaya oblast&#39;</option>
     115<option value="47">Leningradskaya oblast&#39;</option>
     116<option value="48">Lipeckaya oblast&#39;</option>
     117<option value="49">Magadanskaya oblast&#39;</option>
     118<option value="51">Murmanskaya oblast&#39;</option>
     119<option value="52">Nizhegorodskaja oblast&#39;</option>
     120<option value="53">Novgorodskaya oblast&#39;</option>
     121<option value="54">Novosibirskaya oblast&#39;</option>
     122<option value="55">Omskaya oblast&#39;</option>
     123<option value="56">Orenburgskaya oblast&#39;</option>
     124<option value="57">Orlovskaya oblast&#39;</option>
     125<option value="58">Penzenskaya oblast&#39;</option>
     126<option value="60">Pskovskaya oblast&#39;</option>
     127<option value="61">Rostovskaya oblast&#39;</option>
     128<option value="62">Rjazanskaya oblast&#39;</option>
     129<option value="63">Samarskaya oblast&#39;</option>
     130<option value="64">Saratovskaya oblast&#39;</option>
     131<option value="65">Sakhalinskaya oblast&#39;</option>
     132<option value="66">Sverdlovskaya oblast&#39;</option>
     133<option value="67" selected="selected">Smolenskaya oblast&#39;</option>
     134<option value="68">Tambovskaya oblast&#39;</option>
     135<option value="69">Tverskaya oblast&#39;</option>
     136<option value="70">Tomskaya oblast&#39;</option>
     137<option value="71">Tul&#39;skaya oblast&#39;</option>
     138<option value="72">Tyumenskaya oblast&#39;</option>
     139<option value="73">Ul&#39;ianovskaya oblast&#39;</option>
     140<option value="74">Chelyabinskaya oblast&#39;</option>
     141<option value="76">Yaroslavskaya oblast&#39;</option>
     142<option value="79">Evreyskaya avtonomnaja oblast&#39;</option>
     143<option value="83">Neneckiy autonomnyy okrug</option>
     144<option value="86">Khanty-Mansiyskiy avtonomnyy okrug - Yugra</option>
     145<option value="87">Chukotskiy avtonomnyy okrug</option>
     146<option value="89">Yamalo-Neneckiy avtonomnyy okrug</option>
     147</select>'''
     148        self.assertEqual(f.render('region', '67'), out)
  • tests/regressiontests/forms/localflavortests.py

    diff --git a/tests/regressiontests/forms/localflavortests.py b/tests/regressiontests/forms/localflavortests.py
    index 5ee1c32..8c6a4c5 100644
    a b from localflavor.nl import NLLocalFlavorTests 
    2323from localflavor.pl import PLLocalFlavorTests
    2424from localflavor.pt import PTLocalFlavorTests
    2525from localflavor.ro import ROLocalFlavorTests
     26from localflavor.ru import RULocalFlavorTests
    2627from localflavor.se import SELocalFlavorTests
    2728from localflavor.sk import SKLocalFlavorTests
    2829from localflavor.tr import TRLocalFlavorTests
  • tests/regressiontests/forms/tests/__init__.py

    diff --git a/tests/regressiontests/forms/tests/__init__.py b/tests/regressiontests/forms/tests/__init__.py
    index 2d96b2f..770f0de 100644
    a b from regressiontests.forms.localflavortests import ( 
    2222    NLLocalFlavorTests, PLLocalFlavorTests, PTLocalFlavorTests,
    2323    ROLocalFlavorTests, SELocalFlavorTests, SKLocalFlavorTests,
    2424    TRLocalFlavorTests, UKLocalFlavorTests, USLocalFlavorTests,
    25     UYLocalFlavorTests, ZALocalFlavorTests
     25    UYLocalFlavorTests, ZALocalFlavorTests, RULocalFlavorTests
    2626)
Back to Top