PageRenderTime 37ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/django/contrib/flatpages/views.py

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