Project

General

Profile

« Previous | Next » 

Revision f69ae8a2

Added by Benjamin Dauvergne over 11 years ago

personnes: add views to manage service holidays

View differences:

calebasse/personnes/forms.py
1 1
# -*- coding: utf-8 -*-
2 2
from django import forms
3
from django.forms.models import inlineformset_factory, BaseInlineFormSet
3
from django.forms.models import (inlineformset_factory, modelformset_factory,
4
        BaseInlineFormSet)
4 5
from django.utils.translation import gettext_lazy as _
5 6
from django.contrib.auth.models import User
6 7

  
......
114 115
                'services': forms.CheckboxSelectMultiple,
115 116
        }
116 117

  
118

  
117 119
class BaseTimetableFormSet(BaseInlineFormSet):
118 120
    def __init__(self, weekday=None, *args, **kwargs):
119 121
        kwargs['queryset'] = kwargs.get('queryset', TimeTable.objects).filter(weekday=weekday)
120 122
        super(BaseTimetableFormSet, self).__init__(*args, **kwargs)
121 123

  
124

  
122 125
TimetableFormSet = inlineformset_factory(Worker, TimeTable,
123 126
        formset=BaseTimetableFormSet,
124 127
        fields=('start_time', 'end_time', 'start_date', 'end_date'))
......
126 129
HolidayFormSet = inlineformset_factory(
127 130
        Worker, Holiday,
128 131
        fields=('start_date', 'end_date', 'start_time', 'end_time'))
132

  
133
class HolidaySearchForm(forms.Form):
134
    start_date = forms.DateField(required=False)
135
    end_date = forms.DateField(required=False)
136

  
137
    def clean(self):
138
        cleaned_data = super(HolidaySearchForm, self).clean()
139
        if cleaned_data.get('start_date') or cleaned_data.get('end_date'):
140
            if not cleaned_data.get('start_date') \
141
                   or not cleaned_data.get('end_date'):
142
                raise forms.ValidationError(u'Vous devez fournir une date de début et de fin')
143
            if cleaned_data['start_date'] > cleaned_data['end_date']:
144
                raise forms.ValidationError(u'La date de début doit être supérieure à la date de fin')
145
        return cleaned_data
146

  
147
class YearlyHolidayForm(forms.ModelForm):
148
    for_all_services = forms.BooleanField(required=False)
149

  
150
    def save(self, commit=True):
151
        instance = super(YearlyHolidayForm, self).save(commit=commit)
152
        instance.for_all_services = self.cleaned_data.get('for_all_services', False)
153
        return instance
154

  
155
    class Meta:
156
        form = Holiday
157

  
158
YearlyHolidayFormSet = modelformset_factory(Holiday,
159
        can_delete=True,
160
        form=YearlyHolidayForm,
161
        fields=('start_date', 'end_date'))

Also available in: Unified diff