PageRenderTime 2403ms CodeModel.GetById 2384ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/regressiontests/views/views.py

https://code.google.com/p/mango-py/
Python | 141 lines | 114 code | 12 blank | 15 comment | 6 complexity | ae0f82b0791a172e06f04ea104a5a508 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. import sys
  2. from django import forms
  3. from django.http import HttpResponse, HttpResponseRedirect
  4. from django.core.urlresolvers import get_resolver
  5. from django.shortcuts import render_to_response, render
  6. from django.template import Context, RequestContext, TemplateDoesNotExist
  7. from django.views.debug import technical_500_response
  8. from django.views.generic.create_update import create_object
  9. from regressiontests.views import BrokenException, except_args
  10. from models import Article
  11. def index_page(request):
  12. """Dummy index page"""
  13. return HttpResponse('<html><body>Dummy page</body></html>')
  14. def custom_create(request):
  15. """
  16. Calls create_object generic view with a custom form class.
  17. """
  18. class SlugChangingArticleForm(forms.ModelForm):
  19. """Custom form class to overwrite the slug."""
  20. class Meta:
  21. model = Article
  22. def save(self, *args, **kwargs):
  23. self.instance.slug = 'some-other-slug'
  24. return super(SlugChangingArticleForm, self).save(*args, **kwargs)
  25. return create_object(request,
  26. post_save_redirect='/views/create_update/view/article/%(slug)s/',
  27. form_class=SlugChangingArticleForm)
  28. def raises(request):
  29. # Make sure that a callable that raises an exception in the stack frame's
  30. # local vars won't hijack the technical 500 response. See:
  31. # http://code.djangoproject.com/ticket/15025
  32. def callable():
  33. raise Exception
  34. try:
  35. raise Exception
  36. except Exception:
  37. return technical_500_response(request, *sys.exc_info())
  38. def raises404(request):
  39. resolver = get_resolver(None)
  40. resolver.resolve('')
  41. def redirect(request):
  42. """
  43. Forces an HTTP redirect.
  44. """
  45. return HttpResponseRedirect("target/")
  46. def view_exception(request, n):
  47. raise BrokenException(except_args[int(n)])
  48. def template_exception(request, n):
  49. return render_to_response('debug/template_exception.html',
  50. {'arg': except_args[int(n)]})
  51. # Some views to exercise the shortcuts
  52. def render_to_response_view(request):
  53. return render_to_response('debug/render_test.html', {
  54. 'foo': 'FOO',
  55. 'bar': 'BAR',
  56. })
  57. def render_to_response_view_with_request_context(request):
  58. return render_to_response('debug/render_test.html', {
  59. 'foo': 'FOO',
  60. 'bar': 'BAR',
  61. }, context_instance=RequestContext(request))
  62. def render_to_response_view_with_mimetype(request):
  63. return render_to_response('debug/render_test.html', {
  64. 'foo': 'FOO',
  65. 'bar': 'BAR',
  66. }, mimetype='application/x-rendertest')
  67. def render_view(request):
  68. return render(request, 'debug/render_test.html', {
  69. 'foo': 'FOO',
  70. 'bar': 'BAR',
  71. })
  72. def render_view_with_base_context(request):
  73. return render(request, 'debug/render_test.html', {
  74. 'foo': 'FOO',
  75. 'bar': 'BAR',
  76. }, context_instance=Context())
  77. def render_view_with_content_type(request):
  78. return render(request, 'debug/render_test.html', {
  79. 'foo': 'FOO',
  80. 'bar': 'BAR',
  81. }, content_type='application/x-rendertest')
  82. def render_view_with_status(request):
  83. return render(request, 'debug/render_test.html', {
  84. 'foo': 'FOO',
  85. 'bar': 'BAR',
  86. }, status=403)
  87. def render_view_with_current_app(request):
  88. return render(request, 'debug/render_test.html', {
  89. 'foo': 'FOO',
  90. 'bar': 'BAR',
  91. }, current_app="foobar_app")
  92. def render_view_with_current_app_conflict(request):
  93. # This should fail because we don't passing both a current_app and
  94. # context_instance:
  95. return render(request, 'debug/render_test.html', {
  96. 'foo': 'FOO',
  97. 'bar': 'BAR',
  98. }, current_app="foobar_app", context_instance=RequestContext(request))
  99. def raises_template_does_not_exist(request):
  100. # We need to inspect the HTML generated by the fancy 500 debug view but
  101. # the test client ignores it, so we send it explicitly.
  102. try:
  103. return render_to_response('i_dont_exist.html')
  104. except TemplateDoesNotExist:
  105. return technical_500_response(request, *sys.exc_info())
  106. # The default logging config has a logging filter to ensure admin emails are
  107. # only sent with DEBUG=False, but since someone might choose to remove that
  108. # filter, we still want to be able to test the behavior of error emails
  109. # with DEBUG=True. So we need to remove the filter temporarily.
  110. admin_email_handler = [
  111. h for h in logger.handlers
  112. if h.__class__.__name__ == "AdminEmailHandler"
  113. ][0]
  114. orig_filters = admin_email_handler.filters
  115. admin_email_handler.filters = []
  116. admin_email_handler.filters = orig_filters