mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-11-24 20:33:11 +00:00
Take locale datetime formats into account in help text
This commit is contained in:
parent
52a35e523d
commit
090c838381
4 changed files with 78 additions and 7 deletions
|
@ -44,6 +44,49 @@ from cotisations.models import Cotisation, Facture, Vente
|
||||||
from machines.models import Interface, Machine
|
from machines.models import Interface, Machine
|
||||||
from users.models import Adherent, User, Ban, Whitelist
|
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):
|
def all_adherent(search_time=None):
|
||||||
""" Fonction renvoyant tous les users adherents. Optimisee pour n'est
|
""" Fonction renvoyant tous les users adherents. Optimisee pour n'est
|
||||||
|
@ -318,3 +361,19 @@ def remove_user_room(room):
|
||||||
return
|
return
|
||||||
user.room = None
|
user.room = None
|
||||||
user.save()
|
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="<i class=\"fa fa-question-circle\" title=\"{}\"></i>"
|
||||||
|
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
|
||||||
|
|
|
@ -26,6 +26,7 @@ from __future__ import unicode_literals
|
||||||
|
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.forms import Form
|
from django.forms import Form
|
||||||
|
from re2o.utils import get_input_formats_help_text
|
||||||
|
|
||||||
CHOICES_USER = (
|
CHOICES_USER = (
|
||||||
('0', 'Actifs'),
|
('0', 'Actifs'),
|
||||||
|
@ -91,12 +92,17 @@ class SearchFormPlus(Form):
|
||||||
s = forms.DateField(
|
s = forms.DateField(
|
||||||
required=False,
|
required=False,
|
||||||
label="Date de début",
|
label="Date de début",
|
||||||
help_text='DD/MM/YYYY',
|
|
||||||
input_formats=['%d/%m/%Y']
|
|
||||||
)
|
)
|
||||||
e = forms.DateField(
|
e = forms.DateField(
|
||||||
required=False,
|
required=False,
|
||||||
help_text='DD/MM/YYYY',
|
|
||||||
input_formats=['%d/%m/%Y'],
|
|
||||||
label="Date de fin"
|
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
|
||||||
|
)
|
||||||
|
|
|
@ -41,7 +41,7 @@ from django.utils import timezone
|
||||||
from django.contrib.auth.models import Group, Permission
|
from django.contrib.auth.models import Group, Permission
|
||||||
|
|
||||||
from preferences.models import OptionalUser
|
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.mixins import FormRevMixin
|
||||||
from re2o.field_permissions import FieldPermissionFormMixin
|
from re2o.field_permissions import FieldPermissionFormMixin
|
||||||
|
|
||||||
|
@ -558,6 +558,9 @@ class BanForm(FormRevMixin, ModelForm):
|
||||||
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
|
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
|
||||||
super(BanForm, self).__init__(*args, prefix=prefix, **kwargs)
|
super(BanForm, self).__init__(*args, prefix=prefix, **kwargs)
|
||||||
self.fields['date_end'].label = 'Date de fin'
|
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:
|
class Meta:
|
||||||
model = Ban
|
model = Ban
|
||||||
|
@ -570,6 +573,9 @@ class WhitelistForm(FormRevMixin, ModelForm):
|
||||||
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
|
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
|
||||||
super(WhitelistForm, self).__init__(*args, prefix=prefix, **kwargs)
|
super(WhitelistForm, self).__init__(*args, prefix=prefix, **kwargs)
|
||||||
self.fields['date_end'].label = 'Date de fin'
|
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:
|
class Meta:
|
||||||
model = Whitelist
|
model = Whitelist
|
||||||
|
|
|
@ -1229,7 +1229,7 @@ class Ban(RevMixin, AclMixin, models.Model):
|
||||||
user = models.ForeignKey('User', on_delete=models.PROTECT)
|
user = models.ForeignKey('User', on_delete=models.PROTECT)
|
||||||
raison = models.CharField(max_length=255)
|
raison = models.CharField(max_length=255)
|
||||||
date_start = models.DateTimeField(auto_now_add=True)
|
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)
|
state = models.IntegerField(choices=STATES, default=STATE_HARD)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
@ -1314,7 +1314,7 @@ class Whitelist(RevMixin, AclMixin, models.Model):
|
||||||
user = models.ForeignKey('User', on_delete=models.PROTECT)
|
user = models.ForeignKey('User', on_delete=models.PROTECT)
|
||||||
raison = models.CharField(max_length=255)
|
raison = models.CharField(max_length=255)
|
||||||
date_start = models.DateTimeField(auto_now_add=True)
|
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:
|
class Meta:
|
||||||
permissions = (
|
permissions = (
|
||||||
|
|
Loading…
Reference in a new issue