diff options
| author | Jannis Leidel <jannis@leidel.info> | 2011-06-12 13:04:53 +0000 |
|---|---|---|
| committer | Jannis Leidel <jannis@leidel.info> | 2011-06-12 13:04:53 +0000 |
| commit | f749bb829c715237c33c48c146ae78e1ed23ae71 (patch) | |
| tree | 8c4047c32b76bf6b71d407869b0d8dafa195bb17 /tests | |
| parent | f0adae4c6e857cabdff790d20dff815a37645d6f (diff) | |
Fixed #12875 -- Added get_ordering to ModelAdmin. Many thanks to Manuel Saelices.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16383 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/regressiontests/admin_ordering/models.py | 8 | ||||
| -rw-r--r-- | tests/regressiontests/admin_ordering/tests.py | 22 |
2 files changed, 28 insertions, 2 deletions
diff --git a/tests/regressiontests/admin_ordering/models.py b/tests/regressiontests/admin_ordering/models.py index fb766777a9..7c92b42010 100644 --- a/tests/regressiontests/admin_ordering/models.py +++ b/tests/regressiontests/admin_ordering/models.py @@ -24,3 +24,11 @@ class SongInlineDefaultOrdering(admin.StackedInline): class SongInlineNewOrdering(admin.StackedInline): model = Song ordering = ('duration', ) + +class DynOrderingBandAdmin(admin.ModelAdmin): + + def get_ordering(self, request): + if request.user.is_superuser: + return ['rank'] + else: + return ['name'] diff --git a/tests/regressiontests/admin_ordering/tests.py b/tests/regressiontests/admin_ordering/tests.py index 8f5a6bd05a..70eca5f771 100644 --- a/tests/regressiontests/admin_ordering/tests.py +++ b/tests/regressiontests/admin_ordering/tests.py @@ -1,7 +1,8 @@ -from django.test import TestCase +from django.test import TestCase, RequestFactory +from django.contrib.auth.models import User from django.contrib.admin.options import ModelAdmin -from models import Band, Song, SongInlineDefaultOrdering, SongInlineNewOrdering +from models import Band, Song, SongInlineDefaultOrdering, SongInlineNewOrdering, DynOrderingBandAdmin class TestAdminOrdering(TestCase): """ @@ -11,6 +12,7 @@ class TestAdminOrdering(TestCase): """ def setUp(self): + self.request_factory = RequestFactory() b1 = Band(name='Aerosmith', bio='', rank=3) b1.save() b2 = Band(name='Radiohead', bio='', rank=1) @@ -38,6 +40,22 @@ class TestAdminOrdering(TestCase): names = [b.name for b in ma.queryset(None)] self.assertEqual([u'Radiohead', u'Van Halen', u'Aerosmith'], names) + def test_dynamic_ordering(self): + """ + Let's use a custom ModelAdmin that changes the ordering dinamically. + """ + super_user = User.objects.create(username='admin', is_superuser=True) + other_user = User.objects.create(username='other') + request = self.request_factory.get('/') + request.user = super_user + ma = DynOrderingBandAdmin(Band, None) + names = [b.name for b in ma.queryset(request)] + self.assertEqual([u'Radiohead', u'Van Halen', u'Aerosmith'], names) + request.user = other_user + names = [b.name for b in ma.queryset(request)] + self.assertEqual([u'Aerosmith', u'Radiohead', u'Van Halen'], names) + + class TestInlineModelAdminOrdering(TestCase): """ Let's make sure that InlineModelAdmin.queryset uses the ordering we define |
