"""
Mix-ins for building your own `Django Form Tools <https://github.com/django/django-formtools>`_
powered views.
"""
from pytoolbox.django import forms
__all__ = ['CrispyFormsMixin', 'DataTableViewCompositionMixin', 'SerializeStepInstanceMixin']
[docs]class DataTableViewCompositionMixin(object):
"""Compose the wizard with some tables views."""
table_view_classes = {}
[docs] def get(self, request, *args, **kwargs):
"""Retrieve the table view and delegate AJAX to the table view."""
if self.request.is_ajax():
view = self.get_table_view()
if view:
return view.get_ajax(request, *args, **kwargs)
return super().get(request, *args, **kwargs)
[docs] def get_context_data(self, **kwargs):
"""Update the context with the context returned by the table view."""
context = super().get_context_data(**kwargs)
view = self.get_table_view()
if view:
context.update(view.get_context_data())
return context
[docs] def get_table_view(self):
"""Return an instance of the datatable-view for current step, defaults to None."""
view_class = self.table_view_classes.get(self.steps.current)
if view_class:
view = view_class()
view.object_list = None
view.request = self.request
return view
[docs]class SerializeStepInstanceMixin(object):
serialized_instance_form_class = forms.SerializedInstanceForm
serialized_instances_key = 'serialized-instances'
@property
def serialized_instances(self):
try:
return self.storage.extra_data[self.serialized_instances_key]
except KeyError:
value = self.storage.extra_data[self.serialized_instances_key] = {}
return value
[docs] def serialize_step_instance(self, form, step=None):
self.serialized_instances[step or self.steps.current] = \
self.serialized_instance_form_class.serialize(form.save())
# WizardView "Standard Methods"