From 25e06bca57c068d4b9e9b4221b16a667ccb0d38e Mon Sep 17 00:00:00 2001 From: Maxime Turcotte Date: Tue, 15 Apr 2014 15:01:55 -0400 Subject: [PATCH] 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. --- .../admin/js/admin/DateTimeShortcuts.js | 11 +++-- django/contrib/admin/static/admin/js/core.js | 24 ++++++++++ tests/admin_widgets/tests.py | 47 +++++++++++++++++++ 3 files changed, 77 insertions(+), 5 deletions(-) diff --git a/django/contrib/admin/static/admin/js/admin/DateTimeShortcuts.js b/django/contrib/admin/static/admin/js/admin/DateTimeShortcuts.js index fdd4bef53ad..faec5a49af1 100644 --- a/django/contrib/admin/static/admin/js/admin/DateTimeShortcuts.js +++ b/django/contrib/admin/static/admin/js/admin/DateTimeShortcuts.js @@ -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); } } diff --git a/django/contrib/admin/static/admin/js/core.js b/django/contrib/admin/static/admin/js/core.js index 9e735af1e34..2c096aac6db 100644 --- a/django/contrib/admin/static/admin/js/core.js +++ b/django/contrib/admin/static/admin/js/core.js @@ -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 // ---------------------------------------------------------------------------- diff --git a/tests/admin_widgets/tests.py b/tests/admin_widgets/tests.py index 83ae9685239..fb31334b28a 100644 --- a/tests/admin_widgets/tests.py +++ b/tests/admin_widgets/tests.py @@ -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'