Project

General

Profile

« Previous | Next » 

Revision 5b90c12e

Added by Serghei Mihai over 11 years ago

Group absences refactored

View differences:

calebasse/personnes/views.py
229 229
        success_url='../../')
230 230

  
231 231

  
232
class HolidayView(cbv.TemplateView):
232
class HolidayView(cbv.TemplateView, cbv.ServiceViewMixin):
233 233
    months = 3
234 234
    template_name='personnes/holidays.html'
235 235

  
......
267 267
        def holiday_url(holiday):
268 268
            if holiday.worker:
269 269
                return reverse('worker_update', kwargs=dict(
270
                    service=self.service.slug, pk=holiday.worker.pk))
270
                    service = ctx['service'], pk=holiday.worker.pk))
271 271
            else:
272
                slug = holiday.service.slug if holiday.service else self.service.slug
272
                slug = ctx['service']
273 273
                return reverse('group-holidays', kwargs=dict(
274 274
                    service=slug))
275 275

  
......
325 325

  
326 326
group_holiday_update = GroupHolidayUpdateView.as_view()
327 327

  
328
# ===================================================================
328
class HolidayManagement(object):
329
    """
330
    A class providing some methods for handling the absences management
331
    """
332

  
333
    def render_to_json(self, id, context, err = 0, **kwargs):
334
        data = {'err': err, 'id': id, 'content': context}
335
        response = json.dumps(data)
336
        kwargs['content_type'] = 'application/json'
337
        return HttpResponse(response, **kwargs)
338

  
339
    def update_post_data(self, request):
340
        worker = models.Worker.objects.get(pk = self.kwargs['worker_pk'])
341
        post = request.POST.copy()
342
        post.update({'worker': worker.id})
343
        return worker, post
344

  
345
    def form_valid(self, form):
346
        form.save()
347
        if self.request.is_ajax:
348
            instance = form.instance
349
            status = ''
350
            if instance.is_current() and instance.services.count():
351
                status = u'congés annuels, en cours'
352
            elif instance.is_current():
353
                status = u'en cours'
354
            elif instance.services.count():
355
                status = u'congés annuels'
356

  
357
            context = (('period', '%s' % instance),
358
                       ('status', status),
359
                       ('type', '%s' % instance.holiday_type),
360
                       ('comment', instance.comment)
361
                       )
362
            return self.render_to_json(instance.id, context)
363
        return super(HolidayManagement, self).form_valid(form)
364

  
365
    def group_form_valid(self, form):
366
        """
367
        Method handling the saving of group absences form
368
        """
369
        form.save()
370
        form.save_m2m()
371
        if self.request.is_ajax:
372
            instance = form.instance
373
            context = (('start_date', '%s' % instance.start_date),
374
                       ('end_date', '%s' % instance.end_date),
375
                       ('start_time',
376
                        instance.start_time and '%s' % instance.start_time or '---'),
377
                       ('end_time',
378
                        instance.end_time and '%s' % instance.end_time  or '---'),
379
                       ('type', '%s' % instance.holiday_type),
380
                       ('all',
381
                        instance.for_all_services() and '<span class="icon-ok"></span>' or ''),
382
                       ('comment',
383
                        instance.comment)
384
                       )
385
            return self.render_to_json(instance.id, context)
386
        return super(EditGroupHolidayView, self).form_valid(form)
329 387

  
330 388
class GroupHolidaysList(cbv.ListView):
331 389
    model = models.Holiday
332 390
    template_name = 'personnes/group_holidays_list.html'
333 391
    queryset = model.objects.future()
334 392

  
393
    def get_queryset(self, *args, **kwargs):
394
        qs = super(GroupHolidaysList, self).get_queryset(*args, **kwargs)
395
        qs = qs.for_service(self.service)
396
        return qs
397

  
335 398
group_holidays = GroupHolidaysList.as_view()
336 399

  
337
class CreateGroupHolidayView(cbv.CreateView):
400
class CreateGroupHolidayView(HolidayManagement, cbv.CreateView):
338 401
    form_class = forms.GroupHolidayForm
339 402
    model = models.Holiday
403
    template_name_suffix = '_group_form'
404

  
405
    def get_form_kwargs(self):
406
        return {'initial': {'services': Service.objects.all()}}
340 407

  
341 408
    def post(self, request, *args, **kwargs):
342
        # créer l'objet
343
        pass
409
        form = self.form_class(request.POST)
410
        if form.is_valid():
411
            return self.group_form_valid(form)
412

  
413
        return self.form_invalid(form)
414

  
344 415

  
345 416
create_group_holiday = CreateGroupHolidayView.as_view()
346 417

  
347
class EditGroupHolidayView(cbv.FormView):
418
class EditGroupHolidayView(HolidayManagement, cbv.FormView):
348 419
    template_name = 'personnes/group_holiday_update_form.html'
349 420
    form_class = forms.GroupHolidayForm
350 421
    model = models.Holiday
......
352 423
    def get_form_kwargs(self):
353 424
        kwargs = super(EditGroupHolidayView, self).get_form_kwargs()
354 425
        kwargs['instance'] = self.model.objects.get(pk = self.kwargs['pk'])
355
        print kwargs['instance'].service
356 426
        return kwargs
357 427

  
358 428
    def post(self, request, *args, **kwargs):
359
        return super(EditGroupHolidayView, self).post(request, *args, **kwargs)
429
        obj = self.model.objects.get(pk = self.kwargs['pk'])
430
        form = self.form_class(request.POST, instance = obj)
431
        if form.is_valid():
432
            return self.group_form_valid(form)
433

  
434
        return self.form_invalid(form)
360 435

  
361 436
edit_group_holiday = EditGroupHolidayView.as_view()
362 437

  
......
377 452

  
378 453
delete_group_holiday = DeleteGroupHolidayView.as_view()
379 454

  
380
class HolidayManagement(object):
381

  
382
    def render_to_json(self, id, context, err = 0, **kwargs):
383
        data = {'err': err, 'id': id, 'content': context}
384
        response = json.dumps(data)
385
        kwargs['content_type'] = 'application/json'
386
        return HttpResponse(response, **kwargs)
387

  
388
    def update_post_data(self, request):
389
        worker = models.Worker.objects.get(pk = self.kwargs['worker_pk'])
390
        post = request.POST.copy()
391
        post.update({'worker': worker.id})
392
        return worker, post
393

  
394
    def form_valid(self, form):
395
        form.save()
396
        if self.request.is_ajax:
397
            instance = form.instance
398
            status = ''
399
            if instance.is_current() and instance.service:
400
                status = u'congés annuels, en cours'
401
            elif instance.is_current():
402
                status = u'en cours'
403
            elif instance.service:
404
                status = u'congés annuels'
405

  
406
            context = (('period', '%s' % instance),
407
                       ('status', status),
408
                       ('type', '%s' % instance.holiday_type),
409
                       ('comment', instance.comment)
410
                       )
411
            return self.render_to_json(instance.id, context)
412
        return super(HolidayManagement, self).form_valid(form)
413

  
414 455
class HolidayCreateView(HolidayManagement, cbv.CreateView):
415 456
    model = models.Holiday
416 457
    form_class = forms.HolidayForm

Also available in: Unified diff