Rapport: Filtre par échéance
authorPA Parent <paparent@paparent.me>
Tue, 13 Dec 2011 15:20:11 +0000 (10:20 -0500)
committerPA Parent <paparent@paparent.me>
Tue, 13 Dec 2011 15:20:11 +0000 (10:20 -0500)
project/rh/templates/rh/rapports/contrats.html
project/rh/templatetags/rapports.py

index 5e8be41..9a478fc 100644 (file)
@@ -10,6 +10,7 @@
 {% filter_region_contrat %}
 {% filter_implantation_contrat %}
 {% filter_type_contrat %}
+{% filter_echeance_contrat %}
 </div>
 
 <table id="result_list">
index 5d26dfd..bd9ab10 100644 (file)
@@ -1,5 +1,7 @@
 # -*- coding: utf-8 -*-
 
+import datetime
+
 from django.utils.encoding import smart_unicode
 from django.template import Library
 from django.utils.http import urlencode
@@ -50,6 +52,12 @@ def filter_type_contrat(context):
             'choices': prepare_choices(TypeContrat.objects.values_list('id', 'nom'), 'type_contrat', context)}
 
 
+@register.inclusion_tag('admin/filter_select.html', takes_context=True)
+def filter_echeance_contrat(context):
+    return {'title': u"échéance",
+            'choices': prepare_choices_date('date_fin', context)}
+
+
 def get_query_string(request, new_params=None, remove=None):
     if new_params is None: new_params = {}
     if remove is None: remove = []
@@ -78,3 +86,33 @@ def prepare_choices(choices, query_param, context, remove=[]):
                        'query_string': get_query_string(request, {query_param: k}, remove),
                        'display': v})
     return result
+
+
+def prepare_choices_date(field_name, context, remove=[]):
+    request = context['request']
+    params = request.GET
+    field_generic = '%s__' % field_name
+    date_params = dict([(k, v) for k, v in params.items() if k.startswith(field_generic)])
+
+
+    today = datetime.date.today()
+    three_months = today + datetime.timedelta(days=3*30)
+    six_months = today + datetime.timedelta(days=6*30)
+    twelve_months = today + datetime.timedelta(days=12*30)
+
+    links = (
+        ('Tous', {}),
+        ('moins de 3 mois', {'%s__gte' % field_name: today.strftime('%Y-%m-%d'),
+                             '%s__lte' % field_name: three_months.strftime('%Y-%m-%d')}),
+        ('3 à 6 mois', {'%s__gte' % field_name: three_months.strftime('%Y-%m-%d'),
+                        '%s__lte' % field_name: six_months.strftime('%Y-%m-%d')}),
+        ('6 à 12 mois', {'%s__gte' % field_name: six_months.strftime('%Y-%m-%d'),
+                         '%s__lte' % field_name: twelve_months.strftime('%Y-%m-%d')}),
+    )
+
+    result = []
+    for title, param_dict in links:
+        result.append({'selected': date_params == param_dict,
+                       'query_string': get_query_string(request, param_dict, [field_generic]),
+                       'display': title})
+    return result