def test_redirect_to_view_name(self): res = redirect('hardcoded2') self.assertEqual(res['Location'], '/hardcoded/doc.pdf') res = redirect('places', 1) self.assertEqual(res['Location'], '/places/1/') res = redirect('headlines', year='2008', month='02', day='17') self.assertEqual(res['Location'], '/headlines/2008.02.17/') self.assertRaises(NoReverseMatch, redirect, 'not-a-view')
def test_redirect_to_object(self): # We don't really need a model; just something with a get_absolute_url class FakeObj(object): def get_absolute_url(self): return "/hi-there/" res = redirect(FakeObj()) self.assertTrue(isinstance(res, HttpResponseRedirect)) self.assertEqual(res['Location'], '/hi-there/') res = redirect(FakeObj(), permanent=True) self.assertTrue(isinstance(res, HttpResponsePermanentRedirect)) self.assertEqual(res['Location'], '/hi-there/')
def render_revalidation_failure(self, failed_step, form, **kwargs): """ When a step fails, we have to redirect the user to the first failing step. """ self.storage.current_step = failed_step return redirect(self.get_step_url(failed_step))
def render_done(self, form, **kwargs): """ When rendering the done view, we have to redirect first (if the URL name doesn't fit). """ if kwargs.get('step', None) != self.done_step_name: return redirect(self.get_step_url(self.done_step_name)) return super(NamedUrlWizardView, self).render_done(form, **kwargs)
def render_next_step(self, form, **kwargs): """ When using the NamedUrlWizardView, we have to redirect to update the browser's URL to match the shown step. """ next_step = self.get_next_step() self.storage.current_step = next_step return redirect(self.get_step_url(next_step))
def get(self, *args, **kwargs): """ This renders the form or, if needed, does the http redirects. """ step_url = kwargs.get('step', None) if step_url is None: if 'reset' in self.request.GET: self.storage.reset() self.storage.current_step = self.steps.first if self.request.GET: query_string = "?%s" % self.request.GET.urlencode() else: query_string = "" return redirect(self.get_step_url(self.steps.current) + query_string) # is the current step the "done" name/view? elif step_url == self.done_step_name: last_step = self.steps.last return self.render_done(self.get_form(step=last_step, data=self.storage.get_step_data(last_step), files=self.storage.get_step_files(last_step) ), **kwargs) # is the url step name not equal to the step in the storage? # if yes, change the step in the storage (if name exists) elif step_url == self.steps.current: # URL step name and storage step name are equal, render! return self.render(self.get_form( data=self.storage.current_step_data, files=self.storage.current_step_files, ), **kwargs) elif step_url in self.get_form_list(): self.storage.current_step = step_url return self.render(self.get_form( data=self.storage.current_step_data, files=self.storage.current_step_files, ), **kwargs) # invalid step name, reset to first and redirect. else: self.storage.current_step = self.steps.first return redirect(self.get_step_url(self.steps.first))
def post(self, *args, **kwargs): """ Do a redirect if user presses the prev. step button. The rest of this is super'd from WizardView. """ wizard_goto_step = self.request.POST.get('wizard_goto_step', None) if wizard_goto_step and wizard_goto_step in self.get_form_list(): self.storage.current_step = wizard_goto_step return redirect(self.get_step_url(wizard_goto_step)) return super(NamedUrlWizardView, self).post(*args, **kwargs)
def test_redirect_view_object(self): from .views import absolute_kwargs_view res = redirect(absolute_kwargs_view) self.assertEqual(res['Location'], '/absolute_arg_view/') self.assertRaises(NoReverseMatch, redirect, absolute_kwargs_view, wrong_argument=None)
def test_redirect_to_url(self): res = redirect('/foo/') self.assertEqual(res['Location'], '/foo/') res = redirect('http://example.com/') self.assertEqual(res['Location'], 'http://example.com/')