Skip to content

Commit

Permalink
added automatic list filters where useful
Browse files Browse the repository at this point in the history
  • Loading branch information
wolph committed May 17, 2019
1 parent b88c005 commit e097246
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
34 changes: 27 additions & 7 deletions django_admin_generator/management/commands/admin_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def get_apps():
'slug=name',
)

DATE_HIERARCHY_THRESHOLD = 250
LIST_FILTER_THRESHOLD = 25
RAW_ID_THRESHOLD = 100
NO_QUERY_DB = False
Expand Down Expand Up @@ -147,6 +148,7 @@ class AdminModel(object):
)

def __init__(self, model, raw_id_threshold=RAW_ID_THRESHOLD,
date_hierarchy_threshold=DATE_HIERARCHY_THRESHOLD,
list_filter_threshold=LIST_FILTER_THRESHOLD,
search_field_names=SEARCH_FIELD_NAMES,
date_hierarchy_names=DATE_HIERARCHY_NAMES,
Expand All @@ -162,6 +164,7 @@ def __init__(self, model, raw_id_threshold=RAW_ID_THRESHOLD,
self.search_field_names = search_field_names
self.raw_id_threshold = raw_id_threshold
self.list_filter_threshold = list_filter_threshold
self.date_hierarchy_threshold = date_hierarchy_threshold
self.date_hierarchy_names = date_hierarchy_names
self.prepopulated_field_names = prepopulated_field_names
self.query_db = not no_query_db
Expand Down Expand Up @@ -295,15 +298,26 @@ def _unicode_generator(self):

def _process(self):
meta = self.model._meta
qs = self.model.objects.all()

if self.query_db:
self.raw_id_fields += list(self._process_many_to_many(meta))

field_names = list(self._process_fields(meta))

for field_name in self.date_hierarchy_names[::-1]:
if field_name in field_names and not self.date_hierarchy:
self.date_hierarchy = field_name
break
if self.query_db:
threshold = self.list_filter_threshold + 1
for field in field_names:
distinct_count = len(qs.only(field).distinct()[:threshold])
if distinct_count <= self.list_filter_threshold:
self.list_filter.append(field)

if self.query_db:
if qs.count() < self.date_hierarchy_threshold:
for field_name in self.date_hierarchy_names[::-1]:
if field_name in field_names and not self.date_hierarchy:
self.date_hierarchy = field_name
break

for k in sorted(self.prepopulated_field_names):
k, vs = k.split('=', 1)
Expand Down Expand Up @@ -339,6 +353,12 @@ def add_arguments(self, parser):
default=DATE_HIERARCHY_NAMES,
help='A field named like this will be set as `date_hierarchy`'
' [default: %default]')
parser.add_argument(
'--date-hierarchy-threshold', type=int,
default=DATE_HIERARCHY_THRESHOLD,
metavar='DATE_HIERARCHY_THRESHOLD',
help='If a model has less than DATE_HIERARCHY_THRESHOLD items '
'it will be added to `date_hierarchy` [default: %default]')
parser.add_argument(
'-p', '--prepopulated-fields', action='append',
default=PREPOPULATED_FIELD_NAMES,
Expand All @@ -348,8 +368,8 @@ def add_arguments(self, parser):
parser.add_argument(
'-l', '--list-filter-threshold', type=int,
default=LIST_FILTER_THRESHOLD, metavar='LIST_FILTER_THRESHOLD',
help='If a foreign key has less than LIST_FILTER_THRESHOLD items '
'it will be added to `list_filter` [default: %default]')
help='If a foreign key/field has less than LIST_FILTER_THRESHOLD '
'items it will be added to `list_filter` [default: %default]')
parser.add_argument(
'-r', '--raw-id-threshold', type=int,
default=RAW_ID_THRESHOLD, metavar='RAW_ID_THRESHOLD',
Expand All @@ -358,7 +378,7 @@ def add_arguments(self, parser):
parser.add_argument(
'-n', '--no-query-db', action="store_true", dest='no_query_db',
help='Don\'t query the database in order to decide whether '
'relationships are added to `list_filter`')
'fields/relationships are added to `list_filter`')
parser.add_argument(
'app',
help='App to generate admin definitions for')
Expand Down
2 changes: 2 additions & 0 deletions test_project/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

DEFAULTS = {
'date_hierarchy_names': 'date_joined',
'date_hierarchy_threshold': 0,
'list_filter_threshold': 0,
'raw_id_threshold': 0,
'prepopulated_field_names': (
Expand All @@ -15,6 +16,7 @@
}

DEFAULTS_FILTERED = DEFAULTS.copy()
DEFAULTS_FILTERED['date_hierarchy_threshold'] = 250
DEFAULTS_FILTERED['list_filter_threshold'] = 250
DEFAULTS_FILTERED['raw_id_threshold'] = 250

Expand Down

0 comments on commit e097246

Please sign in to comment.