2009-04-11 09:20:10 +00:00
|
|
|
from django.conf import settings
|
2007-09-03 11:21:40 +00:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2008-07-01 15:10:51 +00:00
|
|
|
from django.http import HttpResponse, HttpResponseRedirect
|
2008-06-07 06:25:59 +00:00
|
|
|
from django.core.exceptions import SuspiciousOperation
|
2008-11-12 11:22:05 +00:00
|
|
|
from django.shortcuts import render_to_response
|
2009-04-11 09:20:10 +00:00
|
|
|
from django.utils import simplejson
|
|
|
|
from django.utils.encoding import smart_str
|
|
|
|
from django.core.serializers.json import DjangoJSONEncoder
|
|
|
|
from django.test.client import CONTENT_TYPE_RE
|
2011-02-27 00:24:35 +00:00
|
|
|
from django.template import RequestContext
|
2007-05-10 13:46:15 +00:00
|
|
|
|
|
|
|
def no_template_view(request):
|
|
|
|
"A simple view that expects a GET request, and returns a rendered template"
|
2007-07-20 14:32:20 +00:00
|
|
|
return HttpResponse("No template used. Sample content: twice once twice. Content ends.")
|
2007-05-10 13:46:15 +00:00
|
|
|
|
2008-06-07 06:25:59 +00:00
|
|
|
def staff_only_view(request):
|
|
|
|
"A view that can only be visited by staff. Non staff members get an exception"
|
|
|
|
if request.user.is_staff:
|
|
|
|
return HttpResponse('')
|
|
|
|
else:
|
|
|
|
raise SuspiciousOperation()
|
2008-07-01 15:10:51 +00:00
|
|
|
|
2007-09-03 11:21:40 +00:00
|
|
|
def get_view(request):
|
2008-03-20 06:50:54 +00:00
|
|
|
"A simple login protected view"
|
2007-09-03 11:21:40 +00:00
|
|
|
return HttpResponse("Hello world")
|
|
|
|
get_view = login_required(get_view)
|
|
|
|
|
2009-03-18 10:46:55 +00:00
|
|
|
def request_data(request, template='base.html', data='sausage'):
|
2008-11-12 11:22:05 +00:00
|
|
|
"A simple view that returns the request data in the context"
|
2009-03-18 10:46:55 +00:00
|
|
|
return render_to_response(template, {
|
2008-11-12 11:22:05 +00:00
|
|
|
'get-foo':request.GET.get('foo',None),
|
|
|
|
'get-bar':request.GET.get('bar',None),
|
|
|
|
'post-foo':request.POST.get('foo',None),
|
|
|
|
'post-bar':request.POST.get('bar',None),
|
|
|
|
'request-foo':request.REQUEST.get('foo',None),
|
|
|
|
'request-bar':request.REQUEST.get('bar',None),
|
2009-03-18 10:46:55 +00:00
|
|
|
'data': data,
|
2008-11-12 11:22:05 +00:00
|
|
|
})
|
|
|
|
|
2008-03-20 06:50:54 +00:00
|
|
|
def view_with_argument(request, name):
|
|
|
|
"""A view that takes a string argument
|
|
|
|
|
|
|
|
The purpose of this view is to check that if a space is provided in
|
|
|
|
the argument, the test framework unescapes the %20 before passing
|
|
|
|
the value to the view.
|
|
|
|
"""
|
|
|
|
if name == 'Arthur Dent':
|
|
|
|
return HttpResponse('Hi, Arthur')
|
|
|
|
else:
|
|
|
|
return HttpResponse('Howdy, %s' % name)
|
|
|
|
|
2007-09-03 11:21:40 +00:00
|
|
|
def login_protected_redirect_view(request):
|
|
|
|
"A view that redirects all requests to the GET view"
|
|
|
|
return HttpResponseRedirect('/test_client_regress/get_view/')
|
2008-07-01 15:10:51 +00:00
|
|
|
login_protected_redirect_view = login_required(login_protected_redirect_view)
|
2008-08-15 12:08:29 +00:00
|
|
|
|
|
|
|
def set_session_view(request):
|
|
|
|
"A view that sets a session variable"
|
|
|
|
request.session['session_var'] = 'YES'
|
|
|
|
return HttpResponse('set_session')
|
|
|
|
|
|
|
|
def check_session_view(request):
|
|
|
|
"A view that reads a session variable"
|
|
|
|
return HttpResponse(request.session.get('session_var', 'NO'))
|
2008-10-07 09:23:40 +00:00
|
|
|
|
|
|
|
def request_methods_view(request):
|
|
|
|
"A view that responds with the request method"
|
2009-02-27 13:14:59 +00:00
|
|
|
return HttpResponse('request method: %s' % request.method)
|
2009-04-07 12:06:05 +00:00
|
|
|
|
|
|
|
def return_unicode(request):
|
|
|
|
return render_to_response('unicode.html')
|
2009-04-11 09:20:10 +00:00
|
|
|
|
|
|
|
def return_json_file(request):
|
|
|
|
"A view that parses and returns a JSON string as a file."
|
|
|
|
match = CONTENT_TYPE_RE.match(request.META['CONTENT_TYPE'])
|
|
|
|
if match:
|
|
|
|
charset = match.group(1)
|
|
|
|
else:
|
|
|
|
charset = settings.DEFAULT_CHARSET
|
|
|
|
|
|
|
|
# This just checks that the uploaded data is JSON
|
|
|
|
obj_dict = simplejson.loads(request.raw_post_data.decode(charset))
|
|
|
|
obj_json = simplejson.dumps(obj_dict, encoding=charset,
|
|
|
|
cls=DjangoJSONEncoder,
|
|
|
|
ensure_ascii=False)
|
|
|
|
response = HttpResponse(smart_str(obj_json, encoding=charset), status=200,
|
|
|
|
mimetype='application/json; charset=' + charset)
|
|
|
|
response['Content-Disposition'] = 'attachment; filename=testfile.json'
|
|
|
|
return response
|
2010-08-21 03:01:46 +00:00
|
|
|
|
|
|
|
def check_headers(request):
|
|
|
|
"A view that responds with value of the X-ARG-CHECK header"
|
|
|
|
return HttpResponse('HTTP_X_ARG_CHECK: %s' % request.META.get('HTTP_X_ARG_CHECK', 'Undefined'))
|
|
|
|
|
2011-01-21 01:04:05 +00:00
|
|
|
def raw_post_data(request):
|
|
|
|
"A view that is requested with GET and accesses request.raw_post_data. Refs #14753."
|
|
|
|
return HttpResponse(request.raw_post_data)
|
2011-02-27 00:24:35 +00:00
|
|
|
|
2011-06-28 10:17:56 +00:00
|
|
|
def read_all(request):
|
|
|
|
"A view that is requested with accesses request.read()."
|
|
|
|
return HttpResponse(request.read())
|
|
|
|
|
|
|
|
def read_buffer(request):
|
|
|
|
"A view that is requested with accesses request.read(LARGE_BUFFER)."
|
|
|
|
return HttpResponse(request.read(99999))
|
|
|
|
|
2011-02-27 00:24:35 +00:00
|
|
|
def request_context_view(request):
|
|
|
|
# Special attribute that won't be present on a plain HttpRequest
|
|
|
|
request.special_path = request.path
|
|
|
|
return render_to_response('request_context.html', context_instance=RequestContext(request, {}))
|