Commits

Borislav Borisov committed 444fe13 Draft

Fix class Meta inheritance

Comments (0)

Files changed (2)

form_utils/forms.py

         raise ValueError('"fieldsets" must be an iterable of two-tuples, '
                          'and the second tuple must be a dictionary '
                          'with a "fields" key')
-    return fields
+    return fields or None
 
 
 def get_row_attrs(bases, attrs):
 from django import forms
 from django import template
 from django.core.files.uploadedfile import SimpleUploadedFile
-from django.db.models.fields.files import FieldFile, ImageFieldFile, FileField, ImageField
+from django.db.models.fields.files import (
+    FieldFile, ImageFieldFile, FileField, ImageField)
 from django.test import TestCase
 from django.utils import six
 
 
 from .models import Person, Document
 
+
 class ApplicationForm(BetterForm):
     """
     A sample form with fieldsets.
     name = forms.CharField()
     position = forms.CharField()
     reference = forms.CharField(required=False)
+
     class Meta:
         fieldsets = (('main', {'fields': ('name', 'position'), 'legend': ''}),
                      ('Optional', {'fields': ('reference',),
                                    'classes': ('optional',)}))
 
+
 class InheritedForm(ApplicationForm):
     """
     An inherited form that does not define its own fieldsets inherits
     """
     pass
 
+
 class MudSlingerApplicationForm(ApplicationForm):
     """
     Inherited forms can manually inherit and change/override the
 
     """
     target = forms.CharField()
+
     class Meta:
         fieldsets = list(ApplicationForm.Meta.fieldsets)
         fieldsets[0] = ('main', {'fields': ('name', 'position', 'target'),
     """
     honeypot = forms.CharField()
     name = forms.CharField()
+
     class Meta:
         row_attrs = {'honeypot': {'style': 'display: none'}}
 
 
     """
     title = forms.CharField()
+
     class Meta:
         model = Person
         fieldsets = [('main', {'fields': ['name'],
         model = Person
         fieldsets = [('main', {'fields': ['name']})]
 
+
 class ManualPartialPersonForm(BetterModelForm):
     """
     A ``BetterModelForm`` whose fieldsets don't contain all fields
         fieldsets = [('main', {'fields': ['name']})]
         fields = ['name', 'age']
 
+
 class ExcludePartialPersonForm(BetterModelForm):
     """
     A ``BetterModelForm`` whose fieldsets don't contain all fields
         fieldsets = [('main', {'fields': ['name']})]
         exclude = ['age']
 
+
 class AcrobaticPersonForm(PersonForm):
     """
     A ``BetterModelForm`` that inherits from another and overrides one
     """
     agility = forms.IntegerField()
     speed = forms.IntegerField()
+
     class Meta(PersonForm.Meta):
         fieldsets = list(PersonForm.Meta.fieldsets)
-        fieldsets = fieldsets[:1] + \
-                     [('Acrobatics', {'fields': ('age', 'speed', 'agility')})]
+        fieldsets = fieldsets[:1] + [
+            ('Acrobatics', {'fields': ('age', 'speed', 'agility')})]
+
+
+class AbstractPersonForm(BetterModelForm):
+    """
+    An abstract ``BetterModelForm`` without fieldsets.
+
+    """
+    title = forms.CharField()
+
+    class Meta:
+        pass
+
+
+class InheritedMetaAbstractPersonForm(AbstractPersonForm):
+    """
+    A ``BetterModelForm`` that inherits from abstract one and its Meta class
+    and adds fieldsets
+
+    """
+    class Meta(AbstractPersonForm.Meta):
+        model = Person
+        fieldsets = [('main', {'fields': ['name'],
+                               'legend': '',
+                               'classes': ['main']}),
+                     ('More', {'fields': ['age'],
+                               'description': 'Extra information',
+                               'classes': ['more', 'collapse']}),
+                     (None, {'fields': ['title']})]
 
 
 class BetterFormTests(TestCase):
                                 'classes': ''
                                 }),
                     ],
+            InheritedMetaAbstractPersonForm:
+            [
+                    (['name'],
+                     {
+                                'name': 'main',
+                                'legend': '',
+                                'description': '',
+                                'classes': 'main',
+                                }),
+                    (['age'],
+                    {
+                                'name': 'More',
+                                'legend': 'More',
+                                'description': 'Extra information',
+                                'classes': 'more collapse'
+                                }),
+                    (['title'],
+                    {
+                                'name': None,
+                                'legend': None,
+                                'description': '',
+                                'classes': ''
+                                }),
+                    ],
+
+
         }
 
     def test_iterate_fieldsets(self):