PageRenderTime 17ms CodeModel.GetById 1ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/views/views.py

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