mirror of
https://github.com/wagtail/wagtail.git
synced 2024-12-01 11:41:20 +01:00
Merge branch 'master' of https://github.com/spapas/wagtail into spapas-master
This commit is contained in:
commit
21deb0112a
@ -14,6 +14,7 @@ from django.forms.models import fields_for_model
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured, ValidationError
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.conf import settings
|
||||
|
||||
from wagtail.wagtailcore.models import Page
|
||||
from wagtail.wagtailcore.util import camelcase_to_underscore
|
||||
@ -76,16 +77,77 @@ class FriendlyTimeField(forms.CharField):
|
||||
return datetime.time(hour=hour, minute=minute)
|
||||
else:
|
||||
raise ValidationError("Please type a valid time")
|
||||
|
||||
|
||||
class LocalizedDateInput(forms.DateInput):
|
||||
"""
|
||||
A custom DateInput widget that formats localized dates
|
||||
and adds class="friendly_date" to be picked up by jquery datepicker.
|
||||
"""
|
||||
def __init__(self, attrs=None):
|
||||
default_attrs = {'class': 'localized_date', 'localize':True}
|
||||
if attrs:
|
||||
default_attrs.update(attrs)
|
||||
|
||||
super(LocalizedDateInput, self).__init__(attrs=default_attrs)
|
||||
|
||||
|
||||
FORM_FIELD_OVERRIDES = {
|
||||
models.DateField: {'widget': FriendlyDateInput},
|
||||
models.TimeField: {'widget': FriendlyTimeInput, 'form_class': FriendlyTimeField},
|
||||
}
|
||||
class LocalizedTimeInput(forms.TimeInput):
|
||||
"""
|
||||
A custom TimeInput widget that formats dates as "5.30pm"
|
||||
and adds class="friendly_time" to be picked up by jquery timepicker.
|
||||
"""
|
||||
def __init__(self, attrs=None):
|
||||
default_attrs = {'class': 'localized_time'}
|
||||
if attrs:
|
||||
default_attrs.update(attrs)
|
||||
# Just use 24-hour format
|
||||
super(LocalizedTimeInput, self).__init__(attrs=default_attrs, format='%H:%M')
|
||||
|
||||
|
||||
class LocalizedTimeField(forms.CharField):
|
||||
def to_python(self, time_string):
|
||||
# Check if the string is blank
|
||||
if not time_string:
|
||||
return None
|
||||
|
||||
# Look for time in the string
|
||||
expr = re.compile("^(?P<hour>\d+)(?:(?:.|:)(?P<minute>\d+))?")
|
||||
match = expr.match(time_string.lower())
|
||||
if match:
|
||||
# Pull out values from string
|
||||
hour_string, minute_string= match.groups()
|
||||
|
||||
# Convert hours and minutes to integers
|
||||
hour = int(hour_string)
|
||||
if minute_string:
|
||||
minute = int(minute_string)
|
||||
else:
|
||||
minute = 0
|
||||
if hour>=24 or hour < 0 or minute >=60 or minute < 0:
|
||||
raise ValidationError("Please type a valid time")
|
||||
|
||||
return datetime.time(hour=hour, minute=minute)
|
||||
else:
|
||||
raise ValidationError("Please type a valid time")
|
||||
|
||||
|
||||
if hasattr(settings, 'USE_L10N') and settings.USE_L10N==True:
|
||||
FORM_FIELD_OVERRIDES = {
|
||||
models.DateField: {'widget': LocalizedDateInput},
|
||||
models.TimeField: {'widget': LocalizedTimeInput, 'form_class': LocalizedTimeField},
|
||||
}
|
||||
else: # Fall back to friendly date/time
|
||||
FORM_FIELD_OVERRIDES = {
|
||||
models.DateField: {'widget': FriendlyDateInput},
|
||||
models.TimeField: {'widget': FriendlyTimeInput, 'form_class': FriendlyTimeField},
|
||||
}
|
||||
|
||||
WIDGET_JS = {
|
||||
FriendlyDateInput: (lambda id: "initDateChooser(fixPrefix('%s'));" % id),
|
||||
FriendlyTimeInput: (lambda id: "initTimeChooser(fixPrefix('%s'));" % id),
|
||||
FriendlyDateInput: (lambda id: "initFriendlyDateChooser(fixPrefix('%s'));" % id),
|
||||
FriendlyTimeInput: (lambda id: "initFriendlyTimeChooser(fixPrefix('%s'));" % id),
|
||||
LocalizedDateInput: (lambda id: "initLocalizedDateChooser(fixPrefix('%s'));" % id),
|
||||
LocalizedTimeInput: (lambda id: "initLocalizedTimeChooser(fixPrefix('%s'));" % id),
|
||||
RichTextArea: (lambda id: "makeRichTextEditable(fixPrefix('%s'));" % id),
|
||||
TagWidget: (
|
||||
lambda id: "initTagField(fixPrefix('%s'), '%s');" % (
|
||||
|
@ -27,4 +27,4 @@ function createPageChooser(id, pageType, openAtParentId) {
|
||||
openAtParentId = null;
|
||||
chooserElement.addClass('blank');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -55,23 +55,54 @@ function initDateChoosers(context) {
|
||||
$('input.friendly_date', context).datepicker({
|
||||
dateFormat: 'd M yy', constrainInput: false, /* showOn: 'button', */ firstDay: 1
|
||||
});
|
||||
|
||||
if(window.overrideDateInputFormat && window.overrideDateInputFormat !='') {
|
||||
$('input.localized_date', context).datepicker({
|
||||
dateFormat: window.overrideDateInputFormat, constrainInput: false, /* showOn: 'button', */ firstDay: 1
|
||||
});
|
||||
} else {
|
||||
$('input.localized_date', context).datepicker({
|
||||
constrainInput: false, /* showOn: 'button', */ firstDay: 1
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
function initDateChooser(id) {
|
||||
function initFriendlyDateChooser(id) {
|
||||
$('#' + id).datepicker({
|
||||
dateFormat: 'd M yy', constrainInput: false, /* showOn: 'button', */ firstDay: 1
|
||||
});
|
||||
}
|
||||
function initLocalizedDateChooser(id) {
|
||||
if(window.overrideDateInputFormat && window.overrideDateInputFormat !='') {
|
||||
$('#' + id).datepicker({
|
||||
dateFormat: window.overrideDateInputFormat, constrainInput: false, /* showOn: 'button', */ firstDay: 1
|
||||
});
|
||||
} else {
|
||||
$('#' + id).datepicker({
|
||||
constrainInput: false, /* showOn: 'button', */ firstDay: 1
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function initTimeChoosers(context) {
|
||||
$('input.friendly_time', context).timepicker({
|
||||
timeFormat: 'g.ia'
|
||||
});
|
||||
$('input.localized_time', context).timepicker({
|
||||
timeFormat: 'H:i', maxTime: '23:59'
|
||||
});
|
||||
}
|
||||
function initTimeChooser(id) {
|
||||
function initFriendlyTimeChooser(id) {
|
||||
$('#' + id).timepicker({
|
||||
timeFormat: 'g.ia'
|
||||
});
|
||||
}
|
||||
function initLocalizedTimeChooser(id) {
|
||||
$('#' + id).timepicker({
|
||||
timeFormat: 'H:i', maxTime: '23:59'
|
||||
});
|
||||
}
|
||||
|
||||
function initTagField(id, autocompleteUrl) {
|
||||
$('#' + id).tagit({
|
||||
|
@ -33,4 +33,4 @@
|
||||
{% block content %}{% endblock %}
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
{% endblock %}
|
||||
|
@ -1,9 +1,11 @@
|
||||
{% load compress %}
|
||||
{% load localize %}
|
||||
|
||||
{% comment %}
|
||||
Javascript declarations to be included on the 'create page' and 'edit page' views
|
||||
{% endcomment %}
|
||||
|
||||
|
||||
{% compress js %}
|
||||
<script src="{{ STATIC_URL }}wagtailadmin/js/vendor/rangy-core.js"></script>
|
||||
<script src="{{ STATIC_URL }}wagtailadmin/js/vendor/hallo.js"></script>
|
||||
@ -31,6 +33,12 @@
|
||||
<script src="{{ STATIC_URL }}admin/js/urlify.js"></script>
|
||||
{% endcompress %}
|
||||
|
||||
{% comment %}
|
||||
We should put it outside of compress because it is a remote script. If we
|
||||
wanted to put it inside compress we should download each translation file
|
||||
and include it in wagtailadmin/js/vendor/
|
||||
{% endcomment %}
|
||||
{% get_localized_datepicker_js %}
|
||||
<script>
|
||||
window.chooserUrls = {
|
||||
'documentChooser': '{% url "wagtaildocs_chooser" %}',
|
||||
@ -39,7 +47,10 @@
|
||||
'pageChooser': '{% url "wagtailadmin_choose_page" %}',
|
||||
'snippetChooser': '{% url "wagtailsnippets_choose_generic" %}'
|
||||
};
|
||||
|
||||
|
||||
{% get_date_format_override as format_override %}
|
||||
window.overrideDateInputFormat ='{{ format_override }}';
|
||||
|
||||
(function() {
|
||||
function fixPrefix(str) {return str;}
|
||||
|
||||
|
@ -1,9 +1,9 @@
|
||||
<!doctype html>
|
||||
{% load compress %}
|
||||
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
|
||||
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
|
||||
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
|
||||
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
|
||||
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="{{ LANGUAGE_CODE|default:"en-gb" }}"> <![endif]-->
|
||||
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8" lang="{{ LANGUAGE_CODE|default:"en-gb" }}"> <![endif]-->
|
||||
<!--[if IE 8]> <html class="no-js lt-ie9" lang="{{ LANGUAGE_CODE|default:"en-gb" }}"> <![endif]-->
|
||||
<!--[if gt IE 8]><!--> <html class="no-js" lang="{{ LANGUAGE_CODE|default:"en-gb" }}"> <!--<![endif]-->
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
|
||||
|
48
wagtail/wagtailadmin/templatetags/localize.py
Normal file
48
wagtail/wagtailadmin/templatetags/localize.py
Normal file
@ -0,0 +1,48 @@
|
||||
from django import template
|
||||
from django.conf import settings
|
||||
from django.utils import formats
|
||||
from django.utils.translation import get_language
|
||||
|
||||
register = template.Library()
|
||||
|
||||
# For reasons unkown, the el (greek) locale in django/conf/locale/el/formats.py
|
||||
# *did not* contain a DATE_INPUT_FORMATS -- so it fell back to using the US
|
||||
# date format (mm/dd/yy) which is not the correct one for Greece (dd/mm/yy).
|
||||
# This means that if we used a localized datepicker django *won't* be able to
|
||||
# parse the dates! So a test here checks if DATE_INPUT_FORMATS is actually
|
||||
# defined in a format module. If yes then it will just return an empty string
|
||||
# so that the normal, localized date format from datepicker will be used.
|
||||
# If DATE_INPUT_FORMATS is not defined then it will return
|
||||
@register.assignment_tag
|
||||
def get_date_format_override():
|
||||
if hasattr(settings, 'USE_I18N') and settings.USE_I18N==True:
|
||||
|
||||
for m in formats.get_format_modules():
|
||||
if hasattr(m, 'DATE_INPUT_FORMATS'):
|
||||
return ''
|
||||
else: # fall back to the ISO to be sure date will be parsed
|
||||
return 'yy-mm-dd'
|
||||
else: # Fall back to ISO if I18N is *not* used
|
||||
return 'yy-mm-dd'
|
||||
|
||||
# Get the correct i18n + l10n settings for datepicker depending on current
|
||||
# thread language
|
||||
@register.simple_tag
|
||||
def get_localized_datepicker_js():
|
||||
if hasattr(settings, 'USE_I18N') and settings.USE_I18N==True and \
|
||||
hasattr(settings, 'USE_L10N') and settings.USE_L10N==True:
|
||||
|
||||
lang = get_language()
|
||||
|
||||
if '-' in lang:
|
||||
lang_parts = lang.split('-')
|
||||
lang = lang_parts[0].lower() +'-'+ lang_parts[1].upper()
|
||||
else:
|
||||
lang=lang.lower()
|
||||
return '<script src="//jquery-ui.googlecode.com/svn/tags/latest/ui/i18n/jquery.ui.datepicker-{0}.js"></script>'.format(
|
||||
lang
|
||||
)
|
||||
|
||||
else: # Don't write anything if we don't use I18N and L10N
|
||||
return ''
|
||||
|
Loading…
Reference in New Issue
Block a user