PEP8
[auf_rh_dae.git] / project / rh / templatetags / rapports.py
index 0083e23..08045fa 100644 (file)
@@ -6,10 +6,12 @@ from django.utils.encoding import smart_unicode
 from django.template import Library
 from django.utils.http import urlencode
 
-from auf.django.references.models import Implantation, Region
+from auf.django.references.models import \
+        Implantation, Region, ZoneAdministrative
 
-from project.rh.models import TypeContrat
+from project import groups
 
+from project.rh.models import TypeContrat
 
 register = Library()
 
@@ -38,6 +40,18 @@ def filter_region(context, prefix=None):
 
 
 @register.inclusion_tag('admin/filter.html', takes_context=True)
+def filter_zone_administrative(context, prefix=None):
+    label = "".join([prefix or "", "implantation__zone_administrative"])
+    return {
+        'title': u"zone administrative",
+        'choices': prepare_choices(
+            ZoneAdministrative.objects.values_list('code', 'nom'), label,
+            context, remove=['pays', 'nord_sud']
+        )
+    }
+
+
+@register.inclusion_tag('admin/filter.html', takes_context=True)
 def filter_implantation(context, prefix=None):
     label = "".join([prefix or "", "implantation"])
     return {
@@ -49,24 +63,35 @@ def filter_implantation(context, prefix=None):
 
 
 @register.inclusion_tag('admin/filter.html', takes_context=True)
-def filter_region_contrat(context):
+def filter_zone_administrative_contrat(context):
+    request = context['request']
+    user_groups = [g.name for g in request.user.groups.all()]
+    if groups.CORRESPONDANT_RH in user_groups or\
+         groups.ADMINISTRATEURS in user_groups or\
+         groups.DIRECTEUR_DE_BUREAU in user_groups:
+        employe = groups.get_employe_from_user(request.user)
+        zones = ZoneAdministrative.objects.filter(
+            code=employe.implantation.zone_administrative.code
+        )
+    else:
+        zones = ZoneAdministrative.objects.all()
     return {
         'title': u"région",
         'choices': prepare_choices(
-            Region.objects.values_list('id', 'nom'),
-            'dossier__poste__implantation__region', context,
-            remove=['pays', 'nord_sud']
+            zones.values_list('code', 'nom'),
+            'dossier__poste__implantation__zone_administrative',
+            context, remove=['pays', 'nord_sud']
         )
     }
 
 
 @register.inclusion_tag('admin/filter.html', takes_context=True)
-def filter_region_dossier(context):
+def filter_zone_administrative_dossier(context):
     return {
         'title': u"région",
         'choices': prepare_choices(
-            Region.objects.values_list('id', 'nom'),
-            'poste__implantation__region', context,
+            ZoneAdministrative.objects.values_list('code', 'nom'),
+            'poste__implantation__zone_administrative', context,
             remove=['pays', 'nord_sud']
         )
     }
@@ -85,10 +110,21 @@ def filter_implantation_dossier(context):
 
 @register.inclusion_tag('admin/filter.html', takes_context=True)
 def filter_implantation_contrat(context):
+    request = context['request']
+    user_groups = [g.name for g in request.user.groups.all()]
+    if groups.CORRESPONDANT_RH in user_groups or\
+         groups.ADMINISTRATEURS in user_groups or\
+         groups.DIRECTEUR_DE_BUREAU in user_groups:
+        employe = groups.get_employe_from_user(request.user)
+        implantations = Implantation.objects.filter(
+            zone_administrative=employe.implantation.zone_administrative
+        )
+    else:
+        implantations = Implantation.objects.all()
     return {
         'title': u"implantation",
         'choices': prepare_choices(
-            Implantation.objects.values_list('id', 'nom'),
+            implantations.values_list('id', 'nom'),
             'dossier__poste__implantation', context
         )
     }
@@ -185,12 +221,12 @@ def filter_a_venir(context):
 
 
 @register.inclusion_tag('admin/filter_select.html', takes_context=True)
-def filter_region_remun(context):
+def filter_zone_administrative_remun(context):
     return {
         'title': u"région",
         'choices': prepare_choices(
-            Region.objects.values_list('id', 'nom'),
-            'dossiers__poste__implantation__region', context,
+            ZoneAdministrative.objects.values_list('code', 'nom'),
+            'dossiers__poste__implantation__zone_administrative', context,
             remove=['pays', 'nord_sud']
         )
     }