<s> """<STR_LIT>""" <EOL> import cPickle as pickle <EOL> from django import forms <EOL> from django . conf import settings <EOL> from django . http import Http404 <EOL> from django . shortcuts import render_to_response <EOL> from django . template . context import RequestContext <EOL> from django . utils . hashcompat import md5_constructor <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . contrib . formtools . utils import security_hash <EOL> from django . utils . decorators import method_decorator <EOL> from django . views . decorators . csrf import csrf_protect <EOL> class FormWizard ( object ) : <EOL> step_field_name = "<STR_LIT>" <EOL> def __init__ ( self , form_list , initial = None ) : <EOL> """<STR_LIT>""" <EOL> self . form_list = form_list [ : ] <EOL> self . initial = initial or { } <EOL> self . extra_context = { } <EOL> self . step = <NUM_LIT:0> <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . step , self . form_list , self . initial ) <EOL> def get_form ( self , step , data = None ) : <EOL> "<STR_LIT>" <EOL> return self . form_list [ step ] ( data , prefix = self . prefix_for_step ( step ) , initial = self . initial . get ( step , None ) ) <EOL> def num_steps ( self ) : <EOL> "<STR_LIT>" <EOL> return len ( self . form_list ) <EOL> @ method_decorator ( csrf_protect ) <EOL> def __call__ ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in kwargs : <EOL> self . extra_context . update ( kwargs [ '<STR_LIT>' ] ) <EOL> current_step = self . determine_step ( request , * args , ** kwargs ) <EOL> self . parse_params ( request , * args , ** kwargs ) <EOL> if current_step >= self . num_steps ( ) : <EOL> raise Http404 ( '<STR_LIT>' % current_step ) <EOL> previous_form_list = [ ] <EOL> for i in range ( current_step ) : <EOL> f = self . get_form ( i , request . POST ) <EOL> if request . POST . get ( "<STR_LIT>" % i , '<STR_LIT>' ) != self . security_hash ( request , f ) : <EOL> return self . render_hash_failure ( request , i ) <EOL> if not f . is_valid ( ) : <EOL> return self . render_revalidation_failure ( request , i , f ) <EOL> else : <EOL> self . process_step ( request , f , i ) <EOL> previous_form_list . append ( f ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = self . get_form ( current_step , request . POST ) <EOL> else : <EOL> form = self . get_form ( current_step ) <EOL> if form . is_valid ( ) : <EOL> self . process_step ( request , form , current_step ) <EOL> next_step = current_step + <NUM_LIT:1> <EOL> if next_step == self . num_steps ( ) : <EOL> return self . done ( request , previous_form_list + [ form ] ) <EOL> else : <EOL> form = self . get_form ( next_step ) <EOL> self . step = current_step = next_step <EOL> return self . render ( form , request , current_step ) <EOL> def render ( self , form , request , step , context = None ) : <EOL> "<STR_LIT>" <EOL> old_data = request . POST <EOL> prev_fields = [ ] <EOL> if old_data : <EOL> hidden = forms . HiddenInput ( ) <EOL> for i in range ( step ) : <EOL> old_form = self . get_form ( i , old_data ) <EOL> hash_name = '<STR_LIT>' % i <EOL> prev_fields . extend ( [ bf . as_hidden ( ) for bf in old_form ] ) <EOL> prev_fields . append ( hidden . render ( hash_name , old_data . get ( hash_name , self . security_hash ( request , old_form ) ) ) ) <EOL> return self . render_template ( request , form , '<STR_LIT>' . join ( prev_fields ) , step , context ) <EOL> def prefix_for_step ( self , step ) : <EOL> "<STR_LIT>" <EOL> return str ( step ) <EOL> def render_hash_failure ( self , request , step ) : <EOL> """<STR_LIT>""" <EOL> return self . render ( self . get_form ( step ) , request , step , context = { '<STR_LIT>' : _ ( '<STR_LIT>' ) } ) <EOL> def render_revalidation_failure ( self , request , step , form ) : <EOL> """<STR_LIT>""" <EOL> return self . render ( form , request , step ) <EOL> def security_hash ( self , request , form ) : <EOL> """<STR_LIT>""" <EOL> return security_hash ( request , form ) <EOL> def determine_step ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not request . POST : <EOL> return <NUM_LIT:0> <EOL> try : <EOL> step = int ( request . POST . get ( self . step_field_name , <NUM_LIT:0> ) ) <EOL> except ValueError : <EOL> return <NUM_LIT:0> <EOL> return step <EOL> def parse_params ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def get_template ( self , step ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' <EOL> def render_template ( self , request , form , previous_fields , step , context = None ) : <EOL> """<STR_LIT>""" <EOL> context = context or { } <EOL> context . update ( self . extra_context ) <EOL> return render_to_response ( self . get_template ( step ) , dict ( context , <EOL> step_field = self . step_field_name , <EOL> step0 = step , <EOL> step = step + <NUM_LIT:1> , <EOL> step_count = self . num_steps ( ) , <EOL> form = form , <EOL> previous_fields = previous_fields <EOL> ) , context_instance = RequestContext ( request ) ) <EOL> def process_step ( self , request , form , step ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def done ( self , request , form_list ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" % self . __class__ . __name__ ) </s>
