diff --git a/re2o/utils.py b/re2o/utils.py index 5a4f9400..75304369 100644 --- a/re2o/utils.py +++ b/re2o/utils.py @@ -44,6 +44,49 @@ from cotisations.models import Cotisation, Facture, Vente from machines.models import Interface, Machine from users.models import Adherent, User, Ban, Whitelist +# Mapping of srtftime format for better understanding +# https://docs.python.org/3.6/library/datetime.html#strftime-strptime-behavior +datetime_mapping={ + '%a': '%a', + '%A': '%A', + '%w': '%w', + '%d': 'dd', + '%b': '%b', + '%B': '%B', + '%m': 'mm', + '%y': 'yy', + '%Y': 'yyyy', + '%H': 'HH', + '%I': 'HH(12h)', + '%p': 'AMPM', + '%M': 'MM', + '%S': 'SS', + '%f': 'µµ', + '%z': 'UTC(+/-HHMM)', + '%Z': 'UTC(TZ)', + '%j': '%j', + '%U': 'ww', + '%W': 'ww', + '%c': '%c', + '%x': '%x', + '%X': '%X', + '%%': '%%', +} + + +def convert_datetime_format(format): + i=0 + new_format = "" + while i < len(format): + if format[i] == '%': + char = format[i:i+2] + new_format += datetime_mapping.get(char, char) + i += 2 + else: + new_format += format[i] + i += 1 + return new_format + def all_adherent(search_time=None): """ Fonction renvoyant tous les users adherents. Optimisee pour n'est @@ -318,3 +361,19 @@ def remove_user_room(room): return user.room = None user.save() + + +def get_input_formats_help_text(input_formats): + """Returns a help text about the possible input formats""" + if len(input_formats) > 1: + help_text_template="Format: {main} {more}" + else: + help_text_template="Format: {main}" + more_text_template="" + help_text = help_text_template.format( + main=convert_datetime_format(input_formats[0]), + more=more_text_template.format( + '\n'.join(map(convert_datetime_format, input_formats)) + ) + ) + return help_text diff --git a/search/forms.py b/search/forms.py index b0668ef9..8cdb1cc1 100644 --- a/search/forms.py +++ b/search/forms.py @@ -26,6 +26,7 @@ from __future__ import unicode_literals from django import forms from django.forms import Form +from re2o.utils import get_input_formats_help_text CHOICES_USER = ( ('0', 'Actifs'), @@ -91,12 +92,17 @@ class SearchFormPlus(Form): s = forms.DateField( required=False, label="Date de début", - help_text='DD/MM/YYYY', - input_formats=['%d/%m/%Y'] ) e = forms.DateField( required=False, - help_text='DD/MM/YYYY', - input_formats=['%d/%m/%Y'], label="Date de fin" ) + + def __init__(self, *args, **kwargs): + super(SearchFormPlus, self).__init__(*args, **kwargs) + self.fields['s'].help_text = get_input_formats_help_text( + self.fields['s'].input_formats + ) + self.fields['e'].help_text = get_input_formats_help_text( + self.fields['e'].input_formats + ) diff --git a/users/forms.py b/users/forms.py index 383ef9e8..03571fc3 100644 --- a/users/forms.py +++ b/users/forms.py @@ -41,7 +41,7 @@ from django.utils import timezone from django.contrib.auth.models import Group, Permission from preferences.models import OptionalUser -from re2o.utils import remove_user_room +from re2o.utils import remove_user_room, get_input_formats_help_text from re2o.mixins import FormRevMixin from re2o.field_permissions import FieldPermissionFormMixin @@ -565,6 +565,9 @@ 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 + ) class Meta: model = Ban @@ -577,6 +580,9 @@ class WhitelistForm(FormRevMixin, ModelForm): prefix = kwargs.pop('prefix', self.Meta.model.__name__) super(WhitelistForm, 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 + ) class Meta: model = Whitelist diff --git a/users/migrations/0072_auto_20180426_2021.py b/users/migrations/0072_auto_20180426_2021.py new file mode 100644 index 00000000..f60103e0 --- /dev/null +++ b/users/migrations/0072_auto_20180426_2021.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2018-04-26 18:21 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0071_auto_20180415_1252'), + ] + + operations = [ + migrations.AlterField( + model_name='ban', + name='date_end', + field=models.DateTimeField(), + ), + migrations.AlterField( + model_name='whitelist', + name='date_end', + field=models.DateTimeField(), + ), + ] diff --git a/users/models.py b/users/models.py index f7bfc128..0b89986e 100644 --- a/users/models.py +++ b/users/models.py @@ -1229,7 +1229,7 @@ class Ban(RevMixin, AclMixin, models.Model): user = models.ForeignKey('User', on_delete=models.PROTECT) raison = models.CharField(max_length=255) date_start = models.DateTimeField(auto_now_add=True) - date_end = models.DateTimeField(help_text='%d/%m/%y %H:%M:%S') + date_end = models.DateTimeField() state = models.IntegerField(choices=STATES, default=STATE_HARD) class Meta: @@ -1314,7 +1314,7 @@ class Whitelist(RevMixin, AclMixin, models.Model): user = models.ForeignKey('User', on_delete=models.PROTECT) raison = models.CharField(max_length=255) date_start = models.DateTimeField(auto_now_add=True) - date_end = models.DateTimeField(help_text='%d/%m/%y %H:%M:%S') + date_end = models.DateTimeField() class Meta: permissions = (