Commit 25e06bca authored by Maxime Turcotte's avatar Maxime Turcotte Committed by Tim Graham
Browse files

Fixed #18767 -- Fixed admin calendar for other locales than English.

Refactored openCalendar function from DateTimeShortcuts.js. Now, when
entered manually in the input field, the date will show up correctly on
the calendar for locales that don't use "-" for separator.

Thanks charettes for revivew and Alexey Boriskin for some of the patch.
parent 2a9f44df
Loading
Loading
Loading
Loading
+6 −5
Original line number Diff line number Diff line
@@ -290,11 +290,12 @@ var DateTimeShortcuts = {
        // Determine if the current value in the input has a valid date.
        // If so, draw the calendar with that date's year and month.
        if (inp.value) {
            var date_parts = inp.value.split('-');
            var year = date_parts[0];
            var month = parseFloat(date_parts[1]);
            var selected = new Date(inp.value);
            if (year.match(/\d\d\d\d/) && month >= 1 && month <= 12) {
            var format = get_format('DATE_INPUT_FORMATS')[0];
            var selected = inp.value.strptime(format);
            var year = selected.getFullYear();
            var month = selected.getMonth() + 1;
            var re = /\d{4}/
            if (re.test(year.toString()) && month >= 1 && month <= 12) {
                DateTimeShortcuts.calendars[num].drawDate(month, year, selected);
            }
        }
+24 −0
Original line number Diff line number Diff line
@@ -204,6 +204,30 @@ String.prototype.pad_left = function(pad_length, pad_string) {
    return new_string;
}

String.prototype.strptime = function(format) {
    var split_format = format.split(/[.\-/]/);
    var date = this.split(/[.\-/]/);
    var i = 0;
    while (i < split_format.length) {
        switch (split_format[i]) {
            case "%d":
                var day = date[i];
                break;
            case "%m":
                var month = date[i] - 1;
                break;
            case "%Y":
                var year = date[i];
                break;
            case "%y":
                var year = date[i];
                break;
        }
        ++i;
    };
    return new Date(year, month, day);
}

// ----------------------------------------------------------------------------
// Get the computed style for and element
// ----------------------------------------------------------------------------
+47 −0
Original line number Diff line number Diff line
@@ -2,6 +2,9 @@
from __future__ import unicode_literals

from datetime import datetime, timedelta
import gettext
from importlib import import_module
import os
from unittest import TestCase, skipIf

try:
@@ -637,6 +640,50 @@ class DateTimePickerSeleniumFirefoxTests(AdminSeleniumWebDriverTestCase):

        self.assertEqual(len(selected), 0)

    def test_calendar_show_date_from_input(self):
        """
        Ensure that the calendar show the date from the input field for every
        locale supported by django.
        """
        self.admin_login(username='super', password='secret', login_url='/')

        # Enter test data
        member = models.Member.objects.create(name='Bob', birthdate=datetime(1984, 05, 15), gender='M')

        # Get month names translations for every locales
        month_string = 'January February March April May June July August September October November December'
        path = os.path.join(os.path.dirname(import_module('django.contrib.admin').__file__), 'locale')
        for language_code, language_name in settings.LANGUAGES:
            try:
                catalog = gettext.translation('djangojs', path, [language_code])
            except IOError:
                continue
            if month_string in catalog._catalog:
                month_names = catalog._catalog[month_string]
            else:
                month_names = month_string

            # Get the expected caption
            may_translation = month_names.split(' ')[4]
            expected_caption = '{0:s} {1:d}'.format(may_translation, 1984)

            # Test with every locale
            with override_settings(LANGUAGE_CODE=language_code, USE_L10N=True):

                # Open a page that has a date picker widget
                self.selenium.get('{}{}'.format(self.live_server_url,
                    '/admin_widgets/member/{}/'.format(member.pk)))

                # Click on the calendar icon
                self.selenium.find_element_by_id('calendarlink0').click()

                # Get the calendar caption
                calendar0 = self.selenium.find_element_by_id('calendarin0')
                caption = calendar0.find_element_by_tag_name('caption')

                # Make sure that the right month and year are displayed
                self.assertEqual(caption.text, expected_caption)


class DateTimePickerSeleniumChromeTests(DateTimePickerSeleniumFirefoxTests):
    webdriver_class = 'selenium.webdriver.chrome.webdriver.WebDriver'