From 39e96bc53d656d1c9434852c12ea59c585af5375 Mon Sep 17 00:00:00 2001 From: Yoann Pietri Date: Sat, 12 May 2018 21:21:00 +0200 Subject: [PATCH] Datetimepicker pour les bans et whitelists --- CHANGELOG.md | 7 +++ install_re2o.sh | 4 ++ re2o/settings.py | 1 + users/forms.py | 5 +- users/templates/users/datetimepicker.html | 28 ++++++++++ users/widgets.py | 66 ++++++++++++++++------- 6 files changed, 88 insertions(+), 23 deletions(-) create mode 100644 CHANGELOG.md create mode 100644 users/templates/users/datetimepicker.html diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000..533e3cfb --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,7 @@ +Datepicker +======= +Install libjs-jquery +Install libjs-jquery-ui +Install libjs-jquery-timepicker +Install (if not) javascript-common +Enable (if not) javascript-common conf diff --git a/install_re2o.sh b/install_re2o.sh index 3f627717..fd9fad59 100755 --- a/install_re2o.sh +++ b/install_re2o.sh @@ -429,6 +429,10 @@ end=$(dialog --clear \ 2>&1 >/dev/tty) } +apt install libjs-jquery +apt install libjs-jquery-ui +apt install libjs-jquery-timepicker + main_function() { if [ ! -z "$1" ] then diff --git a/re2o/settings.py b/re2o/settings.py index 52606b6d..e035847c 100644 --- a/re2o/settings.py +++ b/re2o/settings.py @@ -159,6 +159,7 @@ BOOTSTRAP_BASE_URL = '/static/bootstrap/' # Use only absolute paths with '/' delimiters even on Windows STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static').replace('\\', '/'), + os.path.join(BASE_DIR, 'javascript').replace('\\', '/'), ) # Directory where the static files served by the server are stored STATIC_ROOT = os.path.join(BASE_DIR, 'static_files') diff --git a/users/forms.py b/users/forms.py index dcc54004..8c6f5db9 100644 --- a/users/forms.py +++ b/users/forms.py @@ -569,13 +569,12 @@ class BanForm(FormRevMixin, ModelForm): prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(BanForm, self).__init__(*args, prefix=prefix, **kwargs) self.fields['date_end'].label = 'Date de fin' - self.fields['date_end'].help_text = get_input_formats_help_text( - self.fields['date_end'].input_formats - ) + self.fields['date_end'].localize = False class Meta: model = Ban exclude = ['user'] + widgets = {'date_end':DateTimePicker} class WhitelistForm(FormRevMixin, ModelForm): diff --git a/users/templates/users/datetimepicker.html b/users/templates/users/datetimepicker.html new file mode 100644 index 00000000..b1b3951f --- /dev/null +++ b/users/templates/users/datetimepicker.html @@ -0,0 +1,28 @@ +{% load static %} + + + + + + + diff --git a/users/widgets.py b/users/widgets.py index 59208278..78bb25f1 100644 --- a/users/widgets.py +++ b/users/widgets.py @@ -1,23 +1,49 @@ -from django.forms.widgets import Input -from django.forms.utils import flatatt -from django.utils.safestring import mark_safe -from django.template import Context, Template -from django.template.loader import get_template +from django.forms.widgets import Input +from django.forms.utils import flatatt +from django.utils.safestring import mark_safe +from django.template import Context, Template +from django.template.loader import get_template +from django.conf import settings +from django.utils.translation import ugettext_lazy as _, get_language_bidi +from django.utils.dates import ( + WEEKDAYS, + WEEKDAYS_ABBR, + MONTHS, + MONTHS_3, + MONTHS_AP, + MONTHS_ALT +) +def list2str(str_iterable): + """ + Utility function to return a string representing a list of string + + :params str_iterable: An iterable object where each element is of type str + :returns: A representation of the iterable as a list (e.g '["a", "b"]') + """ + return '["' + '", "'.join(str_iterable) + '"]' + class DateTimePicker(Input): - def render(self, name, value, attrs=None): - super().render(name, value, attrs) + is_localized = False + def render(self, name, value, attrs=None): + super().render(name, value, attrs) flat_attrs = flatatt(attrs) - html = '''{% load static %}''' - html += ''' - '''%{'attrs':flat_attrs, 'id':attrs['id']} - template = Template(html) - context = Context({}) - return template.render(context) + context = Context({ + 'attrs': flat_attrs, + 'id': attrs['id'], + 'closeText': _("Close"), + 'currentText': _("Today"), + 'dayNames': mark_safe(list2str((str(item[1]) for item in WEEKDAYS.items()))), + 'dayNamesMin': mark_safe(list2str((str(item[1]) for item in WEEKDAYS_ABBR.items()))), + 'dayNamesShort': mark_safe(list2str((str(item[1]) for item in WEEKDAYS_ABBR.items()))), + 'firstDay': mark_safe('"' + str(WEEKDAYS[settings.FIRST_DAY_OF_WEEK]) + '"'), + 'isRTL': str(get_language_bidi()).lower(), + 'monthNames': mark_safe(list2str((str(item[1]) for item in MONTHS.items()))), + 'monthNamesShort': mark_safe(list2str((str(item[1]) for item in MONTHS_3.items()))), + 'nextText': mark_safe('"' + str(_('Next')) + '"'), + 'prevText': mark_safe('"' + str(_('Previous')) + '"'), + 'weekHeader': mark_safe('"' + str(_('Wk')) + '"' ), + }) + template = get_template('users/datetimepicker.html') + return template.render(context) +