def prepare_form(self, __): # init storage and step helper self.prefix = normalize_name(self.__class__.__name__) self.storage = get_storage(self.storage_name, self.prefix, self.request, getattr(self, "file_storage", None)) self.steps = StepsHelper(self) self.wizard_goto_step = False if self.request.method == "GET": self.storage.reset() self.storage.current_step = self.steps.first self.admin_view.model_form = self.get_step_form() else: # Look for a wizard_goto_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_goto_step = self.request.POST.get("wizard_goto_step", None) if wizard_goto_step and int(wizard_goto_step) < len(self.get_form_list()): self.storage.current_step = self.get_form_list().keys()[int(wizard_goto_step)] self.admin_view.model_form = self.get_step_form() self.wizard_goto_step = True return # 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 self.admin_view.model_form = self.get_step_form()
def dispatch(self, request, *args, **kwargs): """ This method gets called by the routing engine. The first argument is `request` which contains a `HttpRequest` instance. The request is stored in `self.request` for later use. The storage instance is stored in `self.storage`. After processing the request using the `dispatch` method, the response gets updated by the storage engine (for example add cookies). """ # add the storage engine to the current wizardview instance self.prefix = self.get_prefix(*args, **kwargs) self.storage = get_storage(self.storage_name, self.prefix, request, getattr(self, "file_storage", None)) self.steps = StepsHelper(self) response = super(WizardView, self).dispatch(request, *args, **kwargs) # update the response (e.g. adding cookies) self.storage.update_response(response) return response
def dispatch(self, request, *args, **kwargs): """ This method gets called by the routing engine. The first argument is `request` which contains a `HttpRequest` instance. The request is stored in `self.request` for later use. The storage instance is stored in `self.storage`. After processing the request using the `dispatch` method, the response gets updated by the storage engine (for example add cookies). """ # add the storage engine to the current wizardview instance self.prefix = self.get_prefix(*args, **kwargs) self.storage = get_storage(self.storage_name, self.prefix, request, getattr(self, 'file_storage', None)) self.steps = StepsHelper(self) response = super(WizardView, self).dispatch(request, *args, **kwargs) # update the response (e.g. adding cookies) self.storage.update_response(response) return response
def prepare_form(self, __): # init storage and step helper self.prefix = normalize_name(self.__class__.__name__) self.storage = get_storage( self.storage_name, self.prefix, self.request, getattr(self, 'file_storage', None)) self.steps = StepsHelper(self) self.wizard_goto_step = False if self.request.method == 'GET': self.storage.reset() self.storage.current_step = self.steps.first self.admin_view.model_form = self.get_step_form() else: # Look for a wizard_goto_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_goto_step = self.request.POST.get('wizard_goto_step', None) if wizard_goto_step and int(wizard_goto_step) < len(self.get_form_list()): self.storage.current_step = self.get_form_list( ).keys()[int(wizard_goto_step)] self.admin_view.model_form = self.get_step_form() self.wizard_goto_step = True return # 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 self.admin_view.model_form = self.get_step_form()
def dispatch(self, request, *args, **kwargs): """ The method that accepts a request argument plus arguments, and returns an HTTP response. - Verify that the user owns the selected scenario - if not, go to selection page with error message """ # add the storage engine to the current wizard view instance here so we can act on storage here self.prefix = self.get_prefix(*args, **kwargs) self.storage = get_storage(self.storage_name, self.prefix, request, getattr(self, 'file_storage', None)) # Maybe not need to set a flag to make this block work - if first time, we NEED an id # - if revisit, should do nothing here but continue (example: user reloads page) #if not kwargs['scenario_id']: # set_notification('alert-error', # 'Please select a simulation to work in.', # request.session) # return redirect("ts_emod_scenario_browse") if 'scenario_id' in kwargs.keys() and kwargs['scenario_id'] > 0: #todo: check for intervention_tool in self.storage.extra_data.keys() ?? # First hit in Edit mode - populate the steps with initial defaults try: self.storage.reset() self.storage.extra_data['intervention_tool'] = kwargs['scenario_id'] except AttributeError: pass # get the scenario for editing self.request.session['scenario'] = EMODBaseline.from_dw(pk=kwargs['scenario_id']) if str(self.request.session['scenario'].user) == \ str(DimUser.objects.get_or_create(username=self.request.user.username)[0]): return super(InterventionToolView, self).dispatch(request, *args, **kwargs) else: set_notification('alert-error', 'You cannot modify simulations unless you own them. ' 'Please select a simulation to work in.', request.session) return redirect("ts_emod_scenario_browse")
def dispatch(self, request, *args, **kwargs): """ The method that accepts a request argument plus arguments, and returns an HTTP response. Make sure user is logged in """ # add the storage engine to the current wizardview instance here so we can act on storage here self.prefix = self.get_prefix(*args, **kwargs) self.storage = get_storage(self.storage_name, self.prefix, request, getattr(self, 'file_storage', None)) if 'scenario_id' in kwargs: if kwargs['scenario_id'] == '0': # first visit to "Add a New Baseline" # wipe out any previous session-scenario and wizard storage (including edit flag). try: self.storage.reset() except AttributeError: pass if 'scenario' in self.request.session.keys(): del self.request.session['scenario'] if 'scenario_config' in self.request.session.keys(): del self.request.session['scenario_config'] # create empty scenario self.request.session['scenario'] = EMODBaseline( name='', description='', model=DimModel.objects.get(model='EMOD'), user=DimUser.objects.get_or_create(username=self.request.user.username)[0] ) self.request.session['scenario'].model_version = 'emod' # set dummy, will be overwritten elif kwargs['scenario_id'] > 0: # First hit in Edit mode - populate the steps with initial defaults self.storage.reset() self.storage.extra_data['edit_scenario'] = kwargs['scenario_id'] # get the scenario for editing self.request.session['scenario'] = EMODBaseline.from_dw(pk=kwargs['scenario_id']) else: # Make sure there's a scenario in the session if 'scenario' not in self.request.session.keys(): # create empty scenario self.request.session['scenario'] = EMODBaseline( name='', description='', model=DimModel.objects.get(model='EMOD'), user=DimUser.objects.get_or_create(username=self.request.user.username)[0] ) if 'step' in kwargs.keys() and kwargs['step'] == 'climate': # 6230: redirect to Intervention Tool (if user clicked "Skip to Intervention Tool Button") # check to see if the user wants to jump over to the Intervention tool if 'jump_to_intervention_tool' in self.request.session.keys(): my_scenario_id = self.request.session['scenario'].id # clear wizard storage self.storage.reset() del self.request.session['jump_to_intervention_tool'] # redirect to Intervention Tool return redirect("ts_intervention_tool_step", scenario_id=my_scenario_id) self.request.session['scenario'].save() return super(BaselineWizardView, self).dispatch(request, *args, **kwargs)
def test_load_storage(self): self.assertEqual( type(get_storage('django.contrib.formtools.wizard.storage.base.BaseStorage', 'wizard1')), BaseStorage)
def dispatch(self, request, *args, **kwargs): """ The method that accepts a request argument plus arguments, and returns an HTTP response. Make sure user is logged in """ # add the storage engine to the current wizardview instance here so we can act on storage here self.prefix = self.get_prefix(*args, **kwargs) self.storage = get_storage(self.storage_name, self.prefix, request, getattr(self, 'file_storage', None)) if 'scenario_id' in kwargs: if kwargs['scenario_id'] > 0: # First hit in Edit mode - populate the steps with initial defaults self.storage.reset() self.storage.extra_data['edit_scenario'] = kwargs['scenario_id'] # get the scenario for editing self.request.session['scenario'] = EMODBaseline.from_dw(pk=kwargs['scenario_id']) try: self.request.session['scenario_config'] = \ ast.literal_eval(self.request.session['scenario'].get_file_by_type('config').content) except AttributeError: # if query set returned, use first file in "list" file_list = self.request.session['scenario'].get_file_by_type('config') self.request.session['scenario_config'] = \ ast.literal_eval(file_list[0].content) ###### # config step data: self.storage.set_step_data('config', {u'config-name': [self.request.session['scenario']._name], u'config-description': [self.request.session['scenario'].description], u'config-Start_Time': [self.request.session['scenario_config']['parameters']['Start_Time']], u'config-Simulation_Duration': [unicode(self.request.session['scenario_config']['parameters']['Simulation_Duration'])], u'config-Simulation_Type': [self.request.session['scenario_config']['parameters']['Simulation_Type']], u'baseline_wizard_view-current_step': [u'config'] }) template_id = self.request.session['scenario'].template.id my_template_obj = DimTemplate.objects.get(id=template_id) location_id = my_template_obj.location_key_id # climate step data: self.storage.set_step_data('climate', {u'climate-location': [location_id]}) # demographic step data: self.storage.set_step_data('demographic', {u'demographic-location': [location_id]}) # parasite for step_name in ["vector", "parasite"]: # get db config for the step my_json = ConfigData.objects.filter(type='JSONConfig', name=step_name)[0].json # wizard values based on config + template values my_wiz = {u'orig_json_obj': [my_json]} my_json = json.loads(my_json) for key in my_json.keys(): try: # = scenario value OR template value # change into wizard format step name + -json_ + key name my_wiz.update({u'' + step_name + '-json_' + key: [u'' + str(self.request.session['scenario_config']['parameters'][key])]}) except KeyError: pass # insert into wizard storage self.storage.set_step_data(step_name, my_wiz) # Scaling step data: self.storage.set_step_data('scaling_factors', {u'scaling_factors-x_Temporary_Larval_Habitat': [self.request.session['scenario_config']['parameters']['x_Temporary_Larval_Habitat']]}) ###### if 'step' in kwargs.keys() and kwargs['step'] == 'climate': # 6230: redirect to Intervention Tool (if user clicked "Skip to Intervention Tool Button") # check to see if the user wants to jump over to the Intervention tool if 'jump_to_intervention_tool' in self.request.session.keys(): my_scenario_id = self.request.session['scenario'].id # clear wizard storage self.storage.reset() del self.request.session['jump_to_intervention_tool'] # redirect to Intervention Tool return redirect("ts_intervention_tool_step", scenario_id=my_scenario_id) self.request.session['scenario'].save() return super(EditWizardView, self).dispatch(request, *args, **kwargs)
def test_load_storage(self): self.assertEqual( type(get_storage('django.contrib.formtools.wizard.storage.base.BaseStorage', 'wizard1')), BaseStorage)