|
1 from django.conf import settings |
|
2 from django.contrib.auth.decorators import login_required |
|
3 from django.http import HttpResponse, HttpResponseRedirect |
|
4 from django.core.exceptions import SuspiciousOperation |
|
5 from django.shortcuts import render_to_response |
|
6 from django.utils import simplejson |
|
7 from django.utils.encoding import smart_str |
|
8 from django.core.serializers.json import DjangoJSONEncoder |
|
9 from django.test.client import CONTENT_TYPE_RE |
|
10 |
|
11 def no_template_view(request): |
|
12 "A simple view that expects a GET request, and returns a rendered template" |
|
13 return HttpResponse("No template used. Sample content: twice once twice. Content ends.") |
|
14 |
|
15 def staff_only_view(request): |
|
16 "A view that can only be visited by staff. Non staff members get an exception" |
|
17 if request.user.is_staff: |
|
18 return HttpResponse('') |
|
19 else: |
|
20 raise SuspiciousOperation() |
|
21 |
|
22 def get_view(request): |
|
23 "A simple login protected view" |
|
24 return HttpResponse("Hello world") |
|
25 get_view = login_required(get_view) |
|
26 |
|
27 def request_data(request, template='base.html', data='sausage'): |
|
28 "A simple view that returns the request data in the context" |
|
29 return render_to_response(template, { |
|
30 'get-foo':request.GET.get('foo',None), |
|
31 'get-bar':request.GET.get('bar',None), |
|
32 'post-foo':request.POST.get('foo',None), |
|
33 'post-bar':request.POST.get('bar',None), |
|
34 'request-foo':request.REQUEST.get('foo',None), |
|
35 'request-bar':request.REQUEST.get('bar',None), |
|
36 'data': data, |
|
37 }) |
|
38 |
|
39 def view_with_argument(request, name): |
|
40 """A view that takes a string argument |
|
41 |
|
42 The purpose of this view is to check that if a space is provided in |
|
43 the argument, the test framework unescapes the %20 before passing |
|
44 the value to the view. |
|
45 """ |
|
46 if name == 'Arthur Dent': |
|
47 return HttpResponse('Hi, Arthur') |
|
48 else: |
|
49 return HttpResponse('Howdy, %s' % name) |
|
50 |
|
51 def login_protected_redirect_view(request): |
|
52 "A view that redirects all requests to the GET view" |
|
53 return HttpResponseRedirect('/test_client_regress/get_view/') |
|
54 login_protected_redirect_view = login_required(login_protected_redirect_view) |
|
55 |
|
56 def set_session_view(request): |
|
57 "A view that sets a session variable" |
|
58 request.session['session_var'] = 'YES' |
|
59 return HttpResponse('set_session') |
|
60 |
|
61 def check_session_view(request): |
|
62 "A view that reads a session variable" |
|
63 return HttpResponse(request.session.get('session_var', 'NO')) |
|
64 |
|
65 def request_methods_view(request): |
|
66 "A view that responds with the request method" |
|
67 return HttpResponse('request method: %s' % request.method) |
|
68 |
|
69 def return_unicode(request): |
|
70 return render_to_response('unicode.html') |
|
71 |
|
72 def return_json_file(request): |
|
73 "A view that parses and returns a JSON string as a file." |
|
74 match = CONTENT_TYPE_RE.match(request.META['CONTENT_TYPE']) |
|
75 if match: |
|
76 charset = match.group(1) |
|
77 else: |
|
78 charset = settings.DEFAULT_CHARSET |
|
79 |
|
80 # This just checks that the uploaded data is JSON |
|
81 obj_dict = simplejson.loads(request.raw_post_data.decode(charset)) |
|
82 obj_json = simplejson.dumps(obj_dict, encoding=charset, |
|
83 cls=DjangoJSONEncoder, |
|
84 ensure_ascii=False) |
|
85 response = HttpResponse(smart_str(obj_json, encoding=charset), status=200, |
|
86 mimetype='application/json; charset=' + charset) |
|
87 response['Content-Disposition'] = 'attachment; filename=testfile.json' |
|
88 return response |
|
89 |
|
90 def check_headers(request): |
|
91 "A view that responds with value of the X-ARG-CHECK header" |
|
92 return HttpResponse('HTTP_X_ARG_CHECK: %s' % request.META.get('HTTP_X_ARG_CHECK', 'Undefined')) |
|
93 |