3 from django
.conf
import settings
4 from django
.contrib
.auth
.decorators
import login_required
5 from django
.http
import HttpResponse
, HttpResponseRedirect
6 from django
.core
.exceptions
import SuspiciousOperation
7 from django
.shortcuts
import render_to_response
8 from django
.utils
import simplejson
9 from django
.utils
.encoding
import smart_str
10 from django
.core
.serializers
.json
import DjangoJSONEncoder
11 from django
.test
.client
import CONTENT_TYPE_RE
12 from django
.template
import RequestContext
14 def no_template_view(request
):
15 "A simple view that expects a GET request, and returns a rendered template"
16 return HttpResponse("No template used. Sample content: twice once twice. Content ends.")
18 def staff_only_view(request
):
19 "A view that can only be visited by staff. Non staff members get an exception"
20 if request
.user
.is_staff
:
21 return HttpResponse('')
23 raise SuspiciousOperation()
25 def get_view(request
):
26 "A simple login protected view"
27 return HttpResponse("Hello world")
28 get_view
= login_required(get_view
)
30 def request_data(request
, template
='base.html', data
='sausage'):
31 "A simple view that returns the request data in the context"
32 return render_to_response(template
, {
33 'get-foo':request
.GET
.get('foo',None),
34 'get-bar':request
.GET
.get('bar',None),
35 'post-foo':request
.POST
.get('foo',None),
36 'post-bar':request
.POST
.get('bar',None),
37 'request-foo':request
.REQUEST
.get('foo',None),
38 'request-bar':request
.REQUEST
.get('bar',None),
42 def view_with_argument(request
, name
):
43 """A view that takes a string argument
45 The purpose of this view is to check that if a space is provided in
46 the argument, the test framework unescapes the %20 before passing
47 the value to the view.
49 if name
== 'Arthur Dent':
50 return HttpResponse('Hi, Arthur')
52 return HttpResponse('Howdy, %s' % name
)
54 def login_protected_redirect_view(request
):
55 "A view that redirects all requests to the GET view"
56 return HttpResponseRedirect('/test_client_regress/get_view/')
57 login_protected_redirect_view
= login_required(login_protected_redirect_view
)
59 def set_session_view(request
):
60 "A view that sets a session variable"
61 request
.session
['session_var'] = 'YES'
62 return HttpResponse('set_session')
64 def check_session_view(request
):
65 "A view that reads a session variable"
66 return HttpResponse(request
.session
.get('session_var', 'NO'))
68 def request_methods_view(request
):
69 "A view that responds with the request method"
70 return HttpResponse('request method: %s' % request
.method
)
72 def return_unicode(request
):
73 return render_to_response('unicode.html')
75 def return_json_file(request
):
76 "A view that parses and returns a JSON string as a file."
77 match
= CONTENT_TYPE_RE
.match(request
.META
['CONTENT_TYPE'])
79 charset
= match
.group(1)
81 charset
= settings
.DEFAULT_CHARSET
83 # This just checks that the uploaded data is JSON
84 obj_dict
= simplejson
.loads(request
.body
.decode(charset
))
85 obj_json
= simplejson
.dumps(obj_dict
, encoding
=charset
,
86 cls
=DjangoJSONEncoder
,
88 response
= HttpResponse(smart_str(obj_json
, encoding
=charset
), status
=200,
89 mimetype
='application/json; charset=' + charset
)
90 response
['Content-Disposition'] = 'attachment; filename=testfile.json'
93 def check_headers(request
):
94 "A view that responds with value of the X-ARG-CHECK header"
95 return HttpResponse('HTTP_X_ARG_CHECK: %s' % request
.META
.get('HTTP_X_ARG_CHECK', 'Undefined'))
98 "A view that is requested with GET and accesses request.body. Refs #14753."
99 return HttpResponse(request
.body
)
101 def read_all(request
):
102 "A view that is requested with accesses request.read()."
103 return HttpResponse(request
.read())
105 def read_buffer(request
):
106 "A view that is requested with accesses request.read(LARGE_BUFFER)."
107 return HttpResponse(request
.read(99999))
109 def request_context_view(request
):
110 # Special attribute that won't be present on a plain HttpRequest
111 request
.special_path
= request
.path
112 return render_to_response('request_context.html', context_instance
=RequestContext(request
, {}))