PageRenderTime 108ms CodeModel.GetById 100ms app.highlight 2ms RepoModel.GetById 5ms app.codeStats 0ms

/django/contrib/flatpages/views.py

https://code.google.com/p/mango-py/
Python | 64 lines | 33 code | 5 blank | 26 comment | 9 complexity | 9efb77ec881d300ad774bc1997fa58db MD5 | raw file
 1from django.contrib.flatpages.models import FlatPage
 2from django.template import loader, RequestContext
 3from django.shortcuts import get_object_or_404
 4from django.http import HttpResponse, HttpResponseRedirect
 5from django.conf import settings
 6from django.core.xheaders import populate_xheaders
 7from django.utils.safestring import mark_safe
 8from django.views.decorators.csrf import csrf_protect
 9
10DEFAULT_TEMPLATE = 'flatpages/default.html'
11
12# This view is called from FlatpageFallbackMiddleware.process_response
13# when a 404 is raised, which often means CsrfViewMiddleware.process_view
14# has not been called even if CsrfViewMiddleware is installed. So we need
15# to use @csrf_protect, in case the template needs {% csrf_token %}.
16# However, we can't just wrap this view; if no matching flatpage exists,
17# or a redirect is required for authentication, the 404 needs to be returned
18# without any CSRF checks. Therefore, we only
19# CSRF protect the internal implementation.
20def flatpage(request, url):
21    """
22    Public interface to the flat page view.
23
24    Models: `flatpages.flatpages`
25    Templates: Uses the template defined by the ``template_name`` field,
26        or `flatpages/default.html` if template_name is not defined.
27    Context:
28        flatpage
29            `flatpages.flatpages` object
30    """
31    if not url.endswith('/') and settings.APPEND_SLASH:
32        return HttpResponseRedirect("%s/" % request.path)
33    if not url.startswith('/'):
34        url = "/" + url
35    f = get_object_or_404(FlatPage, url__exact=url, sites__id__exact=settings.SITE_ID)
36    return render_flatpage(request, f)
37
38@csrf_protect
39def render_flatpage(request, f):
40    """
41    Internal interface to the flat page view.
42    """
43    # If registration is required for accessing this page, and the user isn't
44    # logged in, redirect to the login page.
45    if f.registration_required and not request.user.is_authenticated():
46        from django.contrib.auth.views import redirect_to_login
47        return redirect_to_login(request.path)
48    if f.template_name:
49        t = loader.select_template((f.template_name, DEFAULT_TEMPLATE))
50    else:
51        t = loader.get_template(DEFAULT_TEMPLATE)
52
53    # To avoid having to always use the "|safe" filter in flatpage templates,
54    # mark the title and content as already safe (since they are raw HTML
55    # content in the first place).
56    f.title = mark_safe(f.title)
57    f.content = mark_safe(f.content)
58
59    c = RequestContext(request, {
60        'flatpage': f,
61    })
62    response = HttpResponse(t.render(c))
63    populate_xheaders(request, response, FlatPage, f.id)
64    return response