Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fixed #18767: Admin's calendar for datetime fields doesn't show right values with russian and some other locales #294

Closed
wants to merge 2 commits into from

3 participants

@uruz

Solution includes incomplete implementation of strptime. I've included test into commit.

aaugustin and others added some commits
@uruz uruz referenced this pull request from a commit in uruz/django
@uruz uruz Integrate pull request #294 into #618 3a719a6
@fabianbuechler fabianbuechler referenced this pull request from a commit in fabianbuechler/django
@fabianbuechler fabianbuechler Integrate pull request #294 into #618. 6fc8933
@uruz uruz referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@uruz uruz referenced this pull request from a commit in uruz/django
@fabianbuechler fabianbuechler Integrate pull request #294 into #618. 36b9d40
@timgraham
Owner

No longer merges cleanly. Also looks like it was incorporated in #618.

@timgraham timgraham closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 19, 2012
  1. @aaugustin

    Merge pull request #293 from mjtamlyn/patch-1

    aaugustin authored
    Fixed indentation in the Python3 docs
  2. @uruz

    Fixed #18767: Admin's calendar for datetime fields doesn't show right…

    uruz authored
    … values with russian and some other locales
This page is out of date. Refresh to see the latest.
View
54 django/contrib/admin/static/admin/js/admin/DateTimeShortcuts.js
@@ -214,15 +214,57 @@ var DateTimeShortcuts = {
var cal_box = document.getElementById(DateTimeShortcuts.calendarDivName1+num)
var cal_link = document.getElementById(DateTimeShortcuts.calendarLinkName+num)
var inp = DateTimeShortcuts.calendarInputs[num];
+ var value = inp.value;
// 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]);
- if (year.match(/\d\d\d\d/) && month >= 1 && month <= 12) {
- DateTimeShortcuts.calendars[num].drawDate(month, year);
+ if (value) {
+ //Poor and incomplete port of python's _strptime module.
+ //We only look for %Y, %y, %m, %d formats.
+ //That should be enough for every locale included with django,
+ //but still may not work with custom format files.
+ var format = get_format('DATE_INPUT_FORMATS')[0];
+ var regexes = {
+ 'Y' : '(\\d{4})',
+ 'y' : '(\\d{2})',
+ 'm' : '(\\d{2})',
+ 'd' : '(\\d{2})',
+ '%' : '%'
+ };
+ var directives = '';
+ var processed_format = '';
+ var directive_index;
+ //Escape those symbols in format that are special for regular expressions syntax
+ format = format.replace(new RegExp("([\\.^$*+?\(\){}\[\]|])", 'g'), '\\$&');
+ while (format.indexOf('%') !== -1){
+ directive_index = format.indexOf('%') + 1;
+ processed_format = processed_format + format.substr(0, directive_index-1) + regexes[format[directive_index]];
+ directives += format[directive_index];
+ format = format.substr(directive_index+1);
+ }
+ var format_regexp = new RegExp('^'+processed_format+'$');
+ if ( (directives.toLowerCase().indexOf('y') !== -1) && (directives.indexOf('m') !== -1)){
+ var groups = format_regexp.exec(value);
+ var year;
+ var year_index = directives.indexOf('Y');
+ if (year_index !== -1){
+ year = parseInt(groups[year_index+1]);
+ }else{
+ year_index = directives.indexOf('y');
+ year = parseInt(groups[year_index+1]);
+ if (year <= 68){
+ year += 2000;
+ }else{
+ year += 1900;
+ }
+ }
+ var month = parseInt(groups[directives.indexOf('m')+1]);
+ if ((month < 1) || (month > 12)){
+ month = undefined;
+ }
+ if (year && month){
+ DateTimeShortcuts.calendars[num].drawDate(month, year);
+ }
}
}
View
38 tests/regressiontests/admin_widgets/tests.py
@@ -14,11 +14,15 @@
from django.test import TestCase as DjangoTestCase
from django.test.utils import override_settings
from django.utils import translation
+from django.utils.importlib import import_module
from django.utils.html import conditional_escape
from django.utils.unittest import TestCase
+import os
+
from . import models
from .widgetadmin import site as widget_admin_site
+import gettext as gettext_module
admin_static_prefix = lambda: {
@@ -464,6 +468,40 @@ def test_show_hide_date_time_picker_widgets(self):
self.assertEqual(
self.get_css_value('#clockbox0', 'display'), 'none')
+ def test_show_correct_date(self):
+ '''
+ Ensure that click on date widget shows correct date for every
+ locale included with django
+ '''
+ self.admin_login(username='super', password='secret', login_url='/')
+ birthdate = datetime(1969, 7, 15)
+ member = models.Member(name = 'Alexander Vasilyev', birthdate = birthdate, gender = 'M')
+ member.save()
+ 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_module.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
+ july_translation = month_names.split(' ')[6]
+ expected_caption = '{0:s} {1:d}'.format(july_translation, 1969)
+ with override_settings(LANGUAGE_CODE = language_code):
+ self.selenium.get('{}{}'.format(self.live_server_url, '/admin_widgets/member/{}/'.format(member.pk)))
+ self.assertEqual(self.get_css_value('#calendarbox0', 'display'), 'none')
+ # Click the calendar icon
+ self.selenium.find_element_by_id('calendarlink0').click()
+ # Check that the date picker is visible
+ self.assertEqual(self.get_css_value('#calendarbox0', 'display'), 'block')
+ # Check that calendar's caption is what we expected
+ caption = self.selenium.find_element_by_css_selector('#calendarin0>table>caption').text
+ self.assertEqual(caption, expected_caption)
+
+
class DateTimePickerSeleniumChromeTests(DateTimePickerSeleniumFirefoxTests):
webdriver_class = 'selenium.webdriver.chrome.webdriver.WebDriver'
Something went wrong with that request. Please try again.