Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Refactored how the formsets are constructed

Merged all the code in a Mixin that looks for self.object and
instantiates the formsets with it or with the base model. This replaces
all the code that previously managed the formsets and their prefixes.
  • Loading branch information...
commit 3c1c28329ff2310191aa7ba29acfadc020771516 1 parent 27f8d4b
Ivan authored May 27, 2012

Showing 1 changed file with 46 additions and 50 deletions. Show diff stats Hide diff stats

  1. 96  django/contrib/admin/views/edit.py
96  django/contrib/admin/views/edit.py
@@ -12,6 +12,44 @@
12 12
 from django.db import models, router
13 13
 
14 14
 
  15
+class FormSetsMixin(object):
  16
+    def construct_formsets(self, **kwargs):
  17
+        """
  18
+        Constructs the formsets taking care of any clashing prefixes.
  19
+
  20
+        It accepts kwargs for the FormSet instantiating and adds the POST and
  21
+        FILES if available.
  22
+        """
  23
+
  24
+        prefixes = {}
  25
+        # Check if we have an instance or if we are creating a new one
  26
+        object = getattr(self, 'object', None)
  27
+
  28
+        for FormSet, inline in zip(self.admin_opts.get_formsets(
  29
+            self.request, object), self.inline_instances):
  30
+
  31
+            prefix = FormSet.get_default_prefix()
  32
+            prefixes[prefix] = prefixes.get(prefix, 0) + 1
  33
+            if prefixes[prefix] != 1 or not prefix:
  34
+                prefix = "%s-%s" % (prefix, prefixes[prefix])
  35
+
  36
+            formset_kwargs = {
  37
+                'prefix': prefix,
  38
+                'queryset': inline.queryset(self.request),
  39
+                'instance': object or self.model()
  40
+            }
  41
+
  42
+            if self.request.method in ('POST', 'PUT'):
  43
+                formset_kwargs.update({
  44
+                    'data': self.request.POST,
  45
+                    'files': self.request.FILES,
  46
+                })
  47
+
  48
+            formset_kwargs.update(kwargs)
  49
+
  50
+            self.formsets.append(FormSet(**formset_kwargs))
  51
+
  52
+
15 53
 class AdminDeleteView(AdminViewMixin, DeleteView):
16 54
 
17 55
     def dispatch(self, request, *args, **kwargs):
@@ -92,7 +130,7 @@ def get_template_names(self):
92 130
             ]
93 131
 
94 132
 
95  
-class AdminAddView(AdminViewMixin, CreateView):
  133
+class AdminAddView(AdminViewMixin, FormSetsMixin, CreateView):
96 134
 
97 135
     def dispatch(self, request, *args, **kwargs):
98 136
         if not self.admin_opts.has_add_permission(request):
@@ -118,7 +156,7 @@ def get_template_names(self):
118 156
 
119 157
     def form_valid(self, form):
120 158
         self.object = self.admin_opts.save_form(self.request, form, change=False)
121  
-        self._post_form_validation()
  159
+        self.construct_formsets(save_as_new="_saveasnew" in self.request.POST)
122 160
         if all_valid(self.formsets):
123 161
             self.admin_opts.save_model(self.request, self.object, form, False)
124 162
             self.admin_opts.save_related(self.request, form, self.formsets, False)
@@ -128,22 +166,9 @@ def form_valid(self, form):
128 166
 
129 167
     def form_invalid(self, form):
130 168
         self.object = self.model()
131  
-        self._post_form_validation()
  169
+        self.construct_formsets(save_as_new="_saveasnew" in self.request.POST)
132 170
         return self.render_to_response(self.get_context_data(form=form))
133 171
 
134  
-    def _post_form_validation(self):
135  
-        prefixes = {}
136  
-        for FormSet, inline in zip(self.admin_opts.get_formsets(self.request), self.inline_instances):
137  
-            prefix = FormSet.get_default_prefix()
138  
-            prefixes[prefix] = prefixes.get(prefix, 0) + 1
139  
-            if prefixes[prefix] != 1 or not prefix:
140  
-                prefix = "%s-%s" % (prefix, prefixes[prefix])
141  
-            formset = FormSet(data=self.request.POST, files=self.request.FILES,
142  
-                              instance=self.object,
143  
-                              save_as_new="_saveasnew" in self.request.POST,
144  
-                              prefix=prefix, queryset=inline.queryset(self.request))
145  
-            self.formsets.append(formset)
146  
-
147 172
     def get_form_kwargs(self):
148 173
         kwargs = super(AdminAddView, self).get_form_kwargs()
149 174
 
@@ -166,15 +191,7 @@ def render_to_response(self, context, **response_kwargs):
166 191
             form_url=self.form_url)
167 192
 
168 193
     def get(self, request, *args, **kwargs):
169  
-        prefixes = {}
170  
-        for FormSet, inline in zip(self.admin_opts.get_formsets(request), self.inline_instances):
171  
-            prefix = FormSet.get_default_prefix()
172  
-            prefixes[prefix] = prefixes.get(prefix, 0) + 1
173  
-            if prefixes[prefix] != 1 or not prefix:
174  
-                prefix = "%s-%s" % (prefix, prefixes[prefix])
175  
-            formset = FormSet(instance=self.model(), prefix=prefix,
176  
-                              queryset=inline.queryset(request))
177  
-            self.formsets.append(formset)
  194
+        self.construct_formsets()
178 195
         return super(CreateView, self).get(request, *args, **kwargs)
179 196
 
180 197
     def get_context_data(self, **kwargs):
@@ -211,8 +228,7 @@ def get_context_data(self, **kwargs):
211 228
         return context
212 229
 
213 230
 
214  
-
215  
-class AdminChangeView(AdminViewMixin, UpdateView):
  231
+class AdminChangeView(AdminViewMixin, FormSetsMixin, UpdateView):
216 232
 
217 233
     def dispatch(self, request, *args, **kwargs):
218 234
         if request.method == 'POST' and "_saveasnew" in request.POST:
@@ -241,15 +257,7 @@ def get_template_names(self):
241 257
 
242 258
     def get(self, request, *args, **kwargs):
243 259
         self.object = self.get_object()
244  
-        prefixes = {}
245  
-        for FormSet, inline in zip(self.admin_opts.get_formsets(self.request, self.object), self.inline_instances):
246  
-            prefix = FormSet.get_default_prefix()
247  
-            prefixes[prefix] = prefixes.get(prefix, 0) + 1
248  
-            if prefixes[prefix] != 1 or not prefix:
249  
-                prefix = "%s-%s" % (prefix, prefixes[prefix])
250  
-            formset = FormSet(instance=self.object, prefix=prefix,
251  
-                              queryset=inline.queryset(request))
252  
-            self.formsets.append(formset)
  260
+        self.construct_formsets()
253 261
         return super(UpdateView, self).get(request, *args, **kwargs)
254 262
 
255 263
     def get_object(self, queryset=None):
@@ -270,21 +278,9 @@ def render_to_response(self, context, **response_kwargs):
270 278
             self.request, context, change=True, obj=self.object,
271 279
             form_url=self.form_url)
272 280
 
273  
-    def _post_form_validation(self):
274  
-        prefixes = {}
275  
-        for FormSet, inline in zip(self.admin_opts.get_formsets(self.request, self.object), self.inline_instances):
276  
-            prefix = FormSet.get_default_prefix()
277  
-            prefixes[prefix] = prefixes.get(prefix, 0) + 1
278  
-            if prefixes[prefix] != 1 or not prefix:
279  
-                prefix = "%s-%s" % (prefix, prefixes[prefix])
280  
-            formset = FormSet(self.request.POST, self.request.FILES,
281  
-                              instance=self.object, prefix=prefix,
282  
-                              queryset=inline.queryset(self.request))
283  
-            self.formsets.append(formset)
284  
-
285 281
     def form_valid(self, form):
286 282
         self.object = self.admin_opts.save_form(self.request, form, change=True)
287  
-        self._post_form_validation()
  283
+        self.construct_formsets()
288 284
         if all_valid(self.formsets):
289 285
             self.admin_opts.save_model(self.request, self.object, form, True)
290 286
             self.admin_opts.save_related(self.request, form, self.formsets, True)
@@ -294,7 +290,7 @@ def form_valid(self, form):
294 290
         return self.render_to_response(self.get_context_data(form=form))
295 291
 
296 292
     def form_invalid(self, form):
297  
-        self._post_form_validation()
  293
+        self.construct_formsets()
298 294
         return self.render_to_response(self.get_context_data(form=form))
299 295
 
300 296
     def get_context_data(self, **kwargs):

0 notes on commit 3c1c283

Please sign in to comment.
Something went wrong with that request. Please try again.