Skip to content

Commit

Permalink
[1.1.X] Fixed #15306 -- Replaced 1.1.X implementation of admin change…
Browse files Browse the repository at this point in the history
…list filtering security fix (r15031/r15033) with the one from trunk so another valid filter usage scenario (using model inheritance) is still possible. Thanks dbenamy for reporting this. Refs #15032.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@15555 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
ramiro committed Feb 16, 2011
1 parent 840314b commit 12fd6e1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
17 changes: 15 additions & 2 deletions django/contrib/admin/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,21 @@ def lookup_allowed(self, lookup, value):

# Special case -- foo__id__exact and foo__id queries are implied
# if foo has been specificially included in the lookup list; so
# drop __id if it is the last part.
if len(parts) > 1 and parts[-1] == self.model._meta.pk.name:
# drop __id if it is the last part. However, first we need to find
# the pk attribute name.
model = self.model
pk_attr_name = None
for part in parts[:-1]:
field, _, _, _ = model._meta.get_field_by_name(part)
if hasattr(field, 'rel'):
model = field.rel.to
pk_attr_name = model._meta.pk.name
elif isinstance(field, RelatedObject):
model = field.model
pk_attr_name = model._meta.pk.name
else:
pk_attr_name = None
if pk_attr_name and len(parts) > 1 and parts[-1] == pk_attr_name:
parts.pop()

try:
Expand Down
12 changes: 12 additions & 0 deletions tests/regressiontests/admin_views/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,17 @@ class Album(models.Model):
class AlbumAdmin(admin.ModelAdmin):
list_filter = ['title']

class Employee(Person):
code = models.CharField(max_length=20)

class WorkHour(models.Model):
datum = models.DateField()
employee = models.ForeignKey(Employee)

class WorkHourAdmin(admin.ModelAdmin):
list_display = ('datum', 'employee')
list_filter = ('employee',)

admin.site.register(Article, ArticleAdmin)
admin.site.register(CustomArticle, CustomArticleAdmin)
admin.site.register(Section, save_as=True, inlines=[ArticleInline])
Expand Down Expand Up @@ -565,6 +576,7 @@ class AlbumAdmin(admin.ModelAdmin):
admin.site.register(PlotDetails)
admin.site.register(CyclicOne)
admin.site.register(CyclicTwo)
admin.site.register(WorkHour, WorkHourAdmin)

# We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
# That way we cover all four cases:
Expand Down
12 changes: 11 additions & 1 deletion tests/regressiontests/admin_views/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
FooAccount, Gallery, ModelWithStringPrimaryKey, \
Person, Persona, Picture, Podcast, Section, Subscriber, Vodcast, \
Language, Collector, Widget, Grommet, DooHickey, FancyDoodad, Whatsit, \
Category, Plot, FunkyTag
Category, Plot, FunkyTag, WorkHour, Employee

try:
set
Expand Down Expand Up @@ -311,6 +311,16 @@ def test_allowed_filtering_15103(self):
except SuspiciousOperation:
self.fail("Filters should be allowed if they are defined on a ForeignKey pointing to this model")

e1 = Employee.objects.create(name='Anonymous', gender=1, age=22, alive=True, code='123')
e2 = Employee.objects.create(name='Visitor', gender=2, age=19, alive=True, code='124')
WorkHour.objects.create(datum=datetime.datetime.now(), employee=e1)
WorkHour.objects.create(datum=datetime.datetime.now(), employee=e2)
response = self.client.get("/test_admin/admin/admin_views/workhour/")
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'employee__person_ptr__exact')
response = self.client.get("/test_admin/admin/admin_views/workhour/?employee__person_ptr__exact=%d" % e1.pk)
self.assertEqual(response.status_code, 200)

class SaveAsTests(TestCase):
fixtures = ['admin-views-users.xml','admin-views-person.xml']

Expand Down

0 comments on commit 12fd6e1

Please sign in to comment.