Ticket #11112: wizard.patch

File wizard.patch, 2.8 KB (added by hlecuanda <hector@…>, 15 years ago)

Handle Formsets in FormWizard

  • wizard.py

    old new  
    44stored on the server side.
    55"""
    66
    7 import cPickle as pickle
    8 
    97from django import forms
    108from django.conf import settings
    119from django.http import Http404
     
    6260        # TODO: Move "hash_%d" to a method to make it configurable.
    6361        for i in range(current_step):
    6462            form = self.get_form(i, request.POST)
     63            # Handle formsets
     64            if issubclass(form.__class__,forms.formsets.BaseFormSet):
     65                # checkhash for management form
     66                if request.POST.get("hash_%d" % i, '') != self.security_hash(request, form.management_form):
     67                    return self.render_hash_failure(request, i)
     68                for f in form.forms:
     69                    # check hash in every form
     70                    if request.POST.get("hash_%d_%s" % (i,f.prefix)) != self.security_hash(request,f):
     71                        return self.render_hash_failure(request,i)
    6572            if request.POST.get("hash_%d" % i, '') != self.security_hash(request, form):
    6673                return self.render_hash_failure(request, i)
    6774            self.process_step(request, form, i)
     
    107114            for i in range(step):
    108115                old_form = self.get_form(i, old_data)
    109116                hash_name = 'hash_%s' % i
    110                 prev_fields.extend([bf.as_hidden() for bf in old_form])
    111                 prev_fields.append(hidden.render(hash_name, old_data.get(hash_name, self.security_hash(request, old_form))))
     117                # handle formsets
     118                if issubclass(old_form.__class__,forms.formsets.BaseFormSet):
     119                    # do management form and generate hash
     120                    prev_fields.extend([bf.as_hidden() for bf in old_form.management_form])
     121                    prev_fields.append(hidden.render(hash_name, old_data.get(hash_name, self.security_hash(request, old_form.management_form))))
     122                    for f in old_form.forms:
     123                        # do each form and generate a hash for each
     124                        hash_name = 'hash_%s_%s' % (i,f.prefix)
     125                        prev_fields.extend([bf.as_hidden() for bf in f])
     126                        prev_fields.append(hidden.render(hash_name, old_data.get(hash_name, self.security_hash(request, f))))
     127                else:
     128                    prev_fields.extend([bf.as_hidden() for bf in old_form])
     129                    prev_fields.append(hidden.render(hash_name, old_data.get(hash_name, self.security_hash(request, old_form))))
     130
    112131        return self.render_template(request, form, ''.join(prev_fields), step, context)
    113132
    114133    # METHODS SUBCLASSES MIGHT OVERRIDE IF APPROPRIATE ########################
Back to Top