Skip to content

Commit

Permalink
Simple clean up changes:
Browse files Browse the repository at this point in the history
1. Moved BaseFormSetMixin.success_url to FormSetMixin.success_url, as this specifically relates to functionality of a subclassed view.
2. Removed ModelFormSetMixin.get_context_data() method as django now uses self.object_list correctly (rather than requiring it to be in **kwargs which was previously the case).
3. ModelFormSetMixin.get_factory_kwargs and InlineFormSetMixin.get_factory_kwargs were both calling self().get_formset_class() a second time even though this would already have been called by BaseFormSetMixin.factory_kwargs() as part of the super() call. The extra calls have been removed.
4. Removed explicit setting of BaseInlineFormSetMixin.formset_class and GenericInlineFormSetMixin.formset_class as the defaults for inlineformset_factory and genericinlineformset_factory set this automatically.
5. Removed BaseInlineFormSetMixin.get_context_data as this will already be set by SingleObjectMixin.get_context_data when subclassed.
  • Loading branch information
sdolemelipone committed Jun 1, 2018
1 parent 26195f3 commit bbd4f5d
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 45 deletions.
44 changes: 2 additions & 42 deletions extra_views/formsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from django.forms.models import modelformset_factory, inlineformset_factory
from django.views.generic.detail import SingleObjectMixin, SingleObjectTemplateResponseMixin
from django.views.generic.list import MultipleObjectMixin, MultipleObjectTemplateResponseMixin
from django.forms.models import BaseInlineFormSet


class BaseFormSetMixin(object):
Expand All @@ -16,7 +15,6 @@ class BaseFormSetMixin(object):
initial = []
form_class = None
formset_class = None
success_url = None
prefix = None
formset_kwargs = {}
factory_kwargs = {}
Expand Down Expand Up @@ -106,6 +104,7 @@ class FormSetMixin(BaseFormSetMixin, ContextMixin):
"""
A mixin that provides a way to show and handle a formset in a request.
"""
success_url = None

def get_success_url(self):
"""
Expand Down Expand Up @@ -140,25 +139,6 @@ class ModelFormSetMixin(FormSetMixin, MultipleObjectMixin):
exclude = None
fields = None

def get_context_data(self, **kwargs):
"""
If an object list has been supplied, inject it into the context with the
supplied context_object_name name.
"""
context = {}

if self.object_list is not None:
context['object_list'] = self.object_list
context_object_name = self.get_context_object_name(self.object_list)
if context_object_name:
context[context_object_name] = self.object_list
context.update(kwargs)

# MultipleObjectMixin get_context_data() doesn't work when object_list
# is not provided in kwargs, so we skip MultipleObjectMixin and call
# ContextMixin directly.
return ContextMixin.get_context_data(self, **context)

def get_formset_kwargs(self):
"""
Returns the keyword arguments for instantiating the formset.
Expand All @@ -177,8 +157,6 @@ def get_factory_kwargs(self):

if self.get_form_class():
kwargs['form'] = self.get_form_class()
if self.get_formset_class():
kwargs['formset'] = self.get_formset_class()
return kwargs

def get_formset(self):
Expand All @@ -201,25 +179,9 @@ class BaseInlineFormSetMixin(BaseFormSetMixin):
"""
model = None
inline_model = None
formset_class = BaseInlineFormSet
exclude = None
fields = None

def get_context_data(self, **kwargs):
"""
If an object has been supplied, inject it into the context with the
supplied context_object_name name.
"""
context = {}

if self.object:
context['object'] = self.object
context_object_name = self.get_context_object_name(self.object)
if context_object_name:
context[context_object_name] = self.object
context.update(kwargs)
return super(BaseInlineFormSetMixin, self).get_context_data(**context)

def get_inline_model(self):
"""
Returns the inline model to use with the inline formset
Expand Down Expand Up @@ -251,8 +213,6 @@ def get_factory_kwargs(self):

if self.get_form_class():
kwargs['form'] = self.get_form_class()
if self.get_formset_class():
kwargs['formset'] = self.get_formset_class()
return kwargs

def get_formset(self):
Expand All @@ -262,7 +222,7 @@ def get_formset(self):
return inlineformset_factory(self.model, self.get_inline_model(), **self.get_factory_kwargs())


class InlineFormSetMixin(BaseInlineFormSetMixin, FormSetMixin, SingleObjectMixin):
class InlineFormSetMixin(BaseInlineFormSetMixin, SingleObjectMixin, FormSetMixin):
"""
A mixin that provides a way to show and handle a inline formset in a request.
"""
Expand Down
4 changes: 1 addition & 3 deletions extra_views/generic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import django
from django.contrib.contenttypes.forms import generic_inlineformset_factory, \
BaseGenericInlineFormSet
from django.contrib.contenttypes.forms import generic_inlineformset_factory

from extra_views.formsets import BaseInlineFormSetMixin, InlineFormSetMixin, \
BaseInlineFormSetView, InlineFormSetView
Expand All @@ -10,7 +9,6 @@ class BaseGenericInlineFormSetMixin(BaseInlineFormSetMixin):
"""
Base class for constructing an generic inline formset within a view
"""
formset_class = BaseGenericInlineFormSet

def get_formset(self):
"""
Expand Down

0 comments on commit bbd4f5d

Please sign in to comment.