| 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | import datetime |
| 4 | |
| 5 | from django.utils.encoding import smart_unicode |
| 6 | from django.template import Library |
| 7 | from django.utils.http import urlencode |
| 8 | |
| 9 | from datamaster_modeles.models import Implantation, Region |
| 10 | from rh.models import TypeContrat |
| 11 | |
| 12 | |
| 13 | register = Library() |
| 14 | |
| 15 | |
| 16 | COMBLE_CHOICES = (('c', 'Comblé'), ('n', 'Vacant')) |
| 17 | |
| 18 | |
| 19 | @register.inclusion_tag('admin/filter.html', takes_context=True) |
| 20 | def filter_comble(context): |
| 21 | return {'title': 'comblé', |
| 22 | 'choices': prepare_choices(COMBLE_CHOICES, 'comble', context)} |
| 23 | |
| 24 | |
| 25 | @register.inclusion_tag('admin/filter.html', takes_context=True) |
| 26 | def filter_region(context): |
| 27 | return {'title': u"région", |
| 28 | 'choices': prepare_choices(Region.objects.values_list('id', 'nom'), 'implantation__region', context, remove=['pays', 'nord_sud'])} |
| 29 | |
| 30 | |
| 31 | @register.inclusion_tag('admin/filter.html', takes_context=True) |
| 32 | def filter_implantation(context): |
| 33 | return {'title': u"implantation", |
| 34 | 'choices': prepare_choices(Implantation.objects.values_list('id', 'nom'), 'implantation', context)} |
| 35 | |
| 36 | |
| 37 | @register.inclusion_tag('admin/filter.html', takes_context=True) |
| 38 | def filter_region_contrat(context): |
| 39 | return {'title': u"région", |
| 40 | 'choices': prepare_choices(Region.objects.values_list('id', 'nom'), 'dossier__poste__implantation__region', context, remove=['pays', 'nord_sud'])} |
| 41 | |
| 42 | |
| 43 | @register.inclusion_tag('admin/filter.html', takes_context=True) |
| 44 | def filter_implantation_contrat(context): |
| 45 | return {'title': u"implantation", |
| 46 | 'choices': prepare_choices(Implantation.objects.values_list('id', 'nom'), 'dossier__poste__implantation', context)} |
| 47 | |
| 48 | |
| 49 | @register.inclusion_tag('admin/filter.html', takes_context=True) |
| 50 | def filter_type_contrat(context): |
| 51 | return {'title': u"type de contrat", |
| 52 | 'choices': prepare_choices(TypeContrat.objects.values_list('id', 'nom'), 'type_contrat', context)} |
| 53 | |
| 54 | @register.inclusion_tag('admin/filter_select.html', takes_context=True) |
| 55 | def filter_echeance_contrat(context): |
| 56 | today = datetime.date.today() |
| 57 | three_months = today + datetime.timedelta(days=3*30) |
| 58 | six_months = today + datetime.timedelta(days=6*30) |
| 59 | twelve_months = today + datetime.timedelta(days=12*30) |
| 60 | |
| 61 | field_name = 'date_fin' |
| 62 | return {'title': u"échéance", |
| 63 | 'choices': prepare_choices_date(field_name, context, links=( |
| 64 | ('Tous', {}), |
| 65 | ('échus', {'%s__lt' % field_name: today.strftime('%Y-%m-%d'),}), |
| 66 | ('moins de 3 mois', {'%s__gte' % field_name: today.strftime('%Y-%m-%d'), |
| 67 | '%s__lte' % field_name: three_months.strftime('%Y-%m-%d')}), |
| 68 | ('3 à 6 mois', {'%s__gte' % field_name: three_months.strftime('%Y-%m-%d'), |
| 69 | '%s__lte' % field_name: six_months.strftime('%Y-%m-%d')}), |
| 70 | ('6 à 12 mois', {'%s__gte' % field_name: six_months.strftime('%Y-%m-%d'), |
| 71 | '%s__lte' % field_name: twelve_months.strftime('%Y-%m-%d')}), |
| 72 | ) |
| 73 | )} |
| 74 | |
| 75 | @register.inclusion_tag('admin/filter_select.html', takes_context=True) |
| 76 | def filter_debut_contrat(context): |
| 77 | year = datetime.date.today().timetuple()[0] |
| 78 | this_year = datetime.date(year, 1, 1) |
| 79 | next_year = datetime.date(year + 1, 1, 1) |
| 80 | last_year = datetime.date(year - 1, 12,31) |
| 81 | |
| 82 | field_name = 'date_debut' |
| 83 | return {'title': u"date début", |
| 84 | 'choices': prepare_choices_date(field_name, context, links=( |
| 85 | ('Tous', {}), |
| 86 | ('années à venir', {'%s__gte' % field_name: next_year.strftime('%Y-%m-%d')}), |
| 87 | ('cette anneée', {'%s__gte' % field_name: this_year.strftime('%Y-%m-%d'), |
| 88 | '%s__lt' % field_name: next_year.strftime('%Y-%m-%d')}), |
| 89 | ('années passées', {'%s__lte' % field_name: last_year.strftime('%Y-%m-%d')}), |
| 90 | ) |
| 91 | )} |
| 92 | |
| 93 | @register.inclusion_tag('admin/filter_select.html', takes_context=True) |
| 94 | def filter_a_venir(context): |
| 95 | today = datetime.date.today() |
| 96 | year, month, day = datetime.date.today().timetuple()[:3] |
| 97 | mois_prochain = datetime.date(year+((month+1)/13), (month+1)%12, 1) |
| 98 | this_month = datetime.date(year, month, 1) |
| 99 | |
| 100 | field_name = 'date_debut' |
| 101 | return {'title': u"à venir", |
| 102 | 'choices': prepare_choices_date(field_name, context, links=( |
| 103 | ('Tous', {}), |
| 104 | ('à venir', {'%s__gt' % field_name: today.strftime('%Y-%m-%d')}), |
| 105 | ('à venir mois prochain', {'%s__gte' % field_name: mois_prochain.strftime('%Y-%m-%d')}), |
| 106 | ('à venir ce mois', {'%s__gte' % field_name: this_month.strftime('%Y-%m-%d'), |
| 107 | '%s__lt' % field_name: mois_prochain.strftime('%Y-%m-%d')}), |
| 108 | ) |
| 109 | )} |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | @register.inclusion_tag('admin/filter_select.html', takes_context=True) |
| 115 | def filter_region_remun(context): |
| 116 | return {'title': u"région", |
| 117 | 'choices': prepare_choices(Region.objects.values_list('id', 'nom'), 'dossiers__poste__implantation__region', context, remove=['pays', 'nord_sud'])} |
| 118 | |
| 119 | |
| 120 | @register.inclusion_tag('admin/filter_select.html', takes_context=True) |
| 121 | def filter_implantation_remun(context): |
| 122 | return {'title': u"implantation", |
| 123 | 'choices': prepare_choices(Implantation.objects.values_list('id', 'nom'), 'dossiers__poste__implantation', context)} |
| 124 | |
| 125 | @register.inclusion_tag('rh/rapports/table_header.html', takes_context=True) |
| 126 | def table_header(context, headers): |
| 127 | return {'headers': headers} |
| 128 | |
| 129 | def get_query_string(request, new_params=None, remove=None): |
| 130 | if new_params is None: new_params = {} |
| 131 | if remove is None: remove = [] |
| 132 | p = dict(request.GET.items()) |
| 133 | for r in remove: |
| 134 | for k in p.keys(): |
| 135 | if k.startswith(r): |
| 136 | del p[k] |
| 137 | for k, v in new_params.items(): |
| 138 | if v is None: |
| 139 | if k in p: |
| 140 | del p[k] |
| 141 | else: |
| 142 | p[k] = v |
| 143 | return '?%s' % urlencode(p) |
| 144 | |
| 145 | |
| 146 | def prepare_choices(choices, query_param, context, remove=[]): |
| 147 | request = context['request'] |
| 148 | query_val = request.GET.get(query_param) |
| 149 | result = [{'selected': query_val is None, |
| 150 | 'query_string': get_query_string(request, {}, [query_param] + remove), |
| 151 | 'display': 'Tout'}] |
| 152 | for k, v in choices: |
| 153 | result.append({'selected': smart_unicode(k) == query_val, |
| 154 | 'query_string': get_query_string(request, {query_param: k}, remove), |
| 155 | 'display': v}) |
| 156 | return result |
| 157 | |
| 158 | |
| 159 | def prepare_choices_date(field_name, context, links, remove=[]): |
| 160 | request = context['request'] |
| 161 | params = request.GET |
| 162 | field_generic = '%s__' % field_name |
| 163 | date_params = dict([(k, v) for k, v in params.items() if k.startswith(field_generic)]) |
| 164 | |
| 165 | |
| 166 | |
| 167 | result = [] |
| 168 | for title, param_dict in links: |
| 169 | result.append({'selected': date_params == param_dict, |
| 170 | 'query_string': get_query_string(request, param_dict, [field_generic]), |
| 171 | 'display': title}) |
| 172 | return result |
| 173 | |
| 174 | class SortHeaders: |
| 175 | def __init__(self, request, headers, order_field_type, order_field, default_order_type="asc", not_sortable=[]): |
| 176 | self.request = request |
| 177 | self.order_field_type = order_field_type |
| 178 | self.order_field = order_field |
| 179 | self.header_defs = headers |
| 180 | self.params = dict(request.GET) |
| 181 | if default_order_type not in ('asc', 'desc'): |
| 182 | raise AttributeError(u"L'ordre par défaut n'est pas valide") |
| 183 | self.default_order_type = default_order_type |
| 184 | self.current_order_field = self.params[self.order_field][0] if self.order_field in self.params else None |
| 185 | self.current_order_field_type = self.params[self.order_field_type][0] if self.order_field_type in self.params else None |
| 186 | self.not_sortable = not_sortable |
| 187 | |
| 188 | def headers(self): |
| 189 | for h in self.header_defs: |
| 190 | params = self.params |
| 191 | if h[0] == self.current_order_field: |
| 192 | order_type = {'asc': 'desc', 'desc': 'asc'}[self.current_order_field_type] |
| 193 | class_order = "%sending" % self.current_order_field_type |
| 194 | else: |
| 195 | order_type = self.default_order_type |
| 196 | class_order = "" |
| 197 | params[self.order_field_type] = [order_type] |
| 198 | params[self.order_field] = [h[0]] |
| 199 | url = "?%s" % "&".join(['%s=%s' % (param, value[0]) for param, value in params.items()]) |
| 200 | yield { |
| 201 | "class_attr": "sorted %s" % class_order if self.field_sorted(h[0]) else "", |
| 202 | "sortable": self.field_sorted(h[0]), |
| 203 | "url": url, |
| 204 | "text": h[1] |
| 205 | } |
| 206 | |
| 207 | def field_sorted(self, field): |
| 208 | return True if field not in self.not_sortable else False |
| 209 | |
| 210 | @register.filter |
| 211 | def split(str, splitter): |
| 212 | return str.split(splitter) |