|
@ -1,11 +1,21 @@ |
|
|
import string |
|
|
import string |
|
|
from math import log10 |
|
|
from math import log10 |
|
|
from django import forms |
|
|
from django import forms |
|
|
from django.db.models import Q |
|
|
|
|
|
from django.contrib import admin |
|
|
from django.contrib import admin |
|
|
from django.contrib.admin.options import IncorrectLookupParameters |
|
|
from django.contrib.admin.options import IncorrectLookupParameters |
|
|
from django.utils.translation import ngettext, gettext as _ |
|
|
from django.contrib.admin.utils import ( |
|
|
|
|
|
build_q_object_from_lookup_parameters, |
|
|
|
|
|
prepare_lookup_value, |
|
|
|
|
|
) |
|
|
from django.core.exceptions import ValidationError |
|
|
from django.core.exceptions import ValidationError |
|
|
|
|
|
from django.utils.translation import ngettext, gettext as _ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def filter_single_value(value): |
|
|
|
|
|
if isinstance(value, list): |
|
|
|
|
|
return value[0] |
|
|
|
|
|
else: |
|
|
|
|
|
return value |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NotEmptyableField(Exception): |
|
|
class NotEmptyableField(Exception): |
|
@ -14,7 +24,7 @@ class NotEmptyableField(Exception): |
|
|
|
|
|
|
|
|
class AlphabeticFilter(admin.SimpleListFilter): |
|
|
class AlphabeticFilter(admin.SimpleListFilter): |
|
|
title = "" |
|
|
title = "" |
|
|
parameter_name = "" |
|
|
parameter_name = None |
|
|
|
|
|
|
|
|
def lookups(self, request, model_admin): |
|
|
def lookups(self, request, model_admin): |
|
|
return ( |
|
|
return ( |
|
@ -26,10 +36,13 @@ class AlphabeticFilter(admin.SimpleListFilter): |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
def queryset(self, request, queryset): |
|
|
def queryset(self, request, queryset): |
|
|
if self.value(): |
|
|
if self.value() is not None: |
|
|
return queryset.filter( |
|
|
queryset = queryset.filter( |
|
|
(self.parameter_name + "__istartswith", self.value()) |
|
|
build_q_object_from_lookup_parameters( |
|
|
|
|
|
{self.parameter_name + "__istartswith": self.value()} |
|
|
) |
|
|
) |
|
|
|
|
|
) |
|
|
|
|
|
return queryset |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RangeFilter(admin.FieldListFilter): |
|
|
class RangeFilter(admin.FieldListFilter): |
|
@ -45,9 +58,6 @@ class RangeFilter(admin.FieldListFilter): |
|
|
field, request, params, model, model_admin, field_path |
|
|
field, request, params, model, model_admin, field_path |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
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) |
|
|
lookup_choices = self.lookups(request, model_admin) |
|
|
|
|
|
|
|
|
if lookup_choices is None: |
|
|
if lookup_choices is None: |
|
@ -65,10 +75,10 @@ class RangeFilter(admin.FieldListFilter): |
|
|
value = tudo[i * passo] |
|
|
value = tudo[i * passo] |
|
|
if value > 100: |
|
|
if value > 100: |
|
|
if value > 1000: |
|
|
if value > 1000: |
|
|
l = int(log10(value)) |
|
|
log_val = int(log10(value)) |
|
|
else: |
|
|
else: |
|
|
l = int(log10(value)) - 1 |
|
|
log_val = int(log10(value)) - 1 |
|
|
value = value // (10**l) * (10**l) |
|
|
value = value // (10**log_val) * (10**log_val) |
|
|
yield (i, ultimo, value) |
|
|
yield (i, ultimo, value) |
|
|
ultimo = value |
|
|
ultimo = value |
|
|
|
|
|
|
|
@ -78,18 +88,18 @@ class RangeFilter(admin.FieldListFilter): |
|
|
def humanize(num): |
|
|
def humanize(num): |
|
|
if num < 1000: |
|
|
if num < 1000: |
|
|
return num |
|
|
return num |
|
|
l = int(log10(num)) |
|
|
log_val = int(log10(num)) |
|
|
if l < 6: |
|
|
if log_val < 6: |
|
|
return ngettext( |
|
|
return ngettext( |
|
|
f"{num//10**3} mil", f"{num//10**3} mil", num // 10**3 |
|
|
f"{num//10**3} mil", f"{num//10**3} mil", num // 10**3 |
|
|
) |
|
|
) |
|
|
elif l < 9: |
|
|
elif log_val < 9: |
|
|
return ngettext( |
|
|
return ngettext( |
|
|
f"{num//10**6} milhão", |
|
|
f"{num//10**6} milhão", |
|
|
f"{num//10**6} milhões", |
|
|
f"{num//10**6} milhões", |
|
|
num // 10**6, |
|
|
num // 10**6, |
|
|
) |
|
|
) |
|
|
elif l < 12: |
|
|
elif log_val < 12: |
|
|
return ngettext( |
|
|
return ngettext( |
|
|
f"{num//10**9} bilhão", |
|
|
f"{num//10**9} bilhão", |
|
|
f"{num//10**9} bilhões", |
|
|
f"{num//10**9} bilhões", |
|
@ -114,7 +124,10 @@ class RangeFilter(admin.FieldListFilter): |
|
|
return self.model.objects.exists() |
|
|
return self.model.objects.exists() |
|
|
|
|
|
|
|
|
def value(self): |
|
|
def value(self): |
|
|
return self.used_parameters.get(self.parameter_name) |
|
|
value = self.used_parameters.get(self.parameter_name) |
|
|
|
|
|
if isinstance(value, list): |
|
|
|
|
|
return value[-1] |
|
|
|
|
|
return value |
|
|
|
|
|
|
|
|
def expected_parameters(self): |
|
|
def expected_parameters(self): |
|
|
return [ |
|
|
return [ |
|
@ -131,7 +144,7 @@ class RangeFilter(admin.FieldListFilter): |
|
|
} |
|
|
} |
|
|
for lookup, title in self.lookup_choices: |
|
|
for lookup, title in self.lookup_choices: |
|
|
yield { |
|
|
yield { |
|
|
"selected": self.value() == str(lookup), |
|
|
"selected": str(lookup) == self.value(), |
|
|
"query_string": changelist.get_query_string( |
|
|
"query_string": changelist.get_query_string( |
|
|
{self.parameter_name: lookup} |
|
|
{self.parameter_name: lookup} |
|
|
), |
|
|
), |
|
@ -141,7 +154,7 @@ class RangeFilter(admin.FieldListFilter): |
|
|
def queryset(self, request, queryset): |
|
|
def queryset(self, request, queryset): |
|
|
try: |
|
|
try: |
|
|
for value, min, max in self.ranges(self.model): |
|
|
for value, min, max in self.ranges(self.model): |
|
|
if self.value() == str(value): |
|
|
if str(value) == self.value(): |
|
|
return queryset.filter( |
|
|
return queryset.filter( |
|
|
(f"{self.field_path}__gte", min), |
|
|
(f"{self.field_path}__gte", min), |
|
|
(f"{self.field_path}__lt", max), |
|
|
(f"{self.field_path}__lt", max), |
|
@ -159,15 +172,20 @@ class DateRangeFilter(admin.FieldListFilter): |
|
|
self.model = model |
|
|
self.model = model |
|
|
self.model_admin = model_admin |
|
|
self.model_admin = model_admin |
|
|
self.lookup_kwargs = [f"{field_path}__gte", f"{field_path}__lte"] |
|
|
self.lookup_kwargs = [f"{field_path}__gte", f"{field_path}__lte"] |
|
|
|
|
|
form_data = {} |
|
|
|
|
|
for p in self.expected_parameters(): |
|
|
|
|
|
if p in params: |
|
|
|
|
|
value = params.pop(p)[-1] |
|
|
|
|
|
form_data[p] = prepare_lookup_value( |
|
|
|
|
|
p, value, self.list_separator |
|
|
|
|
|
) |
|
|
super().__init__( |
|
|
super().__init__( |
|
|
field, request, params, model, model_admin, field_path |
|
|
field, request, params, model, model_admin, field_path |
|
|
) |
|
|
) |
|
|
|
|
|
form = self.get_date_form(form_data) |
|
|
form = self.get_date_form(self.used_parameters) |
|
|
|
|
|
if form.is_valid(): |
|
|
if form.is_valid(): |
|
|
self.used_parameters = { |
|
|
self.used_parameters = { |
|
|
key: value |
|
|
key: [value] |
|
|
for key, value in form.cleaned_data.items() |
|
|
for key, value in form.cleaned_data.items() |
|
|
if value is not None |
|
|
if value is not None |
|
|
} |
|
|
} |
|
@ -181,12 +199,15 @@ class DateRangeFilter(admin.FieldListFilter): |
|
|
return self.lookup_kwargs |
|
|
return self.lookup_kwargs |
|
|
|
|
|
|
|
|
def choices(self, changelist): |
|
|
def choices(self, changelist): |
|
|
|
|
|
form_params = { |
|
|
|
|
|
key: value[-1] for key, value in self.used_parameters.items() |
|
|
|
|
|
} |
|
|
return [ |
|
|
return [ |
|
|
{ |
|
|
{ |
|
|
"query_string": changelist.get_query_string( |
|
|
"query_string": changelist.get_query_string( |
|
|
remove=self.lookup_kwargs |
|
|
remove=self.lookup_kwargs |
|
|
), |
|
|
), |
|
|
"form": self.get_date_form(self.used_parameters, changelist), |
|
|
"form": self.get_date_form(form_params, changelist), |
|
|
} |
|
|
} |
|
|
] |
|
|
] |
|
|
|
|
|
|
|
@ -201,13 +222,15 @@ class DateRangeFilter(admin.FieldListFilter): |
|
|
"placeholder": ( |
|
|
"placeholder": ( |
|
|
_("De") if "__gte" in name else _("Até") |
|
|
_("De") if "__gte" in name else _("Até") |
|
|
), |
|
|
), |
|
|
"data-clear": changelist.get_query_string( |
|
|
"data-clear": ( |
|
|
|
|
|
changelist.get_query_string( |
|
|
remove=[ |
|
|
remove=[ |
|
|
name, |
|
|
name, |
|
|
] |
|
|
] |
|
|
) |
|
|
) |
|
|
if changelist |
|
|
if changelist |
|
|
else "", |
|
|
else "" |
|
|
|
|
|
), |
|
|
} |
|
|
} |
|
|
), |
|
|
), |
|
|
) |
|
|
) |
|
|