Project

General

Profile

« Previous | Next » 

Revision 53647eb3

Added by Serghei Mihai over 10 years ago

absences management refactored

View differences:

calebasse/personnes/forms.py
179 179
                'comment': forms.Textarea(attrs={'rows': 3}),
180 180
        }
181 181

  
182
class HolidayForm(forms.ModelForm):
183

  
184
    class Meta:
185
        model = Holiday
186
        widgets = {
187
            'comment': forms.Textarea(attrs = {'rows': 3, 'cols': 18}),
188
            'start_date': forms.TextInput(attrs = {'size': 10}),
189
            'end_date': forms.TextInput(attrs = {'size': 10}),
190
            }
191

  
192
    def clean(self):
193
        cleaned_data = super(HolidayForm, self).clean()
194
        if cleaned_data.get('start_date') and cleaned_data.get('end_date'):
195
            if cleaned_data['start_date'] > cleaned_data['end_date']:
196
                raise forms.ValidationError(u'La date de début doit être supérieure à la date de fin')
197
        return cleaned_data
198

  
182 199
HolidayFormSet = inlineformset_factory(
183 200
        Worker, Holiday,
184 201
        form=BaseHolidayForm,
calebasse/personnes/templates/personnes/base.html
2 2
{% load url from future %}
3 3

  
4 4
{% block title %}{{ block.super }} - Gestion des personnes {% endblock %}
5
{% block extrascripts %}
6
{{ block.super }}
7
    <script src="{{ STATIC_URL }}js/calebasse.personnes.js"></script>
8
{% endblock %}
5 9

  
6 10
{% block header %}
7 11
  {{ block.super }}
calebasse/personnes/templates/personnes/holiday_form.html
1
<form>
2
  {% csrf_token %}
3
  <table>
4
    <tr><td colspan=2>{{ form.non_field_errors }}</td></tr>
5
    <tr>
6
      <th>Date début</th><th>Date fin</th>
7
    </tr>
8
    <tr>
9
      <td id="start" data-before-selector="#end" class="datepicker" data-number-of-months="2">
10
        {{ form.start_date }} {{ form.start_date.errors }}
11
      </td>
12
      <td id="end" data-after-selector="#start" class="datepicker" data-number-of-months="2">
13
        {{ form.end_date }} {{ form.end_date.errors }}
14
      </td>
15
    </tr>
16
    <tr>
17
      <th>Heure début</th><th>Heure fin</th>
18
    </tr>
19
    <tr>
20
      <td>{{ form.start_time }} {{ form.start_time.errors }}</td>
21
      <td>{{ form.end_time }} {{ form.end_time.errors }}</td>
22
    </tr>
23
    <tr>
24
      <th>Type</<th><th>Commentaire</th>
25
    </tr>
26
    <tr>
27
      <td>{{ form.holiday_type }} {{ form.holiday_type.errors }}</td>
28
      <td>{{ form.comment }} {{ form.comment.errors }}</td>
29
    </tr>
30
  </table>
31
</form>
calebasse/personnes/templates/personnes/holiday_update_form.html
1
<form>
2
  {% csrf_token %}
3
  {% if form %}
4
  <table>
5
    <tr><td colspan=2>{{ form.non_field_errors }}</td></tr>
6
    <tr>
7
      <th>Date début</th><th>Date fin</th>
8
    </tr>
9
    <tr>
10
      <td id="start" data-before-selector="#end" class="datepicker" data-number-of-months="2">
11
        {{ form.start_date }} {{ form.start_date.errors }}
12
      </td>
13
      <td id="end" data-after-selector="#start" class="datepicker" data-number-of-months="2">
14
        {{ form.end_date }} {{ form.end_date.errors }}
15
      </td>
16
    </tr>
17
    <tr>
18
      <th>Heure début</th><th>Heure fin</th>
19
    </tr>
20
    <tr>
21
      <td>{{ form.start_time }} {{ form.start_time.errors }}</td>
22
      <td>{{ form.end_time }} {{ form.end_time.errors }}</td>
23
    </tr>
24
    <tr>
25
      <th>Type</<th><th>Commentaire</th>
26
    </tr>
27
    <tr>
28
      <td>{{ form.holiday_type }} {{ form.holiday_type.errors }}</td>
29
      <td>{{ form.comment }} {{ form.comment.errors }}</td>
30
    </tr>
31
  </table>
32
  {% else %}
33
  Etez-vous sur de vouloir supprimer cette absence?
34
  {% endif %}
35
</form>
calebasse/personnes/templates/personnes/worker_update.html
13 13
                        click: function() { $(this).dialog("close"); } }]}
14 14
                );
15 15
            });
16

  
17
            $('button[data-action=delete]').click(function() {
18
                delete_holiday({{ object.id }}, $(this).data('id'));
19
            });
20

  
21
            $('button[data-action=edit]').click(function() {
22
                edit_holiday({{ object.id }}, $(this).data('id'));
23
            });
24

  
25
            $('button[data-action=new]').click(function() {
26
                add_holiday({{ object.id }});
27
            });
28

  
16 29
        });
17 30
</script>
18 31
{% endblock %}
......
99 112
      </tbody>
100 113
    </table>
101 114
  </div>
102
  <form action="holidays/" method="get">
103 115
    <div class="frame">
104 116
      <h3>Absences</h3>
105
      <button class="save icon-edit"></button>
106
      <ul>
117
      <!-- <button class="save icon-edit"></button> -->
118
      <button class="save icon-plus" data-action='new'></button>
119
      {% if holidays %}
120
      <ul id="holidays">
121
        <li>
122
          <ul class="caption">
123
            <li class="period">Période</li>
124
            <li class="status">Statut</li>
125
            <li class="type">Type</li>
126
            <li class="comment">Commentaire</li>
127
            <li class="actions">Actions</li>
128
          </ul>
129
        </li>
107 130
        {% for holiday in holidays %}
108
        <li>{{ holiday|capfirst }}
131
        <li id='{{ holiday.id }}'>
132
          <ul style="background: {% cycle '#fff' '#eee' %}">
133
            <li class="period">{{ holiday }}</li>
134
            <li class="status">
109 135
            {% if holiday.is_current and holiday.service %}
110
            (congés annuels, en cours)
136
            congés annuels, en cours
111 137
            {% elif holiday.is_current %}
112
            (en cours)
138
            en cours
113 139
            {% elif holiday.service %}
114
            (congés annuels)
115
            {% endif %}
116
            ({{ holiday.holiday_type }})
140
            congés annuels
141
            {% endif %}</li>
142
            <li class="type">
143
            {{ holiday.holiday_type }}
144
            </li>
145
            <li class="comment">
117 146
            {% if holiday.comment %}
118
            « {{ holiday.comment }} »
147
            {{ holiday.comment }}
119 148
            {% endif %}
149
            </li>
150
            <li class="actions">
151
              <button class="icon-edit" data-action='edit' data-id='{{ holiday.id }}'></button>
152
              <button class="icon-remove" data-action='delete' data-id='{{ holiday.id }}'></button>
153
            </li>
154
          </ul>
120 155
        </li>
121 156
        {% endfor %}
122 157
      </ul>
158
      {% endif %}
123 159
    </div>
124
  </form>
125 160
</div>
126 161
{% endblock %}
127 162

  
128 163
{% block dialogs %}
129
<div id="lundi-dlg" style="display: none;">
130
  <table>
131
    <tr>
132
      <th>Heure début</th> <th>Heure fin</th> <th>Date début</th> <th>Date fin</th> <th>Périodicité</th> <th>Services</th>
133
    </tr>
134
    <tr>
135
      <td><input value="13h30" size="8"/></td> <td><input value="17h30" size="8"/></td> <td><input value="20/04/2012" size="12"/></td> <td><input value="-" size="12"/></td> <td><select><option>Toutes les semaines</option></select></td> <td><select><option>CAMSP</option><option>CMPP</option><option>...</option><option>+</option></select></td> <td><button>-</button></td>
136
    </tr>
137
    <tr class="expired"> <td>13h30</td> <td>15h30</td> <td>2/01/2012</td> <td>14/04/2012</td> <td>2× par mois</td> <td>CAMSP</td> <td><button>déver.</button> </tr>
138
  </table>
139
  <button>+</button>
164
<div id="holiday-dlg" style="display: none;">
140 165
</div>
141 166
{% endblock %}
calebasse/personnes/urls.py
9 9
    url(r'^(?P<pk>\d+)/delete/$', 'user_delete'),
10 10
)
11 11

  
12
holiday_actions_paterns = patterns('calebasse.personnes.views',
13
    url(r'^(?P<pk>\d+)/editer/?$', 'edit_holiday', name = 'edit_holiday'),
14
    url(r'^(?P<pk>\d+)/supprimer/?$', 'delete_holiday', name = 'delete_holiday'),
15
    url(r'^ajouter/?$', 'create_holiday', name = 'create_holiday')
16
)
12 17

  
13 18
worker_patterns = patterns('calebasse.personnes.views',
14 19
    url(r'^$', 'worker_listing'),
15 20
    url(r'^new/$', 'worker_new'),
16 21
    url(r'^(?P<pk>\d+)/$', 'worker_update', name='worker_update'),
17 22
    url(r'^(?P<pk>\d+)/delete/$', 'worker_delete'),
18
    url(r'^(?P<pk>\d+)/holidays/$', 'worker_holidays_update',
19
        name='worker-holidays-update'),
20 23
    url(r'^(?P<pk>\d+)/(?P<weekday>\d)/$', 'worker_schedule_update'),
24
    url(r'^(?P<worker_pk>\d+)/holidays/', include(holiday_actions_paterns)),
21 25
)
22 26

  
23 27
holidays_patterns = patterns('calebasse.personnes.views',
24 28
    url(r'^$', 'holiday_listing'),
25 29
    url(r'^groupe/$', 'group_holiday_update',
26
        name='group-holiday-update'))
30
        name='group-holiday-update'),
31
    )
27 32

  
28 33
urlpatterns = patterns('calebasse.personnes.views',
29 34
    url(r'^$', 'homepage'),
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

  
calebasse/static/css/style.css
1401 1401
.icon-lock:before                 { content: "\f023"; }
1402 1402
.icon-remove-sign:before          { content: "\f057"; }
1403 1403
.icon-remove:before               { content: "\f00d"; }
1404
.icon-plus:before                { content: "\f067"; }
1404 1405
.icon-minus:before                { content: "\f068"; }
1405 1406
.icon-envelope:before             { content: "\f003"; }
1406 1407
.icon-comment:before              { content: "\f075"; }
......
1408 1409
.icon-user-space:before           { content: "\f007 "; }
1409 1410
.icon-circle-arrow-left:before    { content: "\f0a8"; }
1410 1411
.icon-circle-arrow-right:before   { content: "\f0a9"; }
1412
.icon-remove:before   { content: "\f00d"; }
1411 1413
.icon-warning-sign:before       { content: "\f071" }
1412 1414

  
1413 1415
/* periodic event search form */
......
1437 1439
#periodic-events-search-form label {
1438 1440
  display: block;
1439 1441
}
1442

  
1443
#holidays {
1444
    margin: .1em;
1445
    padding: .1em;
1446
}
1447

  
1448
#holidays li {
1449
    list-style-type: none;
1450
}
1451

  
1452
#holidays li li {
1453
    display: inline-block;
1454
    list-style-type: none;
1455
    margin: .2em 0;
1456
    padding: 0 .1em;
1457
}
1458

  
1459
li.period {
1460
    width: 20%;
1461
}
1462

  
1463
li.status, li.type, li.actions  {
1464
    width: 10%;
1465
}
1466

  
1467
li.comment {
1468
    width: 45%;
1469
}
1470

  
1471
.caption {
1472
    color: #fff;
1473
    background: #bbb;
1474
    font-weight: bold;
1475
}
1476

  
1477
#holidays ul {
1478
    margin: 0;
1479
    padding: 0 .5em;
1480
}
calebasse/static/js/calebasse.personnes.js
1
function action(worker, on, action, selector, original_color, highlight_color, params, on_success) {
2
    var url = '';
3
    if(on) {
4
        url = '/cmpp/personnes/gestion/' + worker + '/holidays/' + on + '/' + action + '/';
5
        $(selector).attr('style', 'background: ' + highlight_color);
6
    } else {
7
        url = '/cmpp/personnes/gestion/' + worker + '/holidays/' + action;
8
    }
9
    $("#holiday-dlg").load(url,
10
                           function() {
11
                               $(this).dialog({title: params.title,
12
                                               width: params.width,
13
                                               buttons: [{text: params.button_close,
14
                                                          click: function() {
15
                                                              $(this).dialog('close');
16
                                                              $(selector).attr('style', original_color);
17
                                                          }},{text: params.button_confirm,
18
                                                              click: function(){
19
                                                                  $.ajax({url: url,
20
                                                                          type: 'post',
21
                                                                          data: $('#holiday-dlg form').serialize()
22
                                                                         }).done(on_success)
23
                                                              }}]});
24
                           })
25
};
26

  
27
function add_holiday(worker) {
28
    params = {'title': 'Ajouter une absence', 'button_close': 'Fermer',
29
              'button_confirm': 'Ajouter', 'width': '500px'};
30

  
31
    on_success = function(response, status, xhr) {
32
        var content_type = xhr.getResponseHeader('content-type');
33
      if(content_type.indexOf('html') > -1) {
34
            $('#holiday-dlg form').html(response);
35
        } else {
36
            if(!response.err) {
37
                var result = response.content;
38
                var id = response.id;
39
                var li = $("<li id='" + id + "'></li>");
40
                var ul = $('<ul></ul>');
41
                $.each(result, function(i, e) {
42
                    console.log(e);
43
                    var item = $('<li class="' + e[0] + '">' + e[1] + '</li>');
44
                    ul.append(item);
45
                });
46
                var button_edit = $('<button class="icon-edit" data-action="edit" data-id="' + id + '"></button>');
47
                var button_delete = $('<button class="icon-remove" data-action="delete" data-id="' + id + '"></button>');
48
                var actions = $('<li class="actions"></li>');
49
                actions.append(button_edit, button_delete);
50
                ul.append(actions);
51
                li.append(ul);
52
                $("#holidays").append(li);
53
                $("#holiday-dlg").dialog("close");
54
            }
55
        }
56
    };
57
    action(worker, null, 'ajouter', null, null, null, params, on_success);
58
};
59

  
60
function delete_holiday(worker, holiday) {
61
    var selector = '#' + holiday + ' ul';
62
    var initial_color = $(selector).attr('style');
63
    var params = {'title': 'Supprimer une absence',
64
                  'button_close': 'Non', 'button_confirm': 'Oui',
65
                  'width': '450px'}
66

  
67
    on_success = function(response) {
68
        if(!response.err) {
69
            $('#' +  holiday).remove();
70
            $(selector).attr('style', initial_color);
71
            $("#holiday-dlg").dialog("close");
72
        }
73
    };
74
    action(worker, holiday, 'supprimer', selector, initial_color, '#f8b0b0', params, on_success);
75
};
76

  
77
function edit_holiday(worker, holiday) {
78
    var selector = '#' + holiday + ' ul';
79
    var initial_color = $(selector).attr('style');
80
    params = {'title': 'Éditer une absence',
81
              'button_close': 'Fermer', 'button_confirm': 'Mettre à jour',
82
              'width': '500px'}
83

  
84
    on_success = function(response, status, xhr) {
85
        var content_type = xhr.getResponseHeader('content-type');
86
        if(content_type.indexOf('html') > -1) {
87
            $('#holiday-dlg form').html(response);
88
        } else {
89
            if(!response.err) {
90
                $.each(response.content, function(i, e) {
91
                    $('#holidays #' + holiday + ' li.' + e[0]).html(e[1]);
92
                });
93
                $(selector).attr('style', initial_color);
94
                $("#holiday-dlg").dialog("close");
95
            }
96
        }}
97
    action(worker, holiday, 'editer', selector, initial_color,  '#af7', params, on_success);
98
};

Also available in: Unified diff