Skip to content

Commit

Permalink
Merge branch 'release/2.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
wolph committed Jan 26, 2023
2 parents aef6b24 + 30d1d06 commit 7ca09f0
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 23 deletions.
26 changes: 24 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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
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.4.0'
__version__ = '2.5.0'
__author__ = 'Rick van Hattem'
__author_email__ = '[email protected]'
__description__ = ' '.join(('''
Expand Down
36 changes: 31 additions & 5 deletions django_admin_generator/management/commands/admin_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = '''
Expand All @@ -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 = '''
Expand Down Expand Up @@ -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(
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[metadata]
description-file = README.rst
description_file = README.rst

[bdist_wheel]
universal = 1
Expand Down
56 changes: 56 additions & 0 deletions test_project/test_app/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -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',),
),
]
Empty file.
18 changes: 4 additions & 14 deletions test_project/test_app/models.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -23,4 +14,3 @@ class DateHierarchyModel(models.Model):
class SluggedField(models.Model):
name = models.CharField(max_length=100)
slug = models.SlugField(max_length=100)

Empty file.
7 changes: 7 additions & 0 deletions test_project/test_app/sub_models/eggs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.db import models

from .meat import Spam


class Eggs(Spam):
b = models.CharField(max_length=100)
5 changes: 5 additions & 0 deletions test_project/test_app/sub_models/meat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.db import models


class Spam(models.Model):
a = models.CharField(max_length=50)
10 changes: 10 additions & 0 deletions test_project/test_app/sub_models/models.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 7ca09f0

Please sign in to comment.