# -*- coding: utf-8 -*-
+import datetime
+
from django.utils.encoding import smart_unicode
from django.template import Library
from django.utils.http import urlencode
'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 = []
'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