Project

General

Profile

« Previous | Next » 

Revision 53647eb3

Added by Serghei Mihai over 10 years ago

absences management refactored

View differences:

calebasse/personnes/views.py
5 5

  
6 6
from dateutil.relativedelta import relativedelta
7 7

  
8
from django.http import HttpResponseRedirect, Http404
8
from django.http import HttpResponseRedirect, Http404, HttpResponse
9 9
from django.db.models import Q
10 10
from django.contrib.auth.models import User
11 11
from django.contrib import messages
12 12
from django.core.urlresolvers import reverse
13 13
from django.utils.dateformat import format as date_format
14
from django.views.decorators.csrf import csrf_exempt
14 15

  
15 16
from calebasse import cbv, models as cb_models
16 17
from calebasse.ressources.models import Service
17 18

  
19
import json
20

  
18 21
from calebasse.decorators import super_user_only
19 22

  
20 23
import forms
......
322 325

  
323 326
group_holiday_update = GroupHolidayUpdateView.as_view()
324 327

  
328
class HolidayManagement(object):
329
    def render_to_json(self, id, context, err = 0, **kwargs):
330
        data = {'err': err, 'id': id, 'content': context}
331
        response = json.dumps(data)
332
        kwargs['content_type'] = 'application/json'
333
        return HttpResponse(response, **kwargs)
334

  
335
    def form_valid(self, form):
336
        form.save()
337
        if self.request.is_ajax:
338
            instance = form.instance
339
            status = ''
340
            if instance.is_current() and instance.service:
341
                status = u'congés annuels, en cours'
342
            elif instance.is_current():
343
                status = u'en cours'
344
            elif instance.service:
345
                status = u'congés annuels'
346

  
347
            context = (('period', '%s' % instance),
348
                       ('status', status),
349
                       ('type', '%s' % instance.holiday_type),
350
                       ('comment', instance.comment)
351
                       )
352
            return self.render_to_json(instance.id, context)
353
        return super(HolidayManagement, self).form_valid(form)
354

  
355
class HolidayCreateView(HolidayManagement, cbv.ServiceFormMixin,
356
                        cbv.CreateView):
357
    model = models.Holiday
358
    form_class = forms.HolidayForm
359

  
360
    def get_form_kwargs(self, *args, **kwargs):
361
        kwargs = super(HolidayCreateView, self).get_form_kwargs(*args, **kwargs)
362
        worker = models.Worker.objects.get(pk = self.kwargs['worker_pk'])
363
        kwargs['initial'] = {'service': self.service, 'worker': worker}
364
        del kwargs['service']
365
        return kwargs
366

  
367
create_holiday = HolidayCreateView.as_view()
368

  
369
class EditHolidayView(HolidayManagement, cbv.FormView):
370
    template_name = 'personnes/holiday_update_form.html'
371
    form_class = forms.HolidayForm
372
    model = models.Holiday
373

  
374
    def get_form_kwargs(self):
375
        kwargs = super(EditHolidayView, self).get_form_kwargs()
376
        kwargs['instance'] = self.model.objects.get(pk = self.kwargs['pk'])
377
        return kwargs
378

  
379
    def post(self, request, *args, **kwargs):
380
        worker = models.Worker.objects.get(pk = self.kwargs['worker_pk'])
381
        obj = self.model.objects.for_worker(worker).get(pk = self.kwargs['pk'])
382
        form = self.form_class(request.POST, instance = obj)
383
        if form.is_valid():
384
            return self.form_valid(form)
385
        else:
386
            return self.form_invalid(form)
387

  
388

  
389
edit_holiday = EditHolidayView.as_view()
390

  
391
class DeleteHolidayView(cbv.DeleteView):
392
    model = models.Holiday
393
    template_name = 'personnes/holiday_update_form.html'
394

  
395
    def post(self, request, *args, **kwargs):
396
        response = HttpResponse('', content_type = 'application/json')
397
        context = {'err': 0, }
398
        try:
399
            worker = models.Worker.objects.get(pk = self.kwargs['worker_pk'])
400
            self.model.objects.for_worker(worker).get(pk = self.kwargs['pk']).delete()
401
        except Exception, e:
402
            context['error': e.message]
403
        response.content = json.dumps(context)
404
        return response
405

  
406
delete_holiday = DeleteHolidayView.as_view()
325 407

  
326 408
#user_delete = UserCreateView.as_view()
327 409

  

Also available in: Unified diff