Commits

garc...@bcc190cf-cafb-0310-a4f2-bffc1f526a37  committed 9520f4e

[soc2009/i18n] Tests improved, and bugs fixed on and because of tests. All tests now validate.

  • Participants
  • Parent commits ebf6d4f
  • Branches soc2009/i18n-improvements

Comments (0)

Files changed (7)

File django/conf/locale/ca/formats.py

 
 DATE_FORMAT = 'j \de F \de Y'
 TIME_FORMAT = 'G:i:s'
-DATETIME_FORMAT = 'j \de F \de Y \a \le\s G:i'
+DATETIME_FORMAT = 'j \de F \de Y \\a \le\s G:i'
 YEAR_MONTH_FORMAT = 'F \de\l Y'
 MONTH_DAY_FORMAT = 'j \de F'
 SHORT_DATE_FORMAT = 'd/m/Y'

File django/conf/locale/fr/formats.py

 # TIME_INPUT_FORMATS = 
 # DATETIME_INPUT_FORMATS = 
 DECIMAL_SEPARATOR = ','
-THOUSAND_SEPARATOR = ' '
+THOUSAND_SEPARATOR = ' '
 # NUMBER_GROUPING = 

File django/forms/extras/widgets.py

 from django.utils.dates import MONTHS
 from django.utils.safestring import mark_safe
 from django.utils.formats import getformat
+from django.conf import settings
 
 __all__ = ('SelectDateWidget',)
 
         day_html = self.create_select(name, self.day_field, value, day_val,  choices)
 
         format = getformat('DATE_FORMAT')
+        escaped = False
         output = []
         for char in format:
-            if char in 'Yy':
+            if escaped:
+                escaped = False
+            elif char == '\\':
+                escaped = True
+            elif char in 'Yy':
                 output.append(year_html)
             elif char in 'bFMmNn':
                 output.append(month_html)
         if y == m == d == "0":
             return None
         if y and m and d:
-            return '%s-%s-%s' % (y, m, d)
+            if settings.USE_FORMAT_I18N:
+                input_format = getformat('DATE_INPUT_FORMATS')[0]
+                try:
+                    date_value = datetime.date(int(y), int(m), int(d))
+                except ValueError:
+                    pass
+                else:
+                    return date_value.strftime(input_format)
+            else:
+                return '%s-%s-%s' % (y, m, d)
         return data.get(name, None)
 
     def create_select(self, name, field, value, val, choices):

File django/forms/fields.py

 
     def __init__(self, input_formats=None, *args, **kwargs):
         super(DateField, self).__init__(*args, **kwargs)
-        self.input_formats = input_formats or getformat('DATE_INPUT_FORMATS')
+        self.input_formats = input_formats
 
     def clean(self, value):
         """
             return value.date()
         if isinstance(value, datetime.date):
             return value
-        for format in self.input_formats:
+        for format in self.input_formats or getformat('DATE_INPUT_FORMATS'):
             try:
                 return datetime.date(*time.strptime(value, format)[:3])
             except ValueError:
 
     def __init__(self, input_formats=None, *args, **kwargs):
         super(TimeField, self).__init__(*args, **kwargs)
-        self.input_formats = input_formats or getformat('TIME_INPUT_FORMATS')
+        self.input_formats = input_formats
 
     def clean(self, value):
         """
             return None
         if isinstance(value, datetime.time):
             return value
-        for format in self.input_formats:
+        for format in self.input_formats or getformat('TIME_INPUT_FORMATS'):
             try:
                 return datetime.time(*time.strptime(value, format)[3:6])
             except ValueError:
 
     def __init__(self, input_formats=None, *args, **kwargs):
         super(DateTimeField, self).__init__(*args, **kwargs)
-        self.input_formats = input_formats or getformat('DATETIME_INPUT_FORMATS')
+        self.input_formats = input_formats
 
     def clean(self, value):
         """
             if len(value) != 2:
                 raise ValidationError(self.error_messages['invalid'])
             value = '%s %s' % tuple(value)
-        for format in self.input_formats:
+        for format in self.input_formats or getformat('DATETIME_INPUT_FORMATS'):
             try:
                 return datetime.datetime(*time.strptime(value, format)[:6])
             except ValueError:

File django/utils/formats.py

 from django.utils import dateformat
 from django.utils import numberformat 
 
-def getformat_null(format_type):
-    """
-    For a specific format type, returns the default format as
-    set on the settings.
-    format_type is the name of the format, for example 'DATE_FORMAT'
-    """
-    return getattr(settings, format_type)
-
 def project_formats_module():
     """
     Returns the formats module for the current locale, defined
     except ImportError:
         return None
 
-def getformat_real(format_type):
+def getformat(format_type):
     """
     For a specific format type, returns the format for the
     current language (locale) defaulting to the format on settings.
     format_type is the name of the format, for example 'DATE_FORMAT'
     """
-    for module in (project_formats_module(), django_formats_module()):
-        if module:
-            try:
-                return getattr(module, format_type)
-            except AttributeError:
-                pass
-    return getformat_null(format_type)
-
-# getformat will just return the value on setings if
-# we don't use i18n in our project
-if settings.USE_I18N and settings.USE_FORMAT_I18N:
-    getformat = getformat_real
-else:
-    getformat = getformat_null
+    if settings.USE_I18N and settings.USE_FORMAT_I18N:
+        for module in (project_formats_module(), django_formats_module()):
+            if module:
+                try:
+                    return getattr(module, format_type)
+                except AttributeError:
+                    pass
+    return getattr(settings, format_type)
 
 def date_format(value, format=None):
     """

File django/views/i18n.py

     for k, v in pdict.items():
         src.append("catalog['%s'] = [%s];\n" % (javascript_quote(k), ','.join(["''"]*(v+1))))
     for k, v in get_formats().items():
-        src.append("catalog['%s'] = '%s';\n" % (javascript_quote(k), javascript_quote(unicode(v)).encode('utf-8')))
+        src.append("catalog['%s'] = '%s';\n" % (javascript_quote(k), javascript_quote(unicode(v))))
     src.extend(csrc)
     src.append(LibFoot)
     src.append(InterPolate)

File tests/regressiontests/i18n/tests.py

 ...     date_field = forms.DateField()
 ...     datetime_field = forms.DateTimeField()
 ...     time_field = forms.TimeField()
+>>> class SelectDateForm(forms.Form):
+...     date_field = forms.DateField(widget=SelectDateWidget)
 
 Locale independent
 
 >>> template.Template('{{ f }}').render(ctxt)
 u'99999.999'
 >>> template.Template('{{ d }}').render(ctxt)
-u'desembre 31, 2009'
+u'2009-12-31'
 >>> template.Template('{{ dt }}').render(ctxt)
-u'desembre 31, 2009, 8:50 p.m.'
+u'2009-12-31 20:50:00'
 >>> template.Template('{{ n|floatformat:2 }}').render(ctxt)
 u'66666.67'
 >>> template.Template('{{ f|floatformat }}').render(ctxt)
 u'12/31/2009'
 >>> template.Template('{{ dt|date:"SHORT_DATETIME_FORMAT" }}').render(ctxt)
 u'12/31/2009 8:50 p.m.'
->>> form = I18nForm({'decimal_field': u'66666,666', 'float_field': u'99999,999', 'date_field': u'12/31/2009', 'datetime_field': u'12/31/2009 20:50', 'time_field': u'20:50'})
+>>> form = I18nForm({'decimal_field': u'66666,666', 'float_field': u'99999,999', 'date_field': u'31/12/2009', 'datetime_field': u'31/12/2009 20:50', 'time_field': u'20:50'})
 >>> form.is_valid()
 False
+>>> form.errors['float_field']
+[u'Introdu\xefu un n\xfamero.']
+>>> form.errors['decimal_field']
+[u'Introdu\xefu un n\xfamero.']
+>>> form.errors['date_field']
+[u'Introdu\xefu una data v\xe0lida.']
+>>> form.errors['datetime_field']
+[u'Introdu\xefu una data/hora v\xe0lides.']
+>>> form2 = SelectDateForm({'date_field_month': u'12', 'date_field_day': u'31', 'date_field_year': u'2009'})
+>>> form2.is_valid()
+True
+>>> form2.cleaned_data['date_field']
+datetime.date(2009, 12, 31)
 >>> SelectDateWidget().render('mydate', datetime.date(2009, 12, 31))
 u'<select name="mydate_month" id="id_mydate_month">\n<option value="1">gener</option>\n<option value="2">febrer</option>\n<option value="3">mar\xe7</option>\n<option value="4">abril</option>\n<option value="5">maig</option>\n<option value="6">juny</option>\n<option value="7">juliol</option>\n<option value="8">agost</option>\n<option value="9">setembre</option>\n<option value="10">octubre</option>\n<option value="11">novembre</option>\n<option value="12" selected="selected">desembre</option>\n</select>\n<select name="mydate_day" id="id_mydate_day">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31" selected="selected">31</option>\n</select>\n<select name="mydate_year" id="id_mydate_year">\n<option value="2009" selected="selected">2009</option>\n<option value="2010">2010</option>\n<option value="2011">2011</option>\n<option value="2012">2012</option>\n<option value="2013">2013</option>\n<option value="2014">2014</option>\n<option value="2015">2015</option>\n<option value="2016">2016</option>\n<option value="2017">2017</option>\n<option value="2018">2018</option>\n</select>'
 
 >>> settings.USE_FORMAT_I18N = True
 >>> activate('ca')
 >>> getformat('DATE_FORMAT')
-'j \de N \de Y'
+'j \\de F \\de Y'
 >>> getformat('FIRST_DAY_OF_WEEK')
 1
 >>> getformat('DECIMAL_SEPARATOR')
 >>> date_format(d)
 u'31 de desembre de 2009'
 >>> date_format(d, 'YEAR_MONTH_FORMAT')
-u'desembre de 2009'
+u'desembre del 2009'
 >>> date_format(dt, 'SHORT_DATETIME_FORMAT')
 u'31/12/2009 20:50'
 >>> localize('No localizable')
 datetime.datetime(2009, 12, 31, 20, 50)
 >>> form.cleaned_data['time_field']
 datetime.time(20, 50)
+>>> form2 = SelectDateForm({'date_field_month': u'12', 'date_field_day': u'31', 'date_field_year': u'2009'})
+>>> form2.is_valid()
+True
+>>> form2.cleaned_data['date_field']
+datetime.date(2009, 12, 31)
 >>> SelectDateWidget().render('mydate', datetime.date(2009, 12, 31))
 u'<select name="mydate_day" id="id_mydate_day">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31" selected="selected">31</option>\n</select>\n<select name="mydate_month" id="id_mydate_month">\n<option value="1">gener</option>\n<option value="2">febrer</option>\n<option value="3">mar\xe7</option>\n<option value="4">abril</option>\n<option value="5">maig</option>\n<option value="6">juny</option>\n<option value="7">juliol</option>\n<option value="8">agost</option>\n<option value="9">setembre</option>\n<option value="10">octubre</option>\n<option value="11">novembre</option>\n<option value="12" selected="selected">desembre</option>\n</select>\n<select name="mydate_year" id="id_mydate_year">\n<option value="2009" selected="selected">2009</option>\n<option value="2010">2010</option>\n<option value="2011">2011</option>\n<option value="2012">2012</option>\n<option value="2013">2013</option>\n<option value="2014">2014</option>\n<option value="2015">2015</option>\n<option value="2016">2016</option>\n<option value="2017">2017</option>\n<option value="2018">2018</option>\n</select>'
 
 datetime.datetime(2009, 12, 31, 20, 50)
 >>> form.cleaned_data['time_field']
 datetime.time(20, 50)
+>>> form2 = SelectDateForm({'date_field_month': u'12', 'date_field_day': u'31', 'date_field_year': u'2009'})
+>>> form2.is_valid()
+True
+>>> form2.cleaned_data['date_field']
+datetime.date(2009, 12, 31)
 >>> SelectDateWidget().render('mydate', datetime.date(2009, 12, 31))
 u'<select name="mydate_month" id="id_mydate_month">\n<option value="1">January</option>\n<option value="2">February</option>\n<option value="3">March</option>\n<option value="4">April</option>\n<option value="5">May</option>\n<option value="6">June</option>\n<option value="7">July</option>\n<option value="8">August</option>\n<option value="9">September</option>\n<option value="10">October</option>\n<option value="11">November</option>\n<option value="12" selected="selected">December</option>\n</select>\n<select name="mydate_day" id="id_mydate_day">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31" selected="selected">31</option>\n</select>\n<select name="mydate_year" id="id_mydate_year">\n<option value="2009" selected="selected">2009</option>\n<option value="2010">2010</option>\n<option value="2011">2011</option>\n<option value="2012">2012</option>\n<option value="2013">2013</option>\n<option value="2014">2014</option>\n<option value="2015">2015</option>\n<option value="2016">2016</option>\n<option value="2017">2017</option>\n<option value="2018">2018</option>\n</select>'