diff --git a/sigi/apps/servicos/filters.py b/sigi/apps/servicos/filters.py new file mode 100644 index 0000000..ba432b1 --- /dev/null +++ b/sigi/apps/servicos/filters.py @@ -0,0 +1,96 @@ +from datetime import date, timedelta +from django.utils.translation import gettext as _ +from django.contrib import admin + +class ServicoAtivoFilter(admin.FieldListFilter): + parameter_name = None + + def __init__(self, field, request, params, model, model_admin, field_path): + self.model = model + self.model_admin = model_admin + self.parameter_name = f'{field_path}__isnull' + + super().__init__(field, request, params, model, model_admin, field_path) + + self.title = _("Serviço ativo") + + if self.parameter_name in params: + value = params.pop(self.parameter_name) + self.used_parameters[self.parameter_name] = value + lookup_choices = self.lookups(request, model_admin) + + if lookup_choices is None: + lookup_choices = () + self.lookup_choices = list(lookup_choices) + + def lookups(self, request, model_admin): + return ( + ('True', _('Ativo')), + ('False', _('Inativo')) + ) + + def has_output(self): + return self.model.objects.exists() + + def value(self): + return self.used_parameters.get(self.parameter_name) + + def expected_parameters(self): + return [self.parameter_name,] + + def choices(self, changelist): + yield { + 'selected': self.value() is None, + 'query_string': changelist.get_query_string( + remove=[self.parameter_name]), + 'display': _('All'), + } + for lookup, title in self.lookup_choices: + yield { + 'selected': str(self.value()) == str(lookup), + 'query_string': changelist.get_query_string( + {self.parameter_name: lookup} + ), + 'display': title, + } + +class DataUtimoUsoFilter(admin.SimpleListFilter): + title = _("Atualização") + parameter_name = 'atualizacao' + + def lookups(self, request, model_admin): + return ( + ('err', _("Erro na verificação")), + ('year', _("Sem atualização há um ano ou mais")), + ('semester', _("Sem atualização de seis meses a um ano")), + ('quarter', _("Sem atualização de três a seis meses")), + ('month', _("Sem atualização de um a três meses")), + ('week', _("Sem atualização de uma semana a um mês")), + ('updated', _("Atualizado na última semana")), + ) + + def queryset(self, request, queryset): + if self.value() is not None: + queryset = queryset.exclude(tipo_servico__string_pesquisa="") + if self.value() == 'err': + queryset = queryset.exclude(erro_atualizacao="") + elif self.value() == 'year': + limite = date.today() - timedelta(days=365) + queryset = queryset.filter(data_ultimo_uso__lte=limite) + else: + de = date.today() - ( + timedelta(days=365) if self.value() == 'semester' else + timedelta(days=6*30) if self.value() == 'quarter' else + timedelta(days=3*30) if self.value() == 'month' else + timedelta(days=30) if self.value() == 'week' else + timedelta(days=0) + ) + ate = date.today() - ( + timedelta(days=6*30) if self.value() == 'semester' else + timedelta(days=3*30) if self.value() == 'quarter' else + timedelta(days=30) if self.value() == 'month' else + timedelta(days=7) if self.value() == 'week' else + timedelta(days=0) + ) + queryset = queryset.filter(data_ultimo_uso__range=(de, ate)) + return queryset diff --git a/sigi/apps/servicos/forms.py b/sigi/apps/servicos/forms.py new file mode 100644 index 0000000..c584451 --- /dev/null +++ b/sigi/apps/servicos/forms.py @@ -0,0 +1,30 @@ +from django.forms.models import ModelForm +from django.utils.encoding import force_str +from sigi.apps.servicos.models import Servico, CasaAtendida + +class ServicoFormAdmin(ModelForm): + class Meta: + model = Servico + fields = '__all__' + + def __init__(self, *args, **kwargs): + super(ServicoFormAdmin, self).__init__(*args, **kwargs) + + self.fields['contato_tecnico'].choices = () + self.fields['contato_administrativo'].choices = () + + if self.instance.casa_legislativa_id: + id_casa = self.instance.casa_legislativa_id + elif 'initial' in kwargs and 'id_casa' in kwargs['initial']: + id_casa = kwargs['initial']['id_casa'] + self.instance.casa_legislativa_id = id_casa + else: + id_casa = None + + if id_casa: + casa = CasaAtendida.objects.get(pk=id_casa) + contatos = [ + (f.id, force_str(f)) for f in casa.funcionario_set.all() + ] + self.fields['contato_tecnico'].choices = contatos + self.fields['contato_administrativo'].choices = contatos diff --git a/sigi/apps/utils/templates/admin/date_range_filter.html b/sigi/apps/utils/templates/admin/date_range_filter.html new file mode 100644 index 0000000..c3ee1ec --- /dev/null +++ b/sigi/apps/utils/templates/admin/date_range_filter.html @@ -0,0 +1,13 @@ +{% load i18n %} +{% with choice=choices.0 %} +
+
+ + + {% trans 'All' %} + {% for field in choice.form %} + + {% endfor %} +
+
+{% endwith %} \ No newline at end of file