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/views.py
1 1
from collections import defaultdict
2 2
from datetime import date
3 3

  
4
from dateutil.relativedelta import relativedelta
5

  
4 6
from django.http import HttpResponseRedirect, Http404
5 7
from django.db.models import Q
6 8
from django.contrib.auth.models import User
......
181 183
    success_url = '../'
182 184
    template_name = 'personnes/worker_holidays_update.html'
183 185

  
186
    def get_success_url(self):
187
        return self.success_url
188

  
189

  
184 190
worker_listing = WorkerView.as_view()
185 191
worker_new = cbv.CreateView.as_view(model=models.Worker,
186 192
        template_name='calebasse/simple-form.html',
......
191 197
worker_delete = cbv.DeleteView.as_view(model=models.Worker,
192 198
        template_name='calebasse/simple-form.html',
193 199
        success_url='../')
200

  
201

  
202
class HolidayView(cbv.TemplateView):
203
    months = 3
204
    template_name='personnes/holidays.html'
205

  
206
    def get_form(self):
207
        return forms.HolidaySearchForm(data=self.request.GET)
208

  
209
    def get_context_data(self, **kwargs):
210
        ctx = super(HolidayView, self).get_context_data(**kwargs)
211
        end_date = date.today() + relativedelta(months=self.months)
212
        qs = models.Holiday.objects.for_service_workers(self.service)
213
        today = date.today()
214
        future_qs = qs.filter(start_date__gt=today,
215
                start_date__lte=end_date)
216
        annual_qs = models.Holiday.objects.for_service(self.service)
217
        current_qs = qs.filter(start_date__lte=today)
218
        form = self.get_form()
219
        if form.is_valid() and form.cleaned_data.get('start_date'):
220
            cleaned_data = form.cleaned_data
221
            start_date = cleaned_data['start_date']
222
            end_date = cleaned_data['end_date']
223
            q = Q(start_date__gte=start_date, start_date__lte=end_date)
224
            q |= Q(end_date__gte=start_date, end_date__lte=end_date)
225
            future_qs = models.Holiday.objects.filter(q) \
226
                    .filter(worker__services=self.service)
227
            annual_qs = annual_qs.filter(q)
228
            current_qs = []
229
        ctx['current_holidays'] = current_qs
230
        future_holidays = defaultdict(lambda:[])
231
        for holiday in future_qs:
232
            key = (holiday.start_date.year, holiday.start_date.month, holiday.start_date.strftime('%B'))
233
            future_holidays[key].append(holiday)
234
        ctx['future_holidays'] = [ { 
235
            'date': date(day=1, month=key[1], year=key[0]),
236
            'holidays': future_holidays[key] 
237
          } for key in sorted(future_holidays.keys()) ]
238
        ctx['annual_holidays'] = annual_qs
239
        ctx['search_form'] = form
240
        return ctx
241

  
242

  
243
holiday_listing = HolidayView.as_view()
244

  
245

  
246
class YearlyHolidayUpdateView(cbv.FormView):
247
    form_class = forms.YearlyHolidayFormSet
248
    template_name = 'personnes/yearly_holiday_update.html'
249

  
250
    def get_success_url(self):
251
        return '../'
252

  
253
    def get_form_kwargs(self):
254
        kwargs = super(YearlyHolidayUpdateView, self).get_form_kwargs()
255
        qs = models.Holiday.objects.for_service(self.service)
256
        kwargs['queryset'] = qs
257
        initial = [ { 'for_all_services': o.service is None } for o in qs ]
258
        kwargs['initial'] = initial
259
        return kwargs
260

  
261
    def form_valid(self, form):
262
        instances = form.save(commit=False)
263
        for instance in instances:
264
            if instance.for_all_services:
265
                instance.service = None
266
            else:
267
                instance.service = self.service
268
            instance.save()
269
        return HttpResponseRedirect('')
270

  
271

  
272
yearly_holiday_update = YearlyHolidayUpdateView.as_view()

Also available in: Unified diff