def create_session(request): if request.GET.get('advanced', 'no') == 'yes': advanced = True else: advanced = False if request.method == "POST": f, header_formset = session_and_headers_form(request.POST) if f.is_valid(): # did the user accidentally use the hostname 'localhost'? real_hostname = socket.gethostbyaddr(request.META['REMOTE_ADDR'])[0] real_url, corrected = replace_localhost_hostname(f.cleaned_data['url'], real_hostname) header_text = '' if header_formset.is_valid(): header_text = '\r\n'.join([x.formatted_header() for x in header_formset.forms if x.formatted_header()]) s = HttpSession(http_method=f.cleaned_data['method'], http_url=real_url, http_headers=header_text, follow_redirects=f.cleaned_data['follow_redirects'], http_body=f.cleaned_data['body'], autocorrected_localhost=corrected) s.save() if s.pk: # save was successful request.session.setdefault('spyglass_session_ids', set()).add(s.pk) request.session.modified = True return redirect(s.get_absolute_url()) else: # something went wrong... print "[ERROR] Unable to save HttpSession!" else: print " [WARN] submitted form was invalid: " + str(f.errors) else: f, header_formset = session_and_headers_form() context = { 'form': f, 'http_header_form': header_formset, 'use_advanced_form': advanced, } return render_to_response('spyglass/create_session.html', context, context_instance=RequestContext(request))
def create_session(request): if request.GET.get('advanced', 'no') == 'yes': advanced = True else: advanced = False if request.method == "POST": f, header_formset = session_and_headers_form(request.POST) if f.is_valid(): header_text = '' if header_formset.is_valid(): header_text = '\r\n'.join([x.formatted_header() for x in header_formset.forms if x.formatted_header()]) s = HttpSession(http_method=f.cleaned_data['method'], http_url=f.cleaned_data['url'], http_headers=header_text, follow_redirects=f.cleaned_data['follow_redirects'], http_body=f.cleaned_data['body']) s.save() if s.pk: # save was successful return redirect(s.get_absolute_url()) else: # something went wrong... print "[ERROR] Unable to save HttpSession!" else: print " [WARN] submitted form was invalid: " + str(f.errors) else: f, header_formset = session_and_headers_form() context = { 'form': f, 'http_header_form': header_formset, 'use_advanced_form': advanced, } return render_to_response('spyglass/create_session.html', context, context_instance=RequestContext(request))