From 1dbb7844250ebdb653614fb24277c9c3688bac86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ses=C3=B3stris=20Vieira?= Date: Fri, 7 Jan 2022 08:39:34 -0300 Subject: [PATCH] =?UTF-8?q?Melhora=20visualiza=C3=A3o=20de=20n=C3=BAmeros?= =?UTF-8?q?=20grandes=20no=20RangeFilter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sigi/apps/utils/filters.py | 53 +++++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/sigi/apps/utils/filters.py b/sigi/apps/utils/filters.py index 6a9361d..160717c 100644 --- a/sigi/apps/utils/filters.py +++ b/sigi/apps/utils/filters.py @@ -1,8 +1,8 @@ -# coding: utf-8 import string +from math import log10 from django.contrib import admin from django.contrib.admin.options import IncorrectLookupParameters -from django.utils.translation import gettext as _ +from django.utils.translation import ngettext, gettext as _ from django.core.exceptions import ValidationError @@ -46,14 +46,55 @@ class RangeFilter(admin.FieldListFilter): ultimo = 0 for i in range(1, self.num_faixas): - yield (i, ultimo, tudo[i*passo]) - ultimo = tudo[i*passo] + value = tudo[i*passo] + if value > 100: + if value > 1000: + l = int(log10(value)) + else: + l = int(log10(value))-1 + value = value // (10**l) * (10**l) + yield (i, ultimo, value) + ultimo = value yield (self.num_faixas, ultimo, tudo.last()) def lookups(self, request, model_admin): - return ((value, _(f"de {min} até {max}")) - for value, min, max in self.ranges(self.model)) + def humanize(num): + if num < 1000: + return num + l = int(log10(num)) + if l < 6: + return ngettext( + f"{num//10**3} mil", + f"{num//10**3} mil", + num//10**3 + ) + elif l < 9: + return ngettext( + f"{num//10**6} milhão", + f"{num//10**6} milhões", + num//10**6 + ) + elif l < 12: + return ngettext( + f"{num//10**9} bilhão", + f"{num//10**9} bilhões", + num//10**9 + ) + else: + return ngettext( + f"{num//10**12} trilhão", + f"{num//10**12} trilhões", + num//10**12 + ) + + primeiro, *meio, ultimo = self.ranges(self.model) + value, min, max = primeiro + yield (value, _(f"Até {humanize(max)}")) + for value, min, max in meio: + yield (value, _(f"de {humanize(min)} até {humanize(max)}")) + value, min, max = ultimo + yield (value, _(f"Acima de {humanize(min)}")) def has_output(self): return self.model.objects.exists()