fix UT referrer
authorOlivier Larchevêque <olivier.larcheveque@auf.org>
Thu, 27 Sep 2012 23:14:08 +0000 (19:14 -0400)
committerOlivier Larchevêque <olivier.larcheveque@auf.org>
Thu, 27 Sep 2012 23:14:08 +0000 (19:14 -0400)
project/rh/admin.py

index 64a26d9..62df1c9 100644 (file)
@@ -27,6 +27,19 @@ from project.rh.forms import ContratForm, AyantDroitForm, EmployeAdminForm, \
         AjaxSelect, DossierForm, ResponsableInlineForm
 from project.rh.change_list import ChangeList
 
+def listing_par_defaut(model, request):
+    """
+    Teste si la requete provient de la même page.
+    """
+    if not 'HTTP_REFERER' in request.META.keys():
+        return False
+    referer = request.META['HTTP_REFERER']
+    referer = "/".join(referer.split('/')[3:])
+    referer = "/%s" % referer.split('?')[0]
+    change_list_view = 'admin:%s_%s_changelist' % (
+            model._meta.app_label,
+            model.__name__.lower(),)
+    return referer != reverse(change_list_view)
 
 class BaseAdmin(admin.ModelAdmin):
 
@@ -102,17 +115,14 @@ class DateRangeMixin(object):
     prefixe_recherche_temporelle = ""
 
     def get_changelist(self, request, **kwargs):
-        if 'HTTP_REFERER' in request.META.keys():
-                    referer = request.META['HTTP_REFERER']
-                    referer = "/".join(referer.split('/')[3:])
-                    referer = "/%s" % referer.split('?')[0]
-                    change_list_view = 'admin:%s_%s_changelist' % (
-                            self.model._meta.app_label,
-                            self.model.__name__.lower(),)
-                    if referer != reverse(change_list_view):
-                        params = request.GET.copy()
-                        params.update({'statut': 'Actif'})
-                        request.GET = params
+        """
+        On filtre par défaut sur les items 'actifs'.
+        Le changelist plug le filtrage temporel.
+        """
+        if listing_par_defaut(self.model, request):
+            params = request.GET.copy()
+            params.update({'statut': 'Actif'})
+            request.GET = params
         return ChangeList
 
 
@@ -129,6 +139,20 @@ class LinkedInline(admin.options.InlineModelAdmin):
 
 class ProtectRegionMixin(object):
 
+    def changelist_view(self, request, extra_context=None):
+        """
+        On filtre par défaut sur la ZA du user connecté
+        """
+        if listing_par_defaut(self.model, request):
+            if user_gere_obj_de_sa_region(request.user):
+                params = request.GET.copy()
+                employe = groups.get_employe_from_user(request.user)
+                za = employe.implantation.zone_administrative.code
+                prefix_za = "%s__code__exact" % self.model.prefix_implantation
+                params.update({prefix_za: za})
+                request.GET = params
+        return super(ProtectRegionMixin, self).changelist_view(request, extra_context)
+
     def queryset(self, request):
         qs = super(ProtectRegionMixin, self).queryset(request)