2017-01-07 19:11:46 +08:00
|
|
|
from urllib.parse import urlencode
|
2011-12-17 07:40:32 +08:00
|
|
|
|
2009-04-11 17:20:10 +08:00
|
|
|
from django.conf import settings
|
2007-09-03 19:21:40 +08:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2015-05-09 13:33:26 +08:00
|
|
|
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
|
2015-12-23 02:50:16 +08:00
|
|
|
from django.shortcuts import render
|
2014-04-15 03:13:49 +08:00
|
|
|
from django.template.loader import render_to_string
|
2013-02-04 23:50:15 +08:00
|
|
|
from django.test import Client
|
|
|
|
from django.test.client import CONTENT_TYPE_RE
|
2007-05-10 21:46:15 +08:00
|
|
|
|
2013-05-16 07:14:28 +08:00
|
|
|
|
|
|
|
class CustomTestException(Exception):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2007-05-10 21:46:15 +08:00
|
|
|
def no_template_view(request):
|
|
|
|
"A simple view that expects a GET request, and returns a rendered template"
|
2007-07-20 22:32:20 +08:00
|
|
|
return HttpResponse(
|
|
|
|
"No template used. Sample content: twice once twice. Content ends."
|
|
|
|
)
|
2007-05-10 21:46:15 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2008-06-07 14:25:59 +08: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:
|
2019-02-10 05:27:32 +08:00
|
|
|
return HttpResponse()
|
2008-06-07 14:25:59 +08:00
|
|
|
else:
|
2013-05-16 07:14:28 +08:00
|
|
|
raise CustomTestException()
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2016-11-15 01:20:44 +08:00
|
|
|
@login_required
|
2007-09-03 19:21:40 +08:00
|
|
|
def get_view(request):
|
2008-03-20 14:50:54 +08:00
|
|
|
"A simple login protected view"
|
2007-09-03 19:21:40 +08:00
|
|
|
return HttpResponse("Hello world")
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2009-03-18 18:46:55 +08:00
|
|
|
def request_data(request, template="base.html", data="sausage"):
|
2008-11-12 19:22:05 +08:00
|
|
|
"A simple view that returns the request data in the context"
|
2015-12-23 02:50:16 +08:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
template,
|
|
|
|
{
|
2013-10-16 04:36:49 +08:00
|
|
|
"get-foo": request.GET.get("foo"),
|
|
|
|
"get-bar": request.GET.get("bar"),
|
|
|
|
"post-foo": request.POST.get("foo"),
|
|
|
|
"post-bar": request.POST.get("bar"),
|
2009-03-18 18:46:55 +08:00
|
|
|
"data": data,
|
2008-11-12 19:22:05 +08:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2008-03-20 14:50:54 +08: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)
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2013-02-04 23:50:15 +08:00
|
|
|
def nested_view(request):
|
|
|
|
"""
|
|
|
|
A view that uses test client to call another view.
|
|
|
|
"""
|
|
|
|
c = Client()
|
2015-04-28 13:23:42 +08:00
|
|
|
c.get("/no_template_view/")
|
2015-12-23 02:50:16 +08:00
|
|
|
return render(request, "base.html", {"nested": "yes"})
|
2013-02-04 23:50:15 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2016-11-15 01:20:44 +08:00
|
|
|
@login_required
|
2007-09-03 19:21:40 +08:00
|
|
|
def login_protected_redirect_view(request):
|
|
|
|
"A view that redirects all requests to the GET view"
|
2014-01-14 23:43:27 +08:00
|
|
|
return HttpResponseRedirect("/get_view/")
|
2008-08-15 20:08:29 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2014-10-17 21:46:42 +08:00
|
|
|
def redirect_to_self_with_changing_query_view(request):
|
|
|
|
query = request.GET.copy()
|
|
|
|
query["counter"] += "0"
|
|
|
|
return HttpResponseRedirect(
|
|
|
|
"/redirect_to_self_with_changing_query_view/?%s" % urlencode(query)
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2014-10-17 21:46:42 +08:00
|
|
|
|
|
|
|
|
2008-08-15 20:08:29 +08:00
|
|
|
def set_session_view(request):
|
|
|
|
"A view that sets a session variable"
|
|
|
|
request.session["session_var"] = "YES"
|
|
|
|
return HttpResponse("set_session")
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2008-08-15 20:08:29 +08:00
|
|
|
def check_session_view(request):
|
|
|
|
"A view that reads a session variable"
|
|
|
|
return HttpResponse(request.session.get("session_var", "NO"))
|
2008-10-07 17:23:40 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2008-10-07 17:23:40 +08:00
|
|
|
def request_methods_view(request):
|
|
|
|
"A view that responds with the request method"
|
2009-02-27 21:14:59 +08:00
|
|
|
return HttpResponse("request method: %s" % request.method)
|
2009-04-07 20:06:05 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2009-04-07 20:06:05 +08:00
|
|
|
def return_unicode(request):
|
2015-12-23 02:50:16 +08:00
|
|
|
return render(request, "unicode.html")
|
2009-04-11 17:20:10 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2013-04-11 16:36:12 +08:00
|
|
|
def return_undecodable_binary(request):
|
|
|
|
return HttpResponse(
|
|
|
|
b"%PDF-1.4\r\n%\x93\x8c\x8b\x9e ReportLab Generated PDF document "
|
|
|
|
b"http://www.reportlab.com"
|
|
|
|
)
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2015-05-09 13:33:26 +08:00
|
|
|
def return_json_response(request):
|
2016-12-29 21:32:15 +08:00
|
|
|
content_type = request.GET.get("content_type")
|
|
|
|
kwargs = {"content_type": content_type} if content_type else {}
|
|
|
|
return JsonResponse({"key": "value"}, **kwargs)
|
2015-05-09 13:33:26 +08:00
|
|
|
|
|
|
|
|
2019-06-07 12:40:15 +08:00
|
|
|
def return_json_response_latin1(request):
|
|
|
|
return HttpResponse(
|
|
|
|
b'{"a":"\xc5"}', content_type="application/json; charset=latin1"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-03-09 03:07:09 +08:00
|
|
|
def return_text_file(request):
|
|
|
|
"A view that parses and returns text as a file."
|
2009-04-11 17:20:10 +08:00
|
|
|
match = CONTENT_TYPE_RE.match(request.META["CONTENT_TYPE"])
|
|
|
|
if match:
|
2020-05-11 04:03:39 +08:00
|
|
|
charset = match[1]
|
2009-04-11 17:20:10 +08:00
|
|
|
else:
|
|
|
|
charset = settings.DEFAULT_CHARSET
|
|
|
|
|
2019-04-24 19:09:29 +08:00
|
|
|
return HttpResponse(
|
|
|
|
request.body, status=200, content_type="text/plain; charset=%s" % charset
|
|
|
|
)
|
2010-08-21 11:01:46 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2010-08-21 11:01:46 +08: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")
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2010-08-21 11:01:46 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2011-12-17 07:40:32 +08:00
|
|
|
def body(request):
|
|
|
|
"A view that is requested with GET and accesses request.body. Refs #14753."
|
|
|
|
return HttpResponse(request.body)
|
2011-02-27 08:24:35 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2011-06-28 18:17:56 +08:00
|
|
|
def read_all(request):
|
|
|
|
"A view that is requested with accesses request.read()."
|
|
|
|
return HttpResponse(request.read())
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2011-06-28 18:17:56 +08:00
|
|
|
def read_buffer(request):
|
|
|
|
"A view that is requested with accesses request.read(LARGE_BUFFER)."
|
|
|
|
return HttpResponse(request.read(99999))
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2011-02-27 08:24:35 +08:00
|
|
|
def request_context_view(request):
|
|
|
|
# Special attribute that won't be present on a plain HttpRequest
|
|
|
|
request.special_path = request.path
|
2015-09-04 10:01:30 +08:00
|
|
|
return render(request, "request_context.html")
|
2014-04-15 03:13:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
def render_template_multiple_times(request):
|
|
|
|
"""A view that renders a template multiple times."""
|
|
|
|
return HttpResponse(render_to_string("base.html") + render_to_string("base.html"))
|
2017-08-18 08:10:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
def redirect_based_on_extra_headers_1_view(request):
|
|
|
|
if "HTTP_REDIRECT" in request.META:
|
|
|
|
return HttpResponseRedirect("/redirect_based_on_extra_headers_2/")
|
|
|
|
return HttpResponse()
|
|
|
|
|
|
|
|
|
|
|
|
def redirect_based_on_extra_headers_2_view(request):
|
|
|
|
if "HTTP_REDIRECT" in request.META:
|
|
|
|
return HttpResponseRedirect("/redirects/further/more/")
|
|
|
|
return HttpResponse()
|