diff --git a/django/contrib/admin/media/js/admin/DateTimeShortcuts.js b/django/contrib/admin/media/js/admin/DateTimeShortcuts.js index 5da6f2f48e..85482c4cd5 100644 --- a/django/contrib/admin/media/js/admin/DateTimeShortcuts.js +++ b/django/contrib/admin/media/js/admin/DateTimeShortcuts.js @@ -77,13 +77,15 @@ var DateTimeShortcuts = { addEvent(clock_box, 'click', DateTimeShortcuts.cancelEventPropagation); quickElement('h2', clock_box, 'Choose a time'); - time_list = quickElement('ul', clock_box, '', 'class', 'timelist'); + time_list = quickElement('ul', clock_box, ''); + time_list.className = 'timelist'; quickElement("a", quickElement("li", time_list, ""), "Now", "href", "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", new Date().getHourMinute());") quickElement("a", quickElement("li", time_list, ""), "Midnight", "href", "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", '00:00');") quickElement("a", quickElement("li", time_list, ""), "6 a.m.", "href", "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", '06:00');") quickElement("a", quickElement("li", time_list, ""), "Noon", "href", "javascript:DateTimeShortcuts.handleClockQuicklink(" + num + ", '12:00');") - cancel_p = quickElement('p', clock_box, '', 'class', 'calendar-cancel'); + cancel_p = quickElement('p', clock_box, ''); + cancel_p.className = 'calendar-cancel'; quickElement('a', cancel_p, 'Cancel', 'href', 'javascript:DateTimeShortcuts.dismissClock(' + num + ');'); }, openClock: function(num) { @@ -147,8 +149,10 @@ var DateTimeShortcuts = { // next-prev links var cal_nav = quickElement('div', cal_box, ''); - quickElement('a', cal_nav, '<', 'class', 'calendarnav-previous', 'href', 'javascript:DateTimeShortcuts.drawPrev('+num+');'); - quickElement('a', cal_nav, '>', 'class', 'calendarnav-next', 'href', 'javascript:DateTimeShortcuts.drawNext('+num+');'); + var cal_nav_prev = quickElement('a', cal_nav, '<', 'href', 'javascript:DateTimeShortcuts.drawPrev('+num+');'); + cal_nav_prev.className = 'calendarnav-previous'; + var cal_nav_next = quickElement('a', cal_nav, '>', 'href', 'javascript:DateTimeShortcuts.drawNext('+num+');'); + cal_nav_next.className = 'calendarnav-next'; cal_box.appendChild(cal_nav); // main box @@ -158,7 +162,8 @@ var DateTimeShortcuts = { DateTimeShortcuts.calendars[num].drawCurrent(); // calendar shortcuts - var shortcuts = quickElement('div', cal_box, '', 'class', 'calendar-shortcuts'); + var shortcuts = quickElement('div', cal_box, ''); + shortcuts.className = 'calendar-shortcuts'; quickElement('a', shortcuts, 'Yesterday', 'href', 'javascript:DateTimeShortcuts.handleCalendarQuickLink(' + num + ', -1);'); shortcuts.appendChild(document.createTextNode('\240|\240')); quickElement('a', shortcuts, 'Today', 'href', 'javascript:DateTimeShortcuts.handleCalendarQuickLink(' + num + ', 0);'); @@ -166,7 +171,8 @@ var DateTimeShortcuts = { quickElement('a', shortcuts, 'Tomorrow', 'href', 'javascript:DateTimeShortcuts.handleCalendarQuickLink(' + num + ', +1);'); // cancel bar - var cancel_p = quickElement('p', cal_box, '', 'class', 'calendar-cancel'); + var cancel_p = quickElement('p', cal_box, ''); + cancel_p.className = 'calendar-cancel'; quickElement('a', cancel_p, 'Cancel', 'href', 'javascript:DateTimeShortcuts.dismissCalendar(' + num + ');'); }, openCalendar: function(num) { diff --git a/django/contrib/admin/media/js/calendar.js b/django/contrib/admin/media/js/calendar.js index ad1f0a9734..eeff433bfb 100644 --- a/django/contrib/admin/media/js/calendar.js +++ b/django/contrib/admin/media/js/calendar.js @@ -65,7 +65,8 @@ var CalendarNamespace = { // Draw blanks before first of month tableRow = quickElement('tr', tableBody); for (var i = 0; i < startingPos; i++) { - quickElement('td', tableRow, ' ', 'bgcolor','#f3f3f3'); + var _cell = quickElement('td', tableRow, ' '); + _cell.style.backgroundColor = '#f3f3f3'; } // Draw days of month @@ -81,7 +82,8 @@ var CalendarNamespace = { // Draw blanks after end of month (optional, but makes for valid code) while (tableRow.childNodes.length < 7) { - quickElement('td', tableRow, ' ', 'bgcolor','#f3f3f3'); + var _cell = quickElement('td', tableRow, ' '); + _cell.style.backgroundColor = '#f3f3f3'; } calDiv.appendChild(calTable); diff --git a/django/contrib/admin/media/js/urlify.js b/django/contrib/admin/media/js/urlify.js index 412130ad6f..1e0f04b9ab 100644 --- a/django/contrib/admin/media/js/urlify.js +++ b/django/contrib/admin/media/js/urlify.js @@ -1,14 +1,13 @@ function URLify(s, num_chars) { // changes, e.g., "Petty theft" to "petty_theft" - // remove all these words from the string before urlifying - removelist = ["a", "an", "as", "at", "before", "but", "by", "for", "from", - "is", "in", "into", "like", "of", "off", "on", "onto", "per", - "since", "than", "the", "this", "that", "to", "up", "via", + removelist = ["a", "an", "as", "at", "before", "but", "by", "for", "from", + "is", "in", "into", "like", "of", "off", "on", "onto", "per", + "since", "than", "the", "this", "that", "to", "up", "via", "with"]; r = new RegExp('\\b(' + removelist.join('|') + ')\\b', 'gi'); s = s.replace(r, ''); - s = s.replace(/[^\w\s]/g, ''); // remove unneeded chars + s = s.replace(/[^\w\s-]/g, ''); // remove unneeded chars s = s.replace(/^\s+|\s+$/g, ''); // trim leading/trailing spaces s = s.replace(/\s+/g, '_'); // convert spaces to underscores s = s.toLowerCase(); // convert to lowercase diff --git a/django/core/meta/fields.py b/django/core/meta/fields.py index a718484e53..a15432dcd2 100644 --- a/django/core/meta/fields.py +++ b/django/core/meta/fields.py @@ -513,7 +513,7 @@ class PositiveSmallIntegerField(IntegerField): class SlugField(Field): def __init__(self, *args, **kwargs): kwargs['maxlength'] = 50 - kwargs.setdefault('validator_list', []).append(validators.isAlphaNumeric) + kwargs.setdefault('validator_list', []).append(validators.isSlug) # Set db_index=True unless it's been set manually. if not kwargs.has_key('db_index'): kwargs['db_index'] = True diff --git a/django/core/validators.py b/django/core/validators.py index 3e674e7b94..e6a7b069cf 100644 --- a/django/core/validators.py +++ b/django/core/validators.py @@ -21,6 +21,7 @@ email_re = re.compile(r'^[-\w.+]+@\w[\w.-]+$') integer_re = re.compile(r'^-?\d+$') ip4_re = re.compile(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$') phone_re = re.compile(r'^[A-PR-Y0-9]{3}-[A-PR-Y0-9]{3}-[A-PR-Y0-9]{4}$', re.IGNORECASE) +slug_re = re.compile(r'^[-\w]+$') url_re = re.compile(r'^http://\S+$') from django.conf.settings import JING_PATH @@ -60,6 +61,10 @@ def isAlphaNumericURL(field_data, all_data): if not alnumurl_re.search(field_data): raise ValidationError, _("This value must contain only letters, numbers, underscores and slashes.") +def isSlug(field_data, all_data): + if not slug_re.search(field_data): + raise ValidationError, "This value must contain only letters, numbers, underscores or hyphens." + def isLowerCase(field_data, all_data): if field_data.lower() != field_data: raise ValidationError, _("Uppercase letters are not allowed here.") diff --git a/django/utils/dateformat.py b/django/utils/dateformat.py index b558029840..3620558f09 100644 --- a/django/utils/dateformat.py +++ b/django/utils/dateformat.py @@ -11,64 +11,62 @@ Usage: >>> """ +from django.utils.dates import MONTHS, MONTHS_AP, WEEKDAYS from calendar import isleap -from dates import MONTHS, MONTHS_AP, WEEKDAYS +import re -class DateFormat: - year_days = [None, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334] +re_formatchars = re.compile(r'(? 11: + if self.data.hour > 11: return 'p.m.' return 'a.m.' def A(self): "'AM' or 'PM'" - if self.date.hour > 11: - return 'PM' - return 'AM' + return self.a().upper() def B(self): "Swatch Internet time" raise NotImplementedError - def d(self): - "Day of the month, 2 digits with leading zeros; i.e. '01' to '31'" - return '%02d' % self.date.day - - def D(self): - "Day of the week, textual, 3 letters; e.g. 'Fri'" - return WEEKDAYS[self.date.weekday()][0:3] - def f(self): """ Time, in 12-hour hours and minutes, with minutes left off if they're zero. Examples: '1', '1:30', '2:05', '2' Proprietary extension. """ - if self.date.minute == 0: + if self.data.minute == 0: return self.g() return '%s:%s' % (self.g(), self.i()) - def F(self): - "Month, textual, long; e.g. 'January'" - return MONTHS[self.date.month] - def g(self): "Hour, 12-hour format without leading zeros; i.e. '1' to '12'" - if self.date.hour == 0: + if self.data.hour == 0: return 12 - if self.date.hour > 12: - return self.date.hour - 12 - return self.date.hour + if self.data.hour > 12: + return self.data.hour - 12 + return self.data.hour def G(self): "Hour, 24-hour format without leading zeros; i.e. '0' to '23'" - return self.date.hour + return self.data.hour def h(self): "Hour, 12-hour format; i.e. '01' to '12'" @@ -80,43 +78,7 @@ class DateFormat: def i(self): "Minutes; i.e. '00' to '59'" - return '%02d' % self.date.minute - - def I(self): - "'1' if Daylight Savings Time, '0' otherwise." - raise NotImplementedError - - def j(self): - "Day of the month without leading zeros; i.e. '1' to '31'" - return self.date.day - - def l(self): - "Day of the week, textual, long; e.g. 'Friday'" - return WEEKDAYS[self.date.weekday()] - - def L(self): - "Boolean for whether it is a leap year; i.e. True or False" - return isleap(self.date.year) - - def m(self): - "Month; i.e. '01' to '12'" - return '%02d' % self.date.month - - def M(self): - "Month, textual, 3 letters; e.g. 'Jan'" - return MONTHS[self.date.month][0:3] - - def n(self): - "Month without leading zeros; i.e. '1' to '12'" - return self.date.month - - def N(self): - "Month abbreviation in Associated Press style. Proprietary extension." - return MONTHS_AP[self.date.month] - - def O(self): - "Difference to Greenwich time in hours; e.g. '+0200'" - raise NotImplementedError + return '%02d' % self.data.minute def P(self): """ @@ -125,25 +87,79 @@ class DateFormat: Examples: '1 a.m.', '1:30 p.m.', 'midnight', 'noon', '12:30 p.m.' Proprietary extension. """ - if self.date.minute == 0 and self.date.hour == 0: + if self.data.minute == 0 and self.data.hour == 0: return 'midnight' - if self.date.minute == 0 and self.date.hour == 12: + if self.data.minute == 0 and self.data.hour == 12: return 'noon' return '%s %s' % (self.f(), self.a()) + def s(self): + "Seconds; i.e. '00' to '59'" + return '%02d' % self.data.second + +class DateFormat(TimeFormat): + year_days = [None, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334] + + def __init__(self, d): + self.data = d + + def d(self): + "Day of the month, 2 digits with leading zeros; i.e. '01' to '31'" + return '%02d' % self.data.day + + def D(self): + "Day of the week, textual, 3 letters; e.g. 'Fri'" + return WEEKDAYS[self.data.weekday()][0:3] + + def F(self): + "Month, textual, long; e.g. 'January'" + return MONTHS[self.data.month] + + def I(self): + "'1' if Daylight Savings Time, '0' otherwise." + raise NotImplementedError + + def j(self): + "Day of the month without leading zeros; i.e. '1' to '31'" + return self.data.day + + def l(self): + "Day of the week, textual, long; e.g. 'Friday'" + return WEEKDAYS[self.data.weekday()] + + def L(self): + "Boolean for whether it is a leap year; i.e. True or False" + return isleap(self.data.year) + + def m(self): + "Month; i.e. '01' to '12'" + return '%02d' % self.data.month + + def M(self): + "Month, textual, 3 letters; e.g. 'Jan'" + return MONTHS[self.data.month][0:3] + + def n(self): + "Month without leading zeros; i.e. '1' to '12'" + return self.data.month + + def N(self): + "Month abbreviation in Associated Press style. Proprietary extension." + return MONTHS_AP[self.data.month] + + def O(self): + "Difference to Greenwich time in hours; e.g. '+0200'" + raise NotImplementedError + def r(self): "RFC 822 formatted date; e.g. 'Thu, 21 Dec 2000 16:01:07 +0200'" raise NotImplementedError - def s(self): - "Seconds; i.e. '00' to '59'" - return '%02d' % self.date.second - def S(self): "English ordinal suffix for the day of the month, 2 characters; i.e. 'st', 'nd', 'rd' or 'th'" - if self.date.day in (11, 12, 13): # Special case + if self.data.day in (11, 12, 13): # Special case return 'th' - last = self.date.day % 10 + last = self.data.day % 10 if last == 1: return 'st' if last == 2: @@ -166,25 +182,22 @@ class DateFormat: def w(self): "Day of the week, numeric, i.e. '0' (Sunday) to '6' (Saturday)" - weekday = self.date.weekday() - if weekday == 0: - return 6 - return weekday - 1 + return (self.data.weekday() + 1) % 7 def W(self): "ISO-8601 week number of year, weeks starting on Monday" # Algorithm from http://www.personal.ecu.edu/mccartyr/ISOwdALG.txt week_number = None - jan1_weekday = self.date.replace(month=1, day=1).weekday() + 1 - weekday = self.date.weekday() + 1 + jan1_weekday = self.data.replace(month=1, day=1).weekday() + 1 + weekday = self.data.weekday() + 1 day_of_year = self.z() if day_of_year <= (8 - jan1_weekday) and jan1_weekday > 4: - if jan1_weekday == 5 or (jan1_weekday == 6 and isleap(self.date.year-1)): + if jan1_weekday == 5 or (jan1_weekday == 6 and isleap(self.data.year-1)): week_number = 53 else: week_number = 52 else: - if isleap(self.date.year): + if isleap(self.data.year): i = 366 else: i = 365 @@ -197,18 +210,18 @@ class DateFormat: week_number -= 1 return week_number - def Y(self): - "Year, 4 digits; e.g. '1999'" - return self.date.year - def y(self): "Year, 2 digits; e.g. '99'" - return str(self.date.year)[2:] + return str(self.data.year)[2:] + + def Y(self): + "Year, 4 digits; e.g. '1999'" + return self.data.year def z(self): "Day of the year; i.e. '0' to '365'" - doy = self.year_days[self.date.month] + self.date.day - if self.L() and self.date.month > 2: + doy = self.year_days[self.data.month] + self.data.day + if self.L() and self.data.month > 2: doy += 1 return doy @@ -218,94 +231,6 @@ class DateFormat: is always positive.""" raise NotImplementedError - def format(self, formatstr): - result = '' - for char in formatstr: - try: - result += str(getattr(self, char)()) - except AttributeError: - result += char - return result - -class TimeFormat: - def __init__(self, t): - self.time = t - - def a(self): - "'a.m.' or 'p.m.'" - if self.time.hour > 11: - return 'p.m.' - else: - return 'a.m.' - - def A(self): - "'AM' or 'PM'" - return self.a().upper() - - def B(self): - "Swatch Internet time" - raise NotImplementedError - - def f(self): - """ - Time, in 12-hour hours and minutes, with minutes left off if they're zero. - Examples: '1', '1:30', '2:05', '2' - Proprietary extension. - """ - if self.time.minute == 0: - return self.g() - return '%s:%s' % (self.g(), self.i()) - - def g(self): - "Hour, 12-hour format without leading zeros; i.e. '1' to '12'" - if self.time.hour == 0: - return 12 - if self.time.hour > 12: - return self.time.hour - 12 - return self.time.hour - - def G(self): - "Hour, 24-hour format without leading zeros; i.e. '0' to '23'" - return self.time.hour - - def h(self): - "Hour, 12-hour format; i.e. '01' to '12'" - return '%02d' % self.g() - - def H(self): - "Hour, 24-hour format; i.e. '00' to '23'" - return '%02d' % self.G() - - def i(self): - "Minutes; i.e. '00' to '59'" - return '%02d' % self.time.minute - - def P(self): - """ - Time, in 12-hour hours, minutes and 'a.m.'/'p.m.', with minutes left off - if they're zero and the strings 'midnight' and 'noon' if appropriate. - Examples: '1 a.m.', '1:30 p.m.', 'midnight', 'noon', '12:30 p.m.' - Proprietary extension. - """ - if self.time.minute == 0 and self.time.hour == 0: - return 'midnight' - if self.time.minute == 0 and self.time.hour == 12: - return 'noon' - return '%s %s' % (self.f(), self.a()) - - def s(self): - "Seconds; i.e. '00' to '59'" - return '%02d' % self.time.second - - def format(self, formatstr): - result = '' - for char in formatstr: - try: - result += str(getattr(self, char)()) - except AttributeError: - result += char - return result - def format(value, format_string): "Convenience function" df = DateFormat(value) diff --git a/docs/model-api.txt b/docs/model-api.txt index 85d2e58aca..fa6d8f10e6 100644 --- a/docs/model-api.txt +++ b/docs/model-api.txt @@ -171,7 +171,14 @@ The following arguments are available to all field types. All are optional. Like ``unique_for_date`` and ``unique_for_month``. ``validator_list`` - A list of extra validators to apply to the field. + A list of extra validators to apply to the field. Each should be a callable + that takes the parameters ``field_data, all_data`` and raises + ``django.core.validators.ValidationError`` for errors. (See the + `validator docs`_.) + + Django comes with quite a few validators. They're in ``django.core.validators``. + +.. _validator docs: http://www.djangoproject.com/documentation/forms/#validators Field types ----------- @@ -237,7 +244,7 @@ Here are all available field types: ``EmailField`` A ``CharField`` that checks that the value is a valid e-mail address. - Currently, this is a loose test. + (Currently, this is a loose test.) This doesn't accept ``maxlength``. ``FileField`` A file-upload field. @@ -369,8 +376,8 @@ Here are all available field types: ``SlugField`` "Slug" is a newspaper term. A slug is a short label for something, - containing only letters, numbers and underscores. They're generally used in - URLs. + containing only letters, numbers, underscores or hyphens. They're generally + used in URLs. Implies ``maxlength=50`` and ``db_index=True``. diff --git a/docs/templates.txt b/docs/templates.txt index 5c52371c8a..e543b59763 100644 --- a/docs/templates.txt +++ b/docs/templates.txt @@ -446,9 +446,9 @@ Built-in tag reference