Code

Ticket #494: 494.r12661.diff

File 494.r12661.diff, 6.1 KB (added by schinckel, 4 years ago)

I've updated the patch so that it doesn't use an empty list as the default argument for classes: this doesn't necessarily do what you expect it to. A second or tertiary call to the same function will use the same list, even if you have added things to it in the meantime, meaning that classes may get added to other objects that were not supposed to. At this stage, that is unlikely anyway, but it is bad form to create a function definition with an empty list as the default argument to anything. This patch works with r12661.

Line 
1Index: django/forms/formsets.py
2===================================================================
3--- django/forms/formsets.py    (revision 12662)
4+++ django/forms/formsets.py    (working copy)
5@@ -321,11 +321,11 @@
6         return mark_safe(u'\n'.join([unicode(self.management_form), forms]))
7 
8 def formset_factory(form, formset=BaseFormSet, extra=1, can_order=False,
9-                    can_delete=False, max_num=0):
10+                    can_delete=False, max_num=0, classes=None):
11     """Return a FormSet for the given form class."""
12     attrs = {'form': form, 'extra': extra,
13              'can_order': can_order, 'can_delete': can_delete,
14-             'max_num': max_num}
15+             'max_num': max_num, 'classes':classes or []}
16     return type(form.__name__ + 'FormSet', (formset,), attrs)
17 
18 def all_valid(formsets):
19Index: django/forms/models.py
20===================================================================
21--- django/forms/models.py      (revision 12662)
22+++ django/forms/models.py      (working copy)
23@@ -643,7 +643,7 @@
24         super(BaseModelFormSet, self).add_fields(form, index)
25 
26 def modelformset_factory(model, form=ModelForm, formfield_callback=lambda f: f.formfield(),
27-                         formset=BaseModelFormSet,
28+                         formset=BaseModelFormSet, classes=None,
29                          extra=1, can_delete=False, can_order=False,
30                          max_num=0, fields=None, exclude=None):
31     """
32@@ -652,7 +652,8 @@
33     form = modelform_factory(model, form=form, fields=fields, exclude=exclude,
34                              formfield_callback=formfield_callback)
35     FormSet = formset_factory(form, formset, extra=extra, max_num=max_num,
36-                              can_order=can_order, can_delete=can_delete)
37+                              can_order=can_order, can_delete=can_delete,
38+                              classes=classes or [])
39     FormSet.model = model
40     return FormSet
41 
42@@ -794,7 +795,7 @@
43 
44 def inlineformset_factory(parent_model, model, form=ModelForm,
45                           formset=BaseInlineFormSet, fk_name=None,
46-                          fields=None, exclude=None,
47+                          fields=None, exclude=None, classes=None,
48                           extra=3, can_order=False, can_delete=True, max_num=0,
49                           formfield_callback=lambda f: f.formfield()):
50     """
51@@ -817,6 +818,7 @@
52         'fields': fields,
53         'exclude': exclude,
54         'max_num': max_num,
55+        'classes': classes or [],
56     }
57     FormSet = modelformset_factory(model, **kwargs)
58     FormSet.fk = fk
59Index: django/contrib/admin/options.py
60===================================================================
61--- django/contrib/admin/options.py     (revision 12662)
62+++ django/contrib/admin/options.py     (working copy)
63@@ -1188,6 +1188,7 @@
64     verbose_name = None
65     verbose_name_plural = None
66     can_delete = True
67+    classes = []
68 
69     def __init__(self, parent_model, admin_site):
70         self.admin_site = admin_site
71@@ -1204,6 +1205,8 @@
72         js = ['js/jquery.min.js', 'js/inlines.min.js']
73         if self.prepopulated_fields:
74             js.append('js/urlify.js')
75+        if 'collapse' in self.classes:
76+            js.append('js/collapse.min.js')
77         if self.filter_vertical or self.filter_horizontal:
78             js.extend(['js/SelectBox.js' , 'js/SelectFilter2.js'])
79         return forms.Media(js=['%s%s' % (settings.ADMIN_MEDIA_PREFIX, url) for url in js])
80@@ -1234,6 +1237,7 @@
81             "extra": self.extra,
82             "max_num": self.max_num,
83             "can_delete": self.can_delete,
84+            "classes": self.classes,
85         }
86         defaults.update(kwargs)
87         return inlineformset_factory(self.parent_model, self.model, **defaults)
88Index: django/contrib/admin/templates/admin/edit_inline/tabular.html
89===================================================================
90--- django/contrib/admin/templates/admin/edit_inline/tabular.html       (revision 12662)
91+++ django/contrib/admin/templates/admin/edit_inline/tabular.html       (working copy)
92@@ -2,7 +2,7 @@
93 <div class="inline-group" id="{{ inline_admin_formset.formset.prefix }}-group">
94   <div class="tabular inline-related {% if forloop.last %}last-related{% endif %}">
95 {{ inline_admin_formset.formset.management_form }}
96-<fieldset class="module">
97+<fieldset class="module {% for class in inline_admin_formset.formset.classes %} {{class}}{% endfor %}">
98    <h2>{{ inline_admin_formset.opts.verbose_name_plural|capfirst }}</h2>
99    {{ inline_admin_formset.formset.non_form_errors }}
100    <table>
101Index: django/contrib/contenttypes/generic.py
102===================================================================
103--- django/contrib/contenttypes/generic.py      (revision 12662)
104+++ django/contrib/contenttypes/generic.py      (working copy)
105@@ -335,7 +335,7 @@
106 def generic_inlineformset_factory(model, form=ModelForm,
107                                   formset=BaseGenericInlineFormSet,
108                                   ct_field="content_type", fk_field="object_id",
109-                                  fields=None, exclude=None,
110+                                  fields=None, exclude=None, classes=None,
111                                   extra=3, can_order=False, can_delete=True,
112                                   max_num=0,
113                                   formfield_callback=lambda f: f.formfield()):
114@@ -360,7 +360,7 @@
115         exclude = [ct_field.name, fk_field.name]
116     FormSet = modelformset_factory(model, form=form,
117                                    formfield_callback=formfield_callback,
118-                                   formset=formset,
119+                                   formset=formset, classes=classes or [],
120                                    extra=extra, can_delete=can_delete, can_order=can_order,
121                                    fields=fields, exclude=exclude, max_num=max_num)
122     FormSet.ct_field = ct_field
123@@ -394,7 +394,8 @@
124             "can_order": False,
125             "fields": fields,
126             "max_num": self.max_num,
127-            "exclude": exclude
128+            "exclude": exclude,
129+            "classes": classes,
130         }
131         return generic_inlineformset_factory(self.model, **defaults)
132