예제 #1
0
    def get_context_data(self, forms, **kwargs):
        """
        Returns the template context for a step. You can overwrite this method
        to add more data for all or some steps. This method returns a
        dictionary containing the rendered form step. Available template
        context variables are:

        * ``wizard`` -- a container of useful data

        Example::

            class MyWizard(SessionWizardView):
                def get_context_data(self, form, **kwargs):
                    context = super(MyWizard, self).get_context_data(
                            form, **kwargs)
                    if self.steps.current.name == 'my_step_name':
                        context.update({'another_var': True})
                    return context

        """
        context = super(WizardMixin, self).get_context_data(**kwargs)
        context['wizard'] = Wizard(
            forms=forms,
            steps=self.steps,
            management_form=ManagementForm(prefix='mgmt',
                                           initial={
                                               'current_step':
                                               self.steps.current.name,
                                           }),
            as_html=lambda: self.get_wizard_html(
                RequestContext(self.request, context)),
            media=reduce(operator.add, (form.media for form in forms)),
        )
        return context
예제 #2
0
    def get_context_data(self, form, **kwargs):
        """
        Returns the template context for a step. You can overwrite this method
        to add more data for all or some steps. This method returns a
        dictionary containing the rendered form step. Available template
        context variables are:

         * all extra data stored in the storage backend
         * `form` - form instance of the current step
         * `wizard` - the wizard instance itself

        Example:

        .. code-block:: python

            class MyWizard(WizardView):
                def get_context_data(self, form, **kwargs):
                    context = super(MyWizard, self).get_context_data(form=form, **kwargs)
                    if self.steps.current == 'my_step_name':
                        context.update({'another_var': True})
                    return context
        """
        context = super(WizardView, self).get_context_data(**kwargs)
        context.update(self.storage.extra_data)
        context['wizard'] = {
            'form': form,
            'steps': self.steps,
            'management_form': ManagementForm(prefix=self.prefix, initial={
                'current_step': self.steps.current,
            }),
        }
        return context
예제 #3
0
    def post(self, *args, **kwargs):
        """
        This method handles POST requests.

        The wizard will render either the current step (if form validation
        wasn't successful), the next step (if the current step was stored
        successful) or the done view (if no more steps are available)
        """
        # Look for a wizard_prev_step element in the posted data which
        # contains a valid step name. If one was found, render the requested
        # form. (This makes stepping back a lot easier).
        wizard_prev_step = self.request.POST.get('wizard_prev_step', None)
        if wizard_prev_step and wizard_prev_step in self.get_form_list():
            self.storage.current_step = wizard_prev_step
            form = self.get_form(
                data=self.storage.get_step_data(self.steps.current),
                files=self.storage.get_step_files(self.steps.current))
            return self.render(form)

        # Check if form was refreshed
        management_form = ManagementForm(self.request.POST, prefix=self.prefix)
        if not management_form.is_valid():
            raise ValidationError(
                'ManagementForm data is missing or has been tampered.')

        form_current_step = management_form.cleaned_data['current_step']
        if (form_current_step != self.steps.current
                and self.storage.current_step is not None):
            # form refreshed, change current step
            self.storage.current_step = form_current_step

        # get the form for the current step
        form = self.get_form(data=self.request.POST, files=self.request.FILES)

        # and try to validate
        if form.is_valid():
            # if the form is valid, store the cleaned data and files.
            self.storage.set_step_data(self.steps.current,
                                       self.process_step(form))
            self.storage.set_step_files(self.steps.current,
                                        self.process_step_files(form))

            # check if the current step is the last step
            if self.steps.current == self.steps.last:
                # no more steps, render done view
                return self.render_done(form, **kwargs)
            else:
                # proceed to the next step
                return self.render_next_step(form)
        return self.render(form)
예제 #4
0
    def post(self, request, *args, **kwargs):
        """
        This method handles POST requests.

        The wizard will render either the current step (if form validation
        wasn't successful), the next step (if the current step was stored
        successful) or the done view (if no more steps are available)
        """
        # Look for a wizard_next_step element in the posted data which
        # contains a valid step name. If one was found, render the requested
        # form. (This makes stepping back a lot easier).
        wizard_next_step = self.request.POST.get('wizard_next_step')

        if wizard_next_step:
            step = self.steps[wizard_next_step]
            if step:
                step = self.steps[wizard_next_step]
                self.storage.current_step = step
                forms = self.get_validated_step_forms(step)
                return self.render(forms)

        # Check if form was refreshed
        management_form = ManagementForm(self.request.POST, prefix='mgmt')
        if not management_form.is_valid():
            raise ValidationError(
                'ManagementForm data is missing or has been tampered.')
        try:
            step = self.steps[management_form.cleaned_data['current_step']]
        except KeyError:
            raise ValidationError(
                "The current step specified in Wizard management form is invalid."
            )
        self.storage.current_step = step
        forms = self.get_validated_step_forms(step,
                                              data=self.request.POST,
                                              files=self.request.FILES)
        if all((form.is_valid() for form in forms)):
            # Update step with valid data
            step.data = self.request.POST
            step.files = self.request.FILES
            if step == self.steps.last:
                return self.render_done()
            else:
                return self.render_next_step()
        return self.render(forms)