From 013db6ba85fb880bd1f9a5ad2e91dc5c1efe197c Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Mon, 21 Jan 2013 22:34:36 +0100 Subject: [PATCH] Fixed #18051 -- Allowed admin fieldsets to contain lists Thanks Ricardo di Virgilio for the report, Mateus Gondim for the patch and Nick Sandford for the review. --- django/contrib/admin/util.py | 3 +-- tests/regressiontests/admin_util/tests.py | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/django/contrib/admin/util.py b/django/contrib/admin/util.py index a85045c515..07013d1d4b 100644 --- a/django/contrib/admin/util.py +++ b/django/contrib/admin/util.py @@ -88,8 +88,7 @@ def flatten_fieldsets(fieldsets): field_names = [] for name, opts in fieldsets: for field in opts['fields']: - # type checking feels dirty, but it seems like the best way here - if type(field) == tuple: + if isinstance(field, (list, tuple)): field_names.extend(field) else: field_names.append(field) diff --git a/tests/regressiontests/admin_util/tests.py b/tests/regressiontests/admin_util/tests.py index e9e122a9f0..274103cf87 100644 --- a/tests/regressiontests/admin_util/tests.py +++ b/tests/regressiontests/admin_util/tests.py @@ -5,8 +5,8 @@ from datetime import datetime from django.conf import settings from django.contrib import admin from django.contrib.admin import helpers -from django.contrib.admin.util import (display_for_field, label_for_field, - lookup_field, NestedObjects) +from django.contrib.admin.util import (display_for_field, flatten_fieldsets, + label_for_field, lookup_field, NestedObjects) from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE from django.contrib.sites.models import Site from django.db import models, DEFAULT_DB_ALIAS @@ -300,3 +300,21 @@ class UtilTests(unittest.TestCase): '') self.assertEqual(helpers.AdminField(form, 'cb', is_first=False).label_tag(), '') + + def test_flatten_fieldsets(self): + """ + Regression test for #18051 + """ + fieldsets = ( + (None, { + 'fields': ('url', 'title', ('content', 'sites')) + }), + ) + self.assertEqual(flatten_fieldsets(fieldsets), ['url', 'title', 'content', 'sites']) + + fieldsets = ( + (None, { + 'fields': ['url', 'title', ['content', 'sites']) + }), + ) + self.assertEqual(flatten_fieldsets(fieldsets), ['url', 'title', 'content', 'sites'])