python - Django SessionWizardView current step missing from form_list after Ajax request -


i've got sessionwizardview process conditional steps @ end of first step asks 'do want add person' condition generated checking cleaned data previous step;

def function_factory(prev_step):     """ creates functions condition dict controlling additional     entrant steps in process.      :param prev_step: step in signup process check     :type prev_step: unicode     :return: additional_entrant()     :rtype:     """     def additional_entrant(wizard):         """         checks cleaned_data previous step see if entrant         needs added         """         # try cleaned data of prev_step         cleaned_data = wizard.get_cleaned_data_for_step(prev_step) or {}          # check if field ``add_another_person`` checked.         return cleaned_data.get(u'add_another_person', false)      return additional_entrant  def make_condition_stuff(extra_steps, last_step_before_repeat):     cond_funcs = {}     cond_dict = {}     form_lst = [         (u"main_entrant", entrantform),     ]      x in range(last_step_before_repeat, extra_steps):         key1 = u"{}_{}".format(additional_step_name, x)         if x == 1:             prev_step = u"main_entrant"         else:             prev_step = u"{}_{}".format(additional_step_name, x-1)         cond_funcs[key1] = function_factory(prev_step)         cond_dict[key1] = cond_funcs[key1]         form_lst.append(             (key1, additionalentrantform)         )      form_lst.append(         (u"terms", termsform)     )      return cond_funcs, cond_dict, form_lst  last_step_before_extras = 1 extra_steps = settings.additional_entrants  cond_funcs, cond_dict, form_list = make_condition_stuff(     extra_steps,     last_step_before_extras ) 

i've got dictionary stores step data behind key accessible via session cookie, holds list of people's details entered user. after first form, list rendered select box & on selection triggers ajax call sessionwizard kwargs triggers call method returns jsonresponse;

class signupwizard(sessionwizardview):     template_name = 'entrant/wizard_form.html'     form_list = form_list     condition_dict = cond_dict     model = entrant     main_entrant = none     data_dict = dict()      def get_data(self, source_step, step):         session_data_dict = self.get_session_data_dict()         try:             data = session_data_dict[source_step].copy()             data['event'] = self.current_event.id             key in data.iterkeys():                 if step not in key:                     newkey = u'{}-{}'.format(step, key)                     data[newkey] = data[key]                     del data[key]         except (keyerror, runtimeerror):             data = dict()             data['error'] = (                 u'there problem retrieving data requested. '                 u'please resubmit form if try again.'             )          response = jsonresponse(data)         return response      def dispatch(self, request, *args, **kwargs):         response = super(signupwizard, self).dispatch(             request, *args, **kwargs         )         if 'get_data' in kwargs:             data_id = kwargs['get_data']             step = kwargs['step']             response = self.get_data(data_id, step)          # update response (e.g. adding cookies)         self.storage.update_response(response)         return response      def process_step(self, form):         form_data = self.get_form_step_data(form)         current_step = self.storage.current_step or ''         session_data_dict = self.get_session_data_dict()          if current_step in session_data_dict:             # replace existing data step.             session_data_dict.pop(current_step)          if not isinstance(form, termsform):             entrant_data = dict()             fields_to_remove = [                 'email', 'confirm_email', 'password',                 'confirm_password', 'csrfmiddlewaretoken'             ]             k, v in form_data.iteritems():                 entrant_data[k] = v             field in fields_to_remove:                 if '{}-{}'.format(current_step, field) in entrant_data:                     entrant_data.pop('{}-{}'.format(current_step, field))                 if '{}'.format(field) in entrant_data:                     entrant_data.pop('{}'.format(field))              k in entrant_data.iterkeys():                 new_key = re.sub('{}-'.format(current_step), u'', k)                 entrant_data[new_key] = entrant_data.pop(k)              session_data_dict[current_step] = entrant_data             done = false             i, data in enumerate(session_data_dict['data_list']):                 if data[0] == current_step:                     session_data_dict['data_list'][i] = (                         current_step, u'{} {}'.format(                             entrant_data['first_name'],                             entrant_data['last_name']                         )                     )                     done = true              if not done:                 session_data_dict['data_list'].append(                     (                         current_step, u'{} {}'.format(                             entrant_data['first_name'],                             entrant_data['last_name']                         )                     )                 )          return form_data 

if step through form without triggering ajax call form submits , condition dict behaves expected. if ajax triggered , data returned form, once submit form session data appears have gone. there way change way i've got setup get_data() can return data page, without destroying session?

i've got session_engine set cached_db on dev server i've got issue whereby when submit first conditional form , system calls get_next_step() followed get_form_list() , condition check no longer returns first conditional form i'm left default form list , valueerror raised because current_step no longer part of form_list.

so, recap, step through first form, trigger first conditional form using 'add_another_person' field renders form expected @ point form_list looks this;

form_list        u'main_entrant' <class 'online_entry.forms.entrantform'>     u'additional_entrant_1' <class 'online_entry.forms.entrantform'>     u'terms' <class 'online_entry.forms.termsform'> 

but additional_entrant_1 triggers ajax method, submitted, form_list runs through condition dict & looks this;

form_list        u'main_entrant' <class 'online_entry.forms.entrantform'>     u'terms' <class 'online_entry.forms.termsform'> 

could issue session storage or session becoming invalid?

i overlook simple explanation.

the get() request of sessionwizardview resets storage, , ajax call making hit view request, resetting storage, passing information.

so simple override of get() method i've resolved this;

def get(self, request, *args, **kwargs):     if 'get_data' in kwargs:         data_id = kwargs['get_data']         step = kwargs['step']         response = self.get_data(data_id, step)     else:         self.storage.reset()          # reset current step first step.         self.storage.current_step = self.steps.first         response = self.render(self.get_form())     return response 

Comments