Permalink
Browse files

Fixed #15856 -- Added Macedonian localflavor. Many thanks to vasiliyeah.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16385 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent 9f3d76a commit 050e11956f8bf6589e8c93c6ccdbe5fc03e7e410 @jezdez jezdez committed Jun 12, 2011
@@ -0,0 +1,100 @@
+import datetime
+
+from django.core.validators import EMPTY_VALUES
+from django.forms import ValidationError
+from django.forms.fields import RegexField, Select
+from django.utils.translation import ugettext_lazy as _
+
+from mk_choices import MK_MUNICIPALITIES
+
+
+class MKIdentityCardNumberField(RegexField):
+ """
+ A Macedonian ID card number. Accepts both old and new format.
+ """
+ default_error_messages = {
+ 'invalid': _(u'Identity card numbers must contain'
+ ' either 4 to 7 digits or an uppercase letter and 7 digits.'),
+ }
+
+ def __init__(self, *args, **kwargs):
+ kwargs['min_length'] = None
+ kwargs['max_length'] = 8
+ regex = ur'(^[A-Z]{1}\d{7}$)|(^\d{4,7}$)'
+ super(MKIdentityCardNumberField, self).__init__(regex, *args, **kwargs)
+
+
+class MKMunicipalitySelect(Select):
+ """
+ A form ``Select`` widget that uses a list of Macedonian municipalities as
+ choices. The label is the name of the municipality and the value
+ is a 2 character code for the municipality.
+ """
+
+ def __init__(self, attrs=None):
+ super(MKMunicipalitySelect, self).__init__(attrs, choices = MK_MUNICIPALITIES)
+
+
+class UMCNField(RegexField):
+ """
+ A form field that validates input as a unique master citizen
+ number.
+
+ The format of the unique master citizen number has been kept the same from
+ Yugoslavia. It is still in use in other countries as well, it is not applicable
+ solely in Macedonia. For more information see:
+ https://secure.wikimedia.org/wikipedia/en/wiki/Unique_Master_Citizen_Number
+
+ A value will pass validation if it complies to the following rules:
+
+ * Consists of exactly 13 digits
+ * The first 7 digits represent a valid past date in the format DDMMYYY
+ * The last digit of the UMCN passes a checksum test
+ """
+ default_error_messages = {
+ 'invalid': _(u'This field should contain exactly 13 digits.'),
+ 'date': _(u'The first 7 digits of the UMCN must represent a valid past date.'),
+ 'checksum': _(u'The UMCN is not valid.'),
+ }
+
+ def __init__(self, *args, **kwargs):
+ kwargs['min_length'] = None
+ kwargs['max_length'] = 13
+ super(UMCNField, self).__init__(r'^\d{13}$', *args, **kwargs)
+
+ def clean(self, value):
+ value = super(UMCNField, self).clean(value)
+
+ if value in EMPTY_VALUES:
+ return u''
+
+ if not self._validate_date_part(value):
+ raise ValidationError(self.error_messages['date'])
+ if self._validate_checksum(value):
+ return value
+ else:
+ raise ValidationError(self.error_messages['checksum'])
+
+ def _validate_checksum(self, value):
+ a,b,c,d,e,f,g,h,i,j,k,l,K = [int(digit) for digit in value]
+ m = 11 - (( 7*(a+g) + 6*(b+h) + 5*(c+i) + 4*(d+j) + 3*(e+k) + 2*(f+l)) % 11)
+ if (m >= 1 and m <= 9) and K == m:
+ return True
+ elif m == 11 and K == 0:
+ return True
+ else:
+ return False
+
+ def _validate_date_part(self, value):
+ daypart, monthpart, yearpart = int(value[:2]), int(value[2:4]), int(value[4:7])
+ if yearpart >= 800:
+ yearpart += 1000
+ else:
+ yearpart += 2000
+ try:
+ date = datetime.datetime(year = yearpart, month = monthpart, day = daypart).date()
+ except ValueError:
+ return False
+ if date >= datetime.datetime.now().date():
+ return False
+ return True
@@ -0,0 +1,92 @@
+# -*- coding: utf-8 -*-
+"""
+Macedonian municipalities per the reorganization from 2004.
+"""
+from django.utils.translation import ugettext_lazy as _
+
+MK_MUNICIPALITIES = (
+ ('AD', _(u'Aerodrom')),
+ ('AR', _(u'Aračinovo')),
+ ('BR', _(u'Berovo')),
+ ('TL', _(u'Bitola')),
+ ('BG', _(u'Bogdanci')),
+ ('VJ', _(u'Bogovinje')),
+ ('BS', _(u'Bosilovo')),
+ ('BN', _(u'Brvenica')),
+ ('BU', _(u'Butel')),
+ ('VA', _(u'Valandovo')),
+ ('VL', _(u'Vasilevo')),
+ ('VV', _(u'Vevčani')),
+ ('VE', _(u'Veles')),
+ ('NI', _(u'Vinica')),
+ ('VC', _(u'Vraneštica')),
+ ('VH', _(u'Vrapčište')),
+ ('GB', _(u'Gazi Baba')),
+ ('GV', _(u'Gevgelija')),
+ ('GT', _(u'Gostivar')),
+ ('GR', _(u'Gradsko')),
+ ('DB', _(u'Debar')),
+ ('DA', _(u'Debarca')),
+ ('DL', _(u'Delčevo')),
+ ('DK', _(u'Demir Kapija')),
+ ('DM', _(u'Demir Hisar')),
+ ('DE', _(u'Dolneni')),
+ ('DR', _(u'Drugovo')),
+ ('GP', _(u'Gjorče Petrov')),
+ ('ZE', _(u'Želino')),
+ ('ZA', _(u'Zajas')),
+ ('ZK', _(u'Zelenikovo')),
+ ('ZR', _(u'Zrnovci')),
+ ('IL', _(u'Ilinden')),
+ ('JG', _(u'Jegunovce')),
+ ('AV', _(u'Kavadarci')),
+ ('KB', _(u'Karbinci')),
+ ('KX', _(u'Karpoš')),
+ ('VD', _(u'Kisela Voda')),
+ ('KH', _(u'Kičevo')),
+ ('KN', _(u'Konče')),
+ ('OC', _(u'Koćani')),
+ ('KY', _(u'Kratovo')),
+ ('KZ', _(u'Kriva Palanka')),
+ ('KG', _(u'Krivogaštani')),
+ ('KS', _(u'Kruševo')),
+ ('UM', _(u'Kumanovo')),
+ ('LI', _(u'Lipkovo')),
+ ('LO', _(u'Lozovo')),
+ ('MR', _(u'Mavrovo i Rostuša')),
+ ('MK', _(u'Makedonska Kamenica')),
+ ('MD', _(u'Makedonski Brod')),
+ ('MG', _(u'Mogila')),
+ ('NG', _(u'Negotino')),
+ ('NV', _(u'Novaci')),
+ ('NS', _(u'Novo Selo')),
+ ('OS', _(u'Oslomej')),
+ ('OD', _(u'Ohrid')),
+ ('PE', _(u'Petrovec')),
+ ('PH', _(u'Pehčevo')),
+ ('PN', _(u'Plasnica')),
+ ('PP', _(u'Prilep')),
+ ('PT', _(u'Probištip')),
+ ('RV', _(u'Radoviš')),
+ ('RN', _(u'Rankovce')),
+ ('RE', _(u'Resen')),
+ ('RO', _(u'Rosoman')),
+ ('AJ', _(u'Saraj')),
+ ('SL', _(u'Sveti Nikole')),
+ ('SS', _(u'Sopište')),
+ ('SD', _(u'Star Dojran')),
+ ('NA', _(u'Staro Nagoričane')),
+ ('UG', _(u'Struga')),
+ ('RU', _(u'Strumica')),
+ ('SU', _(u'Studeničani')),
+ ('TR', _(u'Tearce')),
+ ('ET', _(u'Tetovo')),
+ ('CE', _(u'Centar')),
+ ('CZ', _(u'Centar-Župa')),
+ ('CI', _(u'Čair')),
+ ('CA', _(u'Čaška')),
+ ('CH', _(u'Češinovo-Obleševo')),
+ ('CS', _(u'Čučer-Sandevo')),
+ ('ST', _(u'Štip')),
+ ('SO', _(u'Šuto Orizari')),
+)
@@ -0,0 +1,44 @@
+from django.db.models.fields import CharField
+from django.utils.translation import ugettext_lazy as _
+
+from django.contrib.localflavor.mk.mk_choices import MK_MUNICIPALITIES
+from django.contrib.localflavor.mk.forms import (UMCNField as UMCNFormField,
+ MKIdentityCardNumberField as MKIdentityCardNumberFormField)
+
+
+class MKIdentityCardNumberField(CharField):
+
+ description = _("Macedonian identity card number")
+
+ def __init__(self, *args, **kwargs):
+ kwargs['max_length'] = 8
+ super(MKIdentityCardNumberField, self).__init__(*args, **kwargs)
+
+ def formfield(self, **kwargs):
+ defaults = {'form_class' : MKIdentityCardNumberFormField}
+ defaults.update(kwargs)
+ return super(MKIdentityCardNumberField, self).formfield(**defaults)
+
+
+class MKMunicipalityField(CharField):
+
+ description = _("A Macedonian municipality (2 character code)")
+
+ def __init__(self, *args, **kwargs):
+ kwargs['choices'] = MK_MUNICIPALITIES
+ kwargs['max_length'] = 2
+ super(MKMunicipalityField, self).__init__(*args, **kwargs)
+
+
+class UMCNField(CharField):
+
+ description = _("Unique master citizen number (13 digits)")
+
+ def __init__(self, *args, **kwargs):
+ kwargs['max_length'] = 13
+ super(UMCNField, self).__init__(*args, **kwargs)
+
+ def formfield(self, **kwargs):
+ defaults = {'form_class' : UMCNFormField}
+ defaults.update(kwargs)
+ return super(UMCNField, self).formfield(**defaults)
@@ -57,6 +57,7 @@ Countries currently supported by :mod:`~django.contrib.localflavor` are:
* Italy_
* Japan_
* Kuwait_
+ * Macedonia_
* Mexico_
* `The Netherlands`_
* Norway_
@@ -110,6 +111,7 @@ Here's an example of how to use them::
.. _Italy: `Italy (it)`_
.. _Japan: `Japan (jp)`_
.. _Kuwait: `Kuwait (kw)`_
+.. _Macedonia: `Macedonia (mk)`_
.. _Mexico: `Mexico (mx)`_
.. _Norway: `Norway (no)`_
.. _Peru: `Peru (pe)`_
@@ -652,8 +654,6 @@ Israel (``il``)
.. _Israeli identification number: http://he.wikipedia.org/wiki/%D7%9E%D7%A1%D7%A4%D7%A8_%D7%96%D7%94%D7%95%D7%AA_(%D7%99%D7%A9%D7%A8%D7%90%D7%9C)
.. _Luhn algorithm: http://en.wikipedia.org/wiki/Luhn_algorithm
-
-
Italy (``it``)
==============
@@ -705,6 +705,57 @@ Kuwait (``kw``)
* The birthdate of the person is a valid date.
* The calculated checksum equals to the last digit of the Civil ID.
+Macedonia (``mk``)
+===================
+
+.. versionadded:: 1.4
+
+.. class:: mk.forms.MKIdentityCardNumberField
+
+ A form field that validates input as a Macedonian identity card number.
+ Both old and new identity card numbers are supported.
+
+
+.. class:: mk.forms.MKMunicipalitySelect
+
+ A form ``Select`` widget that uses a list of Macedonian municipalities as
+ choices.
+
+
+.. class:: mk.forms.UMCNField
+
+ A form field that validates input as a unique master citizen
+ number.
+
+ The format of the unique master citizen number is not unique
+ to Macedonia. For more information see:
+ https://secure.wikimedia.org/wikipedia/en/wiki/Unique_Master_Citizen_Number
+
+ A value will pass validation if it complies to the following rules:
+
+ * Consists of exactly 13 digits
+ * The first 7 digits represent a valid past date in the format DDMMYYY
+ * The last digit of the UMCN passes a checksum test
+
+
+.. class:: mk.models.MKIdentityCardNumberField
+
+ A model field that forms represent as a
+ ``forms.MKIdentityCardNumberField`` field.
+
+
+.. class:: mk.models.MKMunicipalityField
+
+ A model field that forms represent as a
+ ``forms.MKMunicipalitySelect`` and stores the 2 character code of the
+ municipality in the database.
+
+
+.. class:: mk.models.UMCNField
+
+ A model field that forms represent as a ``forms.UMCNField`` field.
+
+
Mexico (``mx``)
===============
Oops, something went wrong.

0 comments on commit 050e119

Please sign in to comment.