Commit a9123809 authored by fred's avatar fred

fix date options for absence dialog for non-staff users

parent c4d00c5f
...@@ -307,17 +307,20 @@ class NewsItemForm(forms.ModelForm): ...@@ -307,17 +307,20 @@ class NewsItemForm(forms.ModelForm):
class AbsenceDateTimeWidget(forms.Select): class AbsenceDateTimeWidget(forms.Select):
def render_options(self, choices, selected_choices): def __init__(self, emission, *args, **kwargs):
self.choices = [] super().__init__(*args, **kwargs)
self.choices = list(self.options(emission))
def options(self, emission):
dates = [] dates = []
for i in range(4): for i in range(4):
if dates: if dates:
since = dates[-1] + datetime.timedelta(minutes=10) since = dates[-1] + datetime.timedelta(minutes=10)
else: else:
since = None since = None
dates.append(self.emission.get_next_planned_date(since)) dates.append(emission.get_next_planned_date(since))
choices = [(x, x.strftime('%d/%m/%Y %H:%M')) for x in dates] for date in dates:
return super(AbsenceDateTimeWidget, self).render_options(choices, selected_choices) yield (date, date.strftime('%d/%m/%Y %H:%M'))
class AbsenceForm(forms.ModelForm): class AbsenceForm(forms.ModelForm):
...@@ -326,7 +329,6 @@ class AbsenceForm(forms.ModelForm): ...@@ -326,7 +329,6 @@ class AbsenceForm(forms.ModelForm):
exclude = () exclude = ()
widgets = { widgets = {
'emission': forms.HiddenInput(), 'emission': forms.HiddenInput(),
'datetime': AbsenceDateTimeWidget(),
} }
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
...@@ -338,6 +340,8 @@ class AbsenceForm(forms.ModelForm): ...@@ -338,6 +340,8 @@ class AbsenceForm(forms.ModelForm):
date = emission.get_next_planned_date() date = emission.get_next_planned_date()
self.fields['datetime'].initial = date self.fields['datetime'].initial = date
self.fields['datetime'].widget = DateTimeWidget() self.fields['datetime'].widget = DateTimeWidget()
else:
self.fields['datetime'].widget = AbsenceDateTimeWidget(emission)
def save(self, commit=True): def save(self, commit=True):
t = super(AbsenceForm, self).save(commit=commit) t = super(AbsenceForm, self).save(commit=commit)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment