STATUT_FUTUR = 'Futur'
STATUT_INCONNU = 'Inconnu'
-class ChangeList(DjangoChangeList):
+
+class RechercheTemporelle(object):
+ model = None
+ params = None
+ prefixe_recherche_temporelle = ""
+ lookup_recherche_temporelle = {}
internal_fields = (KEY_ANNEE, KEY_DATE_DEBUT, KEY_DATE_FIN, KEY_STATUT, )
STATUT_CHOICES = (STATUT_ACTIF, STATUT_INACTIF, STATUT_FUTUR, STATUT_INCONNU )
-
- def __init__(self, *args, **kwargs):
- super(ChangeList, self).__init__(*args, **kwargs)
+
+ def __init__(self, request, model):
+ self.model = model
+ self.params = dict(request.GET.items())
def get_prefix(self):
- return getattr(self.model_admin, 'prefixe_recherche_temporelle', "")
+ klass = getattr(self, "model_admin", self)
+ return getattr(klass, 'prefixe_recherche_temporelle', "")
def get_annees(self):
prefix = self.get_prefix()
annees.sort(reverse=True)
return annees
-
def get_q_inconnu(self, prefix):
date_debut_nulle = Q(**{"%s%s__isnull" % (prefix, KEY_DATE_DEBUT) : True})
date_fin_nulle = Q(**{"%s%s__isnull" % (prefix, KEY_DATE_FIN) : True})
return q_range
+ def purge_params(self, lookup_params):
+ params = lookup_params.copy()
+ for key, value in lookup_params.items():
+ # ignorer les param GET pour la recherche temporelle
+ if len([i for i in self.internal_fields if key.endswith(i)]) > 0:
+ del params[key]
+ self.lookup_recherche_temporelle[key] = value
+ continue
+ return params
+
+ def filter_temporel(self, qs):
+ q = Q()
+ prefix = self.get_prefix()
+ borne_gauche = None
+ borne_droite = None
+ for k, v in self.lookup_recherche_temporelle.items():
+
+ if k.endswith(KEY_ANNEE):
+ borne_gauche = "%s-01-01" % v
+ borne_droite = "%s-12-31" % v
+
+ if k.endswith(KEY_DATE_DEBUT):
+ date = time.strptime(v, settings.DATE_INPUT_FORMATS[0])
+ borne_gauche = time.strftime("%Y-%m-%d", date)
+
+ if k.endswith(KEY_DATE_FIN):
+ date = time.strptime(v, settings.DATE_INPUT_FORMATS[0])
+ borne_droite = time.strftime("%Y-%m-%d", date)
+
+ if k.endswith(KEY_STATUT):
+ aujourdhui = datetime.date.today()
+ if v == STATUT_ACTIF:
+ borne_gauche = aujourdhui
+ borne_droite = aujourdhui
+ elif v == STATUT_INACTIF:
+ # dans le cas d'une FK, on retire des inactifs ceux qui ont une FK active
+ if prefix != "":
+ q_range = self.get_q_range(prefix, aujourdhui, aujourdhui)
+ id_actifs = [o.id for o in qs.filter(q_range).distinct()]
+ qs = qs.exclude(id__in=id_actifs)
+ borne_droite = aujourdhui
+ elif v == STATUT_FUTUR:
+ borne_gauche = aujourdhui
+ elif v == STATUT_INCONNU:
+ q = q & self.get_q_inconnu(prefix)
+
+ q_range = self.get_q_range(prefix, borne_gauche, borne_droite)
+ qs = qs.filter(q & q_range).distinct()
+ return qs
+
+RechercheTemporelle.get_query_string = DjangoChangeList.get_query_string.im_func
+
+
+class ChangeList(DjangoChangeList, RechercheTemporelle):
+
+ def __init__(self, *args, **kwargs):
+ super(ChangeList, self).__init__(*args, **kwargs)
+
def get_query_set(self):
- lookup_recherche_temporelle = {}
use_distinct = False
qs = self.root_query_set
for i in (ALL_VAR, ORDER_VAR, ORDER_TYPE_VAR, SEARCH_VAR, IS_POPUP_VAR, TO_FIELD_VAR):
if i in lookup_params:
del lookup_params[i]
+
+ lookup_params = self.purge_params(lookup_params)
for key, value in lookup_params.items():
if not isinstance(key, str):
# 'key' will be used as a keyword argument later, so Python
del lookup_params[key]
lookup_params[smart_str(key)] = value
- # ignorer les param GET pour la recherche temporelle
- if len([i for i in self.internal_fields if key.endswith(i)]) > 0:
- del lookup_params[key]
- lookup_recherche_temporelle[key] = value
- continue
-
if not use_distinct:
# Check if it's a relationship that might return more than one
# instance
except:
raise IncorrectLookupParameters
- q = Q()
- prefix = self.get_prefix()
- borne_gauche = None
- borne_droite = None
- for k, v in lookup_recherche_temporelle.items():
-
- if k.endswith(KEY_ANNEE):
- borne_gauche = "%s-01-01" % v
- borne_droite = "%s-12-31" % v
-
- if k.endswith(KEY_DATE_DEBUT):
- date = time.strptime(v, settings.DATE_INPUT_FORMATS[0])
- borne_gauche = time.strftime("%Y-%m-%d", date)
-
- if k.endswith(KEY_DATE_FIN):
- date = time.strptime(v, settings.DATE_INPUT_FORMATS[0])
- borne_droite = time.strftime("%Y-%m-%d", date)
-
- if k.endswith(KEY_STATUT):
- aujourdhui = datetime.date.today()
- if v == STATUT_ACTIF:
- borne_gauche = aujourdhui
- borne_droite = aujourdhui
- elif v == STATUT_INACTIF:
- # dans le cas d'une FK, on retire des inactifs ceux qui ont une FK active
- if prefix != "":
- q_range = self.get_q_range(prefix, aujourdhui, aujourdhui)
- id_actifs = [o.id for o in qs.filter(q_range).distinct()]
- qs = qs.exclude(id__in=id_actifs)
- borne_droite = aujourdhui
- elif v == STATUT_FUTUR:
- borne_gauche = aujourdhui
- elif v == STATUT_INCONNU:
- q = q & self.get_q_inconnu(prefix)
-
- q_range = self.get_q_range(prefix, borne_gauche, borne_droite)
- qs = qs.filter(q & q_range).distinct()
+ qs = self.filter_temporel(qs)
# Use select_related() if one of the list_display options is a field
# with a relationship and the provided queryset doesn't already have
'choices': prepare_choices(COMBLE_CHOICES, 'comble', context)}
-@register.inclusion_tag('admin/filter_select.html', takes_context=True)
+@register.inclusion_tag('admin/filter.html', takes_context=True)
def filter_region(context):
return {'title': u"région",
'choices': prepare_choices(Region.objects.values_list('id', 'nom'), 'implantation__region', context, remove=['pays', 'nord_sud'])}
-@register.inclusion_tag('admin/filter_select.html', takes_context=True)
+@register.inclusion_tag('admin/filter.html', takes_context=True)
def filter_implantation(context):
return {'title': u"implantation",
'choices': prepare_choices(Implantation.objects.values_list('id', 'nom'), 'implantation', context)}
-@register.inclusion_tag('admin/filter_select.html', takes_context=True)
+@register.inclusion_tag('admin/filter.html', takes_context=True)
def filter_region_contrat(context):
return {'title': u"région",
'choices': prepare_choices(Region.objects.values_list('id', 'nom'), 'dossier__poste__implantation__region', context, remove=['pays', 'nord_sud'])}
-@register.inclusion_tag('admin/filter_select.html', takes_context=True)
+@register.inclusion_tag('admin/filter.html', takes_context=True)
def filter_implantation_contrat(context):
return {'title': u"implantation",
'choices': prepare_choices(Implantation.objects.values_list('id', 'nom'), 'dossier__poste__implantation', context)}
-@register.inclusion_tag('admin/filter_select.html', takes_context=True)
+@register.inclusion_tag('admin/filter.html', takes_context=True)
def filter_type_contrat(context):
return {'title': u"type de contrat",
'choices': prepare_choices(TypeContrat.objects.values_list('id', 'nom'), 'type_contrat', context)}