Skip to content

Commit

Permalink
Fixed #9053 -- Allowed for sorting of callable and ModelAdmin methods…
Browse files Browse the repository at this point in the history
… specified in list_display (added in r8352). Previously attempting to sort on the former would raise an exception and the latter simply didn't sort. Also added tests for this function. Thanks rgl and jenan.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9211 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
kmtracey committed Oct 8, 2008
1 parent 7e7a370 commit 05d6959
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 21 deletions.
7 changes: 6 additions & 1 deletion django/contrib/admin/views/main.py
Expand Up @@ -157,7 +157,12 @@ def get_ordering(self):
# See whether field_name is a name of a non-field # See whether field_name is a name of a non-field
# that allows sorting. # that allows sorting.
try: try:
attr = getattr(self.model, field_name) if callable(field_name):
attr = field_name
elif hasattr(self.model_admin, field_name):
attr = getattr(self.model_admin, field_name)
else:
attr = getattr(self.model, field_name)
order_field = attr.admin_order_field order_field = attr.admin_order_field
except AttributeError: except AttributeError:
pass pass
Expand Down
14 changes: 13 additions & 1 deletion tests/regressiontests/admin_views/fixtures/admin-views-users.xml
Expand Up @@ -74,8 +74,20 @@
<field type="CharField" name="name">Test section</field> <field type="CharField" name="name">Test section</field>
</object> </object>
<object pk="1" model="admin_views.article"> <object pk="1" model="admin_views.article">
<field type="TextField" name="content">&lt;p&gt;test content&lt;/p&gt;</field> <field type="TextField" name="content">&lt;p&gt;Middle content&lt;/p&gt;</field>
<field type="DateTimeField" name="date">2008-03-18 11:54:58</field> <field type="DateTimeField" name="date">2008-03-18 11:54:58</field>
<field to="admin_views.section" name="section" rel="ManyToOneRel">1</field> <field to="admin_views.section" name="section" rel="ManyToOneRel">1</field>
</object> </object>
<object pk="2" model="admin_views.article">
<field type="TextField" name="content">&lt;p&gt;Oldest content&lt;/p&gt;</field>
<field type="DateTimeField" name="date">2000-03-18 11:54:58</field>
<field to="admin_views.section" name="section" rel="ManyToOneRel">1</field>
</object>
<object pk="3" model="admin_views.article">
<field type="TextField" name="content">&lt;p&gt;Newest content&lt;/p&gt;</field>
<field type="DateTimeField" name="date">2009-03-18 11:54:58</field>
<field to="admin_views.section" name="section" rel="ManyToOneRel">1</field>
</object>


</django-objects> </django-objects>
14 changes: 13 additions & 1 deletion tests/regressiontests/admin_views/models.py
Expand Up @@ -19,12 +19,20 @@ class Article(models.Model):


def __unicode__(self): def __unicode__(self):
return self.title return self.title

def model_year(self):
return self.date.year
model_year.admin_order_field = 'date'

def callable_year(dt_value):
return dt_value.year
callable_year.admin_order_field = 'date'


class ArticleInline(admin.TabularInline): class ArticleInline(admin.TabularInline):
model = Article model = Article


class ArticleAdmin(admin.ModelAdmin): class ArticleAdmin(admin.ModelAdmin):
list_display = ('content', 'date') list_display = ('content', 'date', callable_year, 'model_year', 'modeladmin_year')
list_filter = ('date',) list_filter = ('date',)


def changelist_view(self, request): def changelist_view(self, request):
Expand All @@ -34,6 +42,10 @@ def changelist_view(self, request):
'extra_var': 'Hello!' 'extra_var': 'Hello!'
} }
) )

def modeladmin_year(self, obj):
return obj.date.year
modeladmin_year.admin_order_field = 'date'


class CustomArticle(models.Model): class CustomArticle(models.Model):
content = models.TextField() content = models.TextField()
Expand Down
85 changes: 67 additions & 18 deletions tests/regressiontests/admin_views/tests.py
Expand Up @@ -71,34 +71,83 @@ def testBasicEditPost(self):
post_data = { post_data = {
"name": u"Test section", "name": u"Test section",
# inline data # inline data
"article_set-TOTAL_FORMS": u"4", "article_set-TOTAL_FORMS": u"6",
"article_set-INITIAL_FORMS": u"1", "article_set-INITIAL_FORMS": u"3",
"article_set-0-id": u"1", "article_set-0-id": u"1",
# there is no title in database, give one here or formset # there is no title in database, give one here or formset
# will fail. # will fail.
"article_set-0-title": u"Need a title.", "article_set-0-title": u"Need a title.",
"article_set-0-content": u"&lt;p&gt;test content&lt;/p&gt;", "article_set-0-content": u"&lt;p&gt;Middle content&lt;/p&gt;",
"article_set-0-date_0": u"2008-03-18", "article_set-0-date_0": u"2008-03-18",
"article_set-0-date_1": u"11:54:58", "article_set-0-date_1": u"11:54:58",
"article_set-1-id": u"", "article_set-1-id": u"2",
"article_set-1-title": u"", "article_set-1-title": u"Need a title.",
"article_set-1-content": u"", "article_set-1-content": u"&lt;p&gt;Oldest content&lt;/p&gt;",
"article_set-1-date_0": u"", "article_set-1-date_0": u"2000-03-18",
"article_set-1-date_1": u"", "article_set-1-date_1": u"11:54:58",
"article_set-2-id": u"", "article_set-2-id": u"3",
"article_set-2-title": u"", "article_set-2-title": u"Need a title.",
"article_set-2-content": u"", "article_set-2-content": u"&lt;p&gt;Newest content&lt;/p&gt;",
"article_set-2-date_0": u"", "article_set-2-date_0": u"2009-03-18",
"article_set-2-date_1": u"", "article_set-2-date_1": u"11:54:58",
"article_set-3-id": u"", "article_set-3-id": u"",
"article_set-3-title": u"", "article_set-3-title": u"",
"article_set-3-content": u"", "article_set-3-content": u"",
"article_set-3-date_0": u"", "article_set-3-date_0": u"",
"article_set-3-date_1": u"", "article_set-3-date_1": u"",
"article_set-4-id": u"",
"article_set-4-title": u"",
"article_set-4-content": u"",
"article_set-4-date_0": u"",
"article_set-4-date_1": u"",
"article_set-5-id": u"",
"article_set-5-title": u"",
"article_set-5-content": u"",
"article_set-5-date_0": u"",
"article_set-5-date_1": u"",
} }
response = self.client.post('/test_admin/admin/admin_views/section/1/', post_data) response = self.client.post('/test_admin/admin/admin_views/section/1/', post_data)
self.failUnlessEqual(response.status_code, 302) # redirect somewhere self.failUnlessEqual(response.status_code, 302) # redirect somewhere


def testChangeListSortingCallable(self):
"""
Ensure we can sort on a list_display field that is a callable
(column 2 is callable_year in ArticleAdmin)
"""
response = self.client.get('/test_admin/admin/admin_views/article/', {'ot': 'asc', 'o': 2})
self.failUnlessEqual(response.status_code, 200)
self.failUnless(
response.content.index('Oldest content') < response.content.index('Middle content') and
response.content.index('Middle content') < response.content.index('Newest content'),
"Results of sorting on callable are out of order."
)

def testChangeListSortingModel(self):
"""
Ensure we can sort on a list_display field that is a Model method
(colunn 3 is 'model_year' in ArticleAdmin)
"""
response = self.client.get('/test_admin/admin/admin_views/article/', {'ot': 'dsc', 'o': 3})
self.failUnlessEqual(response.status_code, 200)
self.failUnless(
response.content.index('Newest content') < response.content.index('Middle content') and
response.content.index('Middle content') < response.content.index('Oldest content'),
"Results of sorting on Model method are out of order."
)

def testChangeListSortingModelAdmin(self):
"""
Ensure we can sort on a list_display field that is a ModelAdmin method
(colunn 4 is 'modeladmin_year' in ArticleAdmin)
"""
response = self.client.get('/test_admin/admin/admin_views/article/', {'ot': 'asc', 'o': 4})
self.failUnlessEqual(response.status_code, 200)
self.failUnless(
response.content.index('Oldest content') < response.content.index('Middle content') and
response.content.index('Middle content') < response.content.index('Newest content'),
"Results of sorting on ModelAdmin method are out of order."
)

def get_perm(Model, perm): def get_perm(Model, perm):
"""Return the permission object, for the Model""" """Return the permission object, for the Model"""
ct = ContentType.objects.get_for_model(Model) ct = ContentType.objects.get_for_model(Model)
Expand Down Expand Up @@ -252,23 +301,23 @@ def testAddView(self):
# Try POST just to make sure # Try POST just to make sure
post = self.client.post('/test_admin/admin/admin_views/article/add/', add_dict) post = self.client.post('/test_admin/admin/admin_views/article/add/', add_dict)
self.failUnlessEqual(post.status_code, 403) self.failUnlessEqual(post.status_code, 403)
self.failUnlessEqual(Article.objects.all().count(), 1) self.failUnlessEqual(Article.objects.all().count(), 3)
self.client.get('/test_admin/admin/logout/') self.client.get('/test_admin/admin/logout/')


# Add user may login and POST to add view, then redirect to admin root # Add user may login and POST to add view, then redirect to admin root
self.client.get('/test_admin/admin/') self.client.get('/test_admin/admin/')
self.client.post('/test_admin/admin/', self.adduser_login) self.client.post('/test_admin/admin/', self.adduser_login)
post = self.client.post('/test_admin/admin/admin_views/article/add/', add_dict) post = self.client.post('/test_admin/admin/admin_views/article/add/', add_dict)
self.assertRedirects(post, '/test_admin/admin/') self.assertRedirects(post, '/test_admin/admin/')
self.failUnlessEqual(Article.objects.all().count(), 2) self.failUnlessEqual(Article.objects.all().count(), 4)
self.client.get('/test_admin/admin/logout/') self.client.get('/test_admin/admin/logout/')


# Super can add too, but is redirected to the change list view # Super can add too, but is redirected to the change list view
self.client.get('/test_admin/admin/') self.client.get('/test_admin/admin/')
self.client.post('/test_admin/admin/', self.super_login) self.client.post('/test_admin/admin/', self.super_login)
post = self.client.post('/test_admin/admin/admin_views/article/add/', add_dict) post = self.client.post('/test_admin/admin/admin_views/article/add/', add_dict)
self.assertRedirects(post, '/test_admin/admin/admin_views/article/') self.assertRedirects(post, '/test_admin/admin/admin_views/article/')
self.failUnlessEqual(Article.objects.all().count(), 3) self.failUnlessEqual(Article.objects.all().count(), 5)
self.client.get('/test_admin/admin/logout/') self.client.get('/test_admin/admin/logout/')


# 8509 - if a normal user is already logged in, it is possible # 8509 - if a normal user is already logged in, it is possible
Expand Down Expand Up @@ -392,7 +441,7 @@ def testDeleteView(self):
self.failUnlessEqual(request.status_code, 403) self.failUnlessEqual(request.status_code, 403)
post = self.client.post('/test_admin/admin/admin_views/article/1/delete/', delete_dict) post = self.client.post('/test_admin/admin/admin_views/article/1/delete/', delete_dict)
self.failUnlessEqual(post.status_code, 403) self.failUnlessEqual(post.status_code, 403)
self.failUnlessEqual(Article.objects.all().count(), 1) self.failUnlessEqual(Article.objects.all().count(), 3)
self.client.get('/test_admin/admin/logout/') self.client.get('/test_admin/admin/logout/')


# Delete user can delete # Delete user can delete
Expand All @@ -406,7 +455,7 @@ def testDeleteView(self):
self.failUnlessEqual(response.status_code, 200) self.failUnlessEqual(response.status_code, 200)
post = self.client.post('/test_admin/admin/admin_views/article/1/delete/', delete_dict) post = self.client.post('/test_admin/admin/admin_views/article/1/delete/', delete_dict)
self.assertRedirects(post, '/test_admin/admin/') self.assertRedirects(post, '/test_admin/admin/')
self.failUnlessEqual(Article.objects.all().count(), 0) self.failUnlessEqual(Article.objects.all().count(), 2)
self.client.get('/test_admin/admin/logout/') self.client.get('/test_admin/admin/logout/')


class AdminViewStringPrimaryKeyTest(TestCase): class AdminViewStringPrimaryKeyTest(TestCase):
Expand Down

0 comments on commit 05d6959

Please sign in to comment.