diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c342294..996c80d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -30,8 +30,6 @@ jobs: matrix: tox-env: ${{ fromJson(needs.setup.outputs.tox-envs) }} -# For some reason this build is broken on github actions -# if: ${{ matrix.tox-env != 'py37-django32' }} steps: - run: | PY=${{ matrix.tox-env }} @@ -45,3 +43,27 @@ jobs: cache: 'pip' - run: python -m pip install --upgrade tox - run: tox --skip-missing-interpreters -e ${{ matrix.tox-env }} + + docs: + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version: '3.10' + cache: 'pip' + - run: python -m pip install --upgrade tox + - run: tox --skip-missing-interpreters -e docs + + flake8: + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version: '3.10' + cache: 'pip' + - run: python -m pip install --upgrade tox + - run: tox --skip-missing-interpreters -e flake8 diff --git a/django_admin_generator/__about__.py b/django_admin_generator/__about__.py index 05ba737..4b4fc53 100644 --- a/django_admin_generator/__about__.py +++ b/django_admin_generator/__about__.py @@ -1,5 +1,5 @@ __package_name__ = 'django-admin-generator' -__version__ = '2.4.0' +__version__ = '2.5.0' __author__ = 'Rick van Hattem' __author_email__ = 'Wolph@Wol.ph' __description__ = ' '.join((''' diff --git a/django_admin_generator/management/commands/admin_generator.py b/django_admin_generator/management/commands/admin_generator.py index d6eb78c..4efee1c 100644 --- a/django_admin_generator/management/commands/admin_generator.py +++ b/django_admin_generator/management/commands/admin_generator.py @@ -15,6 +15,7 @@ def get_models(app): def get_apps(): for app_config in apps.get_app_configs(): yield app_config.name, app_config + yield app_config.name.rsplit('.')[-1], app_config MAX_LINE_WIDTH = 78 @@ -50,7 +51,6 @@ def get_apps(): PRINT_IMPORTS = '''# vim: set fileencoding=utf-8 : from django.contrib import admin -from . import models ''' PRINT_ADMIN_CLASS = ''' @@ -67,11 +67,11 @@ def _register(model, admin_class): ''' PRINT_ADMIN_REGISTRATION = ''' -_register(models.%(name)s, %(name)sAdmin)''' +_register(%(full_name)s, %(name)sAdmin)''' PRINT_ADMIN_REGISTRATION_LONG = ''' _register( - models.%(name)s, + %(full_name)s, %(name)sAdmin)''' PRINT_ADMIN_PROPERTY = ''' @@ -109,6 +109,30 @@ def __str__(self): # pragma: no cover def _unicode_generator(self): yield PRINT_IMPORTS + models = dict() + modules = dict() + module_names = dict() + for admin_model in sorted(self, key=lambda x: x.model.__module__): + model = admin_model.model + module = model.__module__ + # Get the module name if it was generated before or use the last + # part of the module path + name = modules.get(module, module.rsplit('.', 1)[-1]) + print(name, module, file=sys.stderr) + + # If the module name was already used, use the last two parts of + # the module path converting `project.spam.models` to `spam_models` + if module_names.get(name, module) != module: + name = '_'.join(module.rsplit('.', 2)[-2:]) + + # Store the module name and models for later use. + module_names[name] = module + modules[module] = name + models[admin_model.name] = name + + for module, name in sorted(modules.items()): + yield 'import %s as %s\n' % (module, name) + admin_model_names = [] for admin_model in self: yield PRINT_ADMIN_CLASS % dict( @@ -120,9 +144,11 @@ def _unicode_generator(self): yield PRINT_ADMIN_REGISTRATION_METHOD for name in admin_model_names: - row = PRINT_ADMIN_REGISTRATION % dict(name=name) + full_name = '%s.%s' % (models[name], name) + context = dict(name=name, full_name=full_name) + row = PRINT_ADMIN_REGISTRATION % context if len(row) > MAX_LINE_WIDTH: - row = PRINT_ADMIN_REGISTRATION_LONG % dict(name=name) + row = PRINT_ADMIN_REGISTRATION_LONG % context yield row def __repr__(self): diff --git a/setup.cfg b/setup.cfg index 61b41b9..7da0267 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [metadata] -description-file = README.rst +description_file = README.rst [bdist_wheel] universal = 1 diff --git a/test_project/test_app/migrations/0001_initial.py b/test_project/test_app/migrations/0001_initial.py new file mode 100644 index 0000000..4e09b03 --- /dev/null +++ b/test_project/test_app/migrations/0001_initial.py @@ -0,0 +1,56 @@ +# Generated by Django 4.0.8 on 2023-01-25 23:42 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='DateHierarchyModel', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('created_at', models.DateField()), + ], + ), + migrations.CreateModel( + name='SluggedField', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=100)), + ('slug', models.SlugField(max_length=100)), + ], + ), + migrations.CreateModel( + name='Spam', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('a', models.CharField(max_length=50)), + ], + ), + migrations.CreateModel( + name='ACollectionOfSpamAndEggsAndOtherStuff', + fields=[ + ('spam_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='test_app.spam')), + ('this_should_be_something_really_long_a', models.CharField(max_length=100)), + ('this_should_be_something_really_long_b', models.CharField(max_length=100)), + ('this_should_be_something_really_long_c', models.CharField(max_length=100)), + ('this_should_be_something_really_long_d', models.CharField(max_length=100)), + ], + bases=('test_app.spam',), + ), + migrations.CreateModel( + name='Eggs', + fields=[ + ('spam_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='test_app.spam')), + ('b', models.CharField(max_length=100)), + ], + bases=('test_app.spam',), + ), + ] diff --git a/test_project/test_app/migrations/__init__.py b/test_project/test_app/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test_project/test_app/models.py b/test_project/test_app/models.py index b7415b3..71503ca 100644 --- a/test_project/test_app/models.py +++ b/test_project/test_app/models.py @@ -1,19 +1,10 @@ from django.db import models +from .sub_models import eggs, meat, models as sub_models_models -class Spam(models.Model): - a = models.CharField(max_length=50) - - -class Eggs(Spam): - b = models.CharField(max_length=100) - - -class ACollectionOfSpamAndEggsAndOtherStuff(Spam): - this_should_be_something_really_long_a = models.CharField(max_length=100) - this_should_be_something_really_long_b = models.CharField(max_length=100) - this_should_be_something_really_long_c = models.CharField(max_length=100) - this_should_be_something_really_long_d = models.CharField(max_length=100) +assert eggs +assert meat +assert sub_models_models class DateHierarchyModel(models.Model): @@ -23,4 +14,3 @@ class DateHierarchyModel(models.Model): class SluggedField(models.Model): name = models.CharField(max_length=100) slug = models.SlugField(max_length=100) - diff --git a/test_project/test_app/sub_models/__init__.py b/test_project/test_app/sub_models/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test_project/test_app/sub_models/eggs.py b/test_project/test_app/sub_models/eggs.py new file mode 100644 index 0000000..19b2bce --- /dev/null +++ b/test_project/test_app/sub_models/eggs.py @@ -0,0 +1,7 @@ +from django.db import models + +from .meat import Spam + + +class Eggs(Spam): + b = models.CharField(max_length=100) diff --git a/test_project/test_app/sub_models/meat.py b/test_project/test_app/sub_models/meat.py new file mode 100644 index 0000000..4a7ee1a --- /dev/null +++ b/test_project/test_app/sub_models/meat.py @@ -0,0 +1,5 @@ +from django.db import models + + +class Spam(models.Model): + a = models.CharField(max_length=50) diff --git a/test_project/test_app/sub_models/models.py b/test_project/test_app/sub_models/models.py new file mode 100644 index 0000000..9a52e00 --- /dev/null +++ b/test_project/test_app/sub_models/models.py @@ -0,0 +1,10 @@ +from django.db import models + +from test_project.test_app.sub_models.meat import Spam + + +class ACollectionOfSpamAndEggsAndOtherStuff(Spam): + this_should_be_something_really_long_a = models.CharField(max_length=100) + this_should_be_something_really_long_b = models.CharField(max_length=100) + this_should_be_something_really_long_c = models.CharField(max_length=100) + this_should_be_something_really_long_d = models.CharField(max_length=100)