Skip to content

Instantly share code, notes, and snippets.

@frague59 frague59/edit.py
Created Jun 13, 2016

Embed
What would you like to do?
BaseFormsetMixin
class BaseFormsetMixin(TemplateResponseMixin, FormMixin):
"""
Base formset mixin, which provide the use of a (model) form with an (inline) formset. This mixin can be used vith
an :class:`django:django.views.UpdateView` or a :class:`django:django.views.CreateView`
"""
formset_class = None
formset_visible_fields = None
formset_continuable = False
template_name = "base_form.html"
formset_initials = None
success_url = None
_formset_instance = None
request = None
def get_formset(self, formset_class=None, data=None):
"""
Gets the formset instance
:param formset_class: formset class
:param data: data
:returns: formset instance
"""
formset_class = formset_class or self.get_formset_class()
try:
# noinspection PyUnresolvedReferences
kwargs = self.get_formset_kwargs()
except AttributeError:
kwargs = {}
if data:
kwargs.update({'data': data})
instance = formset_class(**kwargs)
settings.DEBUG and logger.debug(u'BaseFormsetMixin::get_formset() type(instance) = %s', type(instance))
return instance
# if data:
# self._formset_instance = formset_class(data)
# else:
# self._formset_instance = formset_class()
# initial = self.get_formset_initial()
# initial_data = []
# for i in xrange(self._formset_instance.max_num):
# initial_data.append(initial)
# self._formset_instance.initial = initial_data
# return self._formset_instance
def get_formset_visible_fields(self):
"""
Gets the visible fields for the formset.
:returns: list of field names, :attr:`utils.generic.edit.BaseFormsetMixin.formset_visible_fields` if provided
"""
if self.formset_visible_fields:
return self.formset_visible_fields
return []
def get_formset_class(self):
"""
Gets the formset class
:returns: formset class, :attr:`utils.generic.edit.BaseFormsetMixin.formset_class` if provided
"""
if self.formset_class:
return self.formset_class
raise ImproperlyConfigured('%s must provide a formset_class attribute or a get_formset_class() implementation.'
% (self.__class__.__name__,))
def get_formset_initial(self):
if not self.formset_initials:
return {}
return self.formset_initials
def get_formset_continuable(self):
return self.formset_continuable
def get_success_url(self):
if self.success_url:
return self.success_url
raise ImproperlyConfigured('%s must provide a success_url attribute or a get_success_url() implementation.'
% (self.__class__.__name__,))
def is_continuable(self):
if self.request.method == 'POST':
return self.request.POST.get('formset_continuable', None) == 'on'
return False
def _get_continuable_success_url(self):
if self.is_continuable():
return '.' # self.request.get_full_path()
else:
return self.get_success_url()
def get_context_data(self, **kwargs):
context_data = {'form': self.get_form(self.get_form_class()),
'formset': self.get_formset(),
'formset_visible_fields': self.get_formset_visible_fields(),
'formset_continuable': self.get_formset_continuable()}
return context_data
def post(self, request, *args, **kwargs):
# Gets the form
form = self.get_form()
formset = self.get_formset()
form_is_valid = form.is_valid()
formset_is_valid = formset.is_valid()
settings.DEBUG and logger.debug(u'BaseFormsetMixin::post() '
u'form_is_valid = %s / formset_is_valid = %s',
form_is_valid, formset_is_valid)
if form_is_valid and formset_is_valid:
return self.form_valid()
else:
return self.form_invalid()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.