Project

General

Profile

« Previous | Next » 

Revision 84614733

Added by Benjamin Dauvergne over 12 years ago

personnes: rewrite holidays view (fixes #2308)

View differences:

calebasse/personnes/forms.py
182 182

  
183 183
    def clean(self):
184 184
        cleaned_data = super(HolidaySearchForm, self).clean()
185
        if cleaned_data.get('start_date') or cleaned_data.get('end_date'):
186
            if not cleaned_data.get('start_date') \
187
                   or not cleaned_data.get('end_date'):
188
                raise forms.ValidationError(u'Vous devez fournir une date de début et de fin')
185
        if cleaned_data.get('start_date') and cleaned_data.get('end_date'):
189 186
            if cleaned_data['start_date'] > cleaned_data['end_date']:
190 187
                raise forms.ValidationError(u'La date de début doit être supérieure à la date de fin')
191 188
        return cleaned_data
calebasse/personnes/models.py
356 356
                          |models.Q(service__isnull=True))
357 357

  
358 358
    def for_service_workers(self, service):
359
        return self.filter(worker__services=service)
359
        return self.filter(models.Q(worker__services=service)
360
                |models.Q(service=service)
361
                |models.Q(worker__isnull=True, service__isnull=True))
360 362

  
361 363
    def future(self):
362 364
        return self.filter(end_date__gte=date.today())
......
418 420
                ret += u" (jusqu'à {0})".format(time2french(self.end_time))
419 421
        return ret
420 422

  
421
    def to_interval(self, date):
423
    def to_interval(self, date=None):
422 424
        if date == self.start_date:
423 425
            start_time = self.start_time or datetime_time(8, 0)
424 426
        else:
calebasse/personnes/templates/personnes/holidays.html
14 14
  <h4>En cours</h4>
15 15

  
16 16
  <ul>
17
    {% for holiday in current_holidays %}
18
    <li><a href="{% url 'worker_update' service=service pk=holiday.worker.pk %}">
19
        <span class="lastname">{{ holiday.worker.last_name }}</span> {{holiday.worker.first_name }}</a>,
20
        {{ holiday }}</li>
17
    {% for holiday in current_holidays|dictsort:"worker" %}
18
      <li><a href="{{ holiday.url }}">{{ holiday.worker }} {{ holiday.holiday }}</a></li>
21 19
    {% endfor %}
22 20
  </ul>
23 21
{% endif %}
......
26 24
  <h4>À venir (jusqu'au {{ end_date|date:"d/m/Y" }})</h4>
27 25

  
28 26
  <table id="conges-a-venir">
29
    <thead>
30
      <tr>
31
        {% for month in future_holidays %}
32
          <th>{{ month.date|date:"F"|capfirst }}</th>
33
        {% endfor %}
34
      </tr>
35
    </thead>
36
    <tbody>
37
      <tr>
38
        {% for month in future_holidays %}
39
          <td>
40
            <ul>
41
              {% for holiday in month.holidays %}
42
              <li><a href="{% url 'worker_update' service=service pk=holiday.worker.pk %}">
43
                  <span class="lastname">{{ holiday.worker.last_name }}</span> {{holiday.worker.first_name }}</a>,
44
                  {{ holiday }}</li>
45
              {% endfor %}
46
            </ul>
47
          </td>
48
        {% endfor %}
49
      </tr>
50
    </tbody>
27
    {% for months in future_holidays %}
28
      <thead>
29
        <tr>
30
          {% for future in months %}
31
            <th>{{ future.month|capfirst }}</th>
32
          {% endfor %}
33
        </tr>
34
      </thead>
35
      <tbody>
36
        <tr>
37
          {% for future in months %}
38
            <td>
39
              <ul>
40
                {% for holiday in future.holidays|dictsort:"worker" %}
41
                  <li>
42
                    <a href="{{ holiday.url }}">
43
                      {{ holiday.worker }} {{ holiday.holiday }}
44
                    </a>
45
                  </li>
46
                {% endfor %}
47
              </ul>
48
            </td>
49
          {% endfor %}
50
        </tr>
51
      </tbody>
52
   {% endfor %}
51 53
  </table>
52 54
{% endif %}
53 55

  
......
68 70
  <form>
69 71
    {{ search_form.non_field_errors }}
70 72
    {{ search_form.start_date.errors }}
73
    {{ search_form.end_date.errors }}
71 74
    <p>Afficher la liste des congés pris entre
72 75
      <span id="start-date-datepicker" data-number-of-months="3" data-before-selector="#end-date-datepicker" class="datepicker">{{ search_form.start_date }}</span>
73 76
      et
calebasse/personnes/urls.py
15 15
    url(r'^new/$', 'worker_new'),
16 16
    url(r'^(?P<pk>\d+)/$', 'worker_update', name='worker_update'),
17 17
    url(r'^(?P<pk>\d+)/delete/$', 'worker_delete'),
18
    url(r'^(?P<pk>\d+)/holidays/$', 'worker_holidays_update'),
18
    url(r'^(?P<pk>\d+)/holidays/$', 'worker_holidays_update',
19
        name='worker-holidays-update'),
19 20
    url(r'^(?P<pk>\d+)/(?P<weekday>\d)/$', 'worker_schedule_update'),
20 21
)
21 22

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

  
26 28
urlpatterns = patterns('calebasse.personnes.views',
27 29
    url(r'^$', 'homepage'),
calebasse/personnes/views.py
1 1
# -*- coding: utf-8 -*-
2 2
from collections import defaultdict
3 3
from datetime import date
4
from interval import IntervalSet
4 5

  
5 6
from dateutil.relativedelta import relativedelta
6 7

  
......
8 9
from django.db.models import Q
9 10
from django.contrib.auth.models import User
10 11
from django.contrib import messages
12
from django.core.urlresolvers import reverse
11 13

  
12 14
from calebasse import cbv, models as cb_models
13 15
from calebasse.ressources.models import Service
......
227 229

  
228 230
    def get_context_data(self, **kwargs):
229 231
        ctx = super(HolidayView, self).get_context_data(**kwargs)
230
        end_date = date.today() + relativedelta(months=self.months)
231
        qs = models.Holiday.objects.future().select_related('worker')
232
        form = forms.HolidaySearchForm(data=self.request.GET)
233

  
232 234
        today = date.today()
233
        future_qs = qs.for_period(today, end_date).select_related('worker')
234
        group_qs = models.Holiday.objects.for_service(self.service).select_related()
235
        current_qs = qs.today()
236
        form = self.get_form()
237
        if form.is_valid() and form.cleaned_data.get('start_date'):
238
            cleaned_data = form.cleaned_data
239
            start_date = cleaned_data['start_date']
240
            end_date = cleaned_data['end_date']
241
            future_qs = models.Holiday.objects \
242
                    .for_period(start_date, end_date) \
243
                    .for_service_workers(self.service)
244
            group_qs = group_qs.for_period(start_date, end_date)
245
            current_qs = []
246
        ctx['end_date'] = end_date
247
        ctx['current_holidays'] = current_qs
248
        future_holidays = defaultdict(lambda:[])
249
        for holiday in future_qs:
250
            key = (holiday.start_date.year, holiday.start_date.month, holiday.start_date.strftime('%B'))
251
            future_holidays[key].append(holiday)
252
        ctx['future_holidays'] = [ {
253
            'date': date(day=1, month=key[1], year=key[0]),
254
            'holidays': future_holidays[key]
255
          } for key in sorted(future_holidays.keys()) ]
256
        ctx['group_holidays'] = group_qs.order_by('-start_date')
235
        filter_start_date = today
236
        filter_end_date = date.today() + relativedelta(months=self.months)
237

  
238
        if form.is_valid():
239
            if form.cleaned_data.get('start_date'):
240
                filter_start_date = form.cleaned_data.get('start_date')
241
            if form.cleaned_data.get('end_date'):
242
                filter_end_date = form.cleaned_data.get('end_date')
243

  
244
        workers = models.Worker.objects.filter(enabled=True)
245
        holidays = models.Holiday.objects \
246
                .filter(end_date__gte=filter_start_date,
247
                        start_date__lte=filter_end_date) \
248
                .select_related('worker', 'service')
249
        holiday_by_worker = defaultdict(lambda: [])
250
        all_holidays = holidays.filter(worker__isnull=True)
251

  
252
        for worker in workers:
253
            holiday_by_worker[worker] = list(all_holidays)
254

  
255
        for holiday in holidays.filter(worker__isnull=False):
256
            holiday_by_worker[holiday.worker].append(holiday)
257

  
258
        def holiday_url(holiday):
259
            if holiday.worker:
260
                return reverse('worker-holidays-update', kwargs=dict(
261
                    service=self.service.slug, pk=holiday.worker.pk))
262
            else:
263
                slug = holiday.service.slug if holiday.service else self.service.slug
264
                return reverse('group-holiday-update', kwargs=dict(
265
                    service=slug))
266

  
267
        currents = []
268
        futures = defaultdict(lambda: [])
269
        for worker, holidays in holiday_by_worker.iteritems():
270
            for holiday in holidays:
271
                url = holiday_url(holiday)
272
                holiday_tpl = dict(worker=worker.display_name, holiday=holiday, url=url)
273
                if holiday.start_date <= today <= holiday.end_date:
274
                    currents.append(holiday_tpl)
275
                start_date = max(holiday.start_date, filter_start_date)
276
                month_name = start_date.strftime('%B')
277
                key = start_date.year, start_date.month, month_name
278
                futures[key].append(holiday_tpl)
279

  
280
        future_holidays = []
281
        for key in sorted(futures.keys()):
282
            future_holidays.append(dict(
283
                month=key[2],
284
                holidays=futures[key]))
285
        future_holidays2 = []
286
        for i in range(0, len(future_holidays), 2):
287
            future_holidays2.append(future_holidays[i:i+2])
288

  
289
        ctx['end_date'] = filter_end_date
290
        ctx['current_holidays'] = currents
291
        ctx['future_holidays'] = future_holidays2
292
        ctx['group_holidays'] = all_holidays.order_by('-start_date')
257 293
        ctx['search_form'] = form
258 294
        return ctx
259 295

  

Also available in: Unified diff