Skip to content

Commit

Permalink
Merge branch 'release/2.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
wolph committed May 17, 2019
2 parents a68ccf1 + a04e5d8 commit 16aa8fb
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 16 deletions.
10 changes: 9 additions & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,17 @@ exclude_lines =
if __name__ == .__main__.:

[run]
source = django_admin_generator
source =
django_admin_generator
test_project

branch = True
omit =
*/test_project/*
*/metadata.py

[paths]
source =
django_admin_generator
test_project

1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ env:
- TOX_ENV=py36-django20
- TOX_ENV=flake8
- TOX_ENV=docs
- TOX_ENV=coveralls

install:
- pip install --upgrade pip
Expand Down
2 changes: 1 addition & 1 deletion django_admin_generator/__about__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__package_name__ = 'django-admin-generator'
__version__ = '2.1.0'
__version__ = '2.2.0'
__author__ = 'Rick van Hattem'
__author_email__ = 'Wolph@Wol.ph'
__description__ = ' '.join(('''
Expand Down
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
11 changes: 10 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ def run_tests(self):
},
long_description=long_description,
cmdclass={'test': PyTest},
classifiers=['License :: OSI Approved :: BSD License'],
classifiers=[
'Development Status :: 5 - Production/Stable',
'Environment :: Web Environment',
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
'Natural Language :: English',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
],
)

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
31 changes: 25 additions & 6 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
[tox]
envlist =
py{py,27}-django11,
py35-django{11,20},
py36-django{11,20},
py35-django{11,21,22},
py36-django{11,21,22},
py37-django{11,21,22},
py38-django{11,21,22},
flake8,
docs

Expand All @@ -12,13 +14,16 @@ usedevelop = True
[testenv]
deps =
django11: Django<2.0
django20: Django>=2.0,<2.1
-rtest_project/requirements.txt
django21: Django>=2.1,<2.2
django22: Django>=2.2,<2.3
-r{toxinidir}/test_project/requirements.txt

envlist =
py{py,27}-django11,
py35-django{11,20},
py36-django{11,20},
py35-django{11,21,22},
py36-django{11,21,22},
py37-django{11,21,22},
py38-django{11,21,22}

commands =
python setup.py test
Expand All @@ -40,3 +45,17 @@ commands =
rm -f docs/modules.rst
sphinx-build -W -b html -d docs/_build/doctrees docs docs/_build/html {posargs}
deps = -r{toxinidir}/docs/requirements.txt

[testenv:coveralls]
passenv = TRAVIS TRAVIS_JOB_ID TRAVIS_BRANCH

commands =
pip freeze
python setup.py test
coveralls

deps =
Django>=2.0,<2.1
-r{toxinidir}/text_project/requirements.txt
coveralls

0 comments on commit 16aa8fb

Please sign in to comment.