PageRenderTime 15ms CodeModel.GetById 2ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/test_client_regress/views.py

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