2006-05-02 09:31:56 +08:00
|
|
|
from django.conf import settings
|
2012-10-01 20:17:55 +08:00
|
|
|
from django.contrib.flatpages.models import FlatPage
|
2014-01-26 04:54:25 +08:00
|
|
|
from django.contrib.sites.shortcuts import get_current_site
|
2012-10-01 20:17:55 +08:00
|
|
|
from django.http import Http404, HttpResponse, HttpResponsePermanentRedirect
|
|
|
|
from django.shortcuts import get_object_or_404
|
2015-01-08 22:03:43 +08:00
|
|
|
from django.template import loader
|
2007-11-14 20:58:53 +08:00
|
|
|
from django.utils.safestring import mark_safe
|
2010-02-05 05:47:19 +08:00
|
|
|
from django.views.decorators.csrf import csrf_protect
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
DEFAULT_TEMPLATE = 'flatpages/default.html'
|
2005-09-24 06:09:42 +08:00
|
|
|
|
2010-02-05 05:47:19 +08:00
|
|
|
# This view is called from FlatpageFallbackMiddleware.process_response
|
|
|
|
# when a 404 is raised, which often means CsrfViewMiddleware.process_view
|
|
|
|
# has not been called even if CsrfViewMiddleware is installed. So we need
|
|
|
|
# to use @csrf_protect, in case the template needs {% csrf_token %}.
|
2010-08-27 21:55:11 +08:00
|
|
|
# However, we can't just wrap this view; if no matching flatpage exists,
|
|
|
|
# or a redirect is required for authentication, the 404 needs to be returned
|
|
|
|
# without any CSRF checks. Therefore, we only
|
|
|
|
# CSRF protect the internal implementation.
|
2013-10-31 23:42:28 +08:00
|
|
|
|
|
|
|
|
2005-11-11 12:45:05 +08:00
|
|
|
def flatpage(request, url):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2010-08-27 21:55:11 +08:00
|
|
|
Public interface to the flat page view.
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-11 12:45:05 +08:00
|
|
|
Models: `flatpages.flatpages`
|
2005-07-13 09:25:57 +08:00
|
|
|
Templates: Uses the template defined by the ``template_name`` field,
|
2012-06-27 09:17:15 +08:00
|
|
|
or :template:`flatpages/default.html` if template_name is not defined.
|
2005-07-13 09:25:57 +08:00
|
|
|
Context:
|
2005-11-11 12:45:05 +08:00
|
|
|
flatpage
|
|
|
|
`flatpages.flatpages` object
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
|
|
|
if not url.startswith('/'):
|
2011-04-20 22:41:47 +08:00
|
|
|
url = '/' + url
|
2012-10-01 20:17:55 +08:00
|
|
|
site_id = get_current_site(request).id
|
2011-04-20 22:41:47 +08:00
|
|
|
try:
|
2016-03-29 06:33:29 +08:00
|
|
|
f = get_object_or_404(FlatPage, url=url, sites=site_id)
|
2011-04-20 22:41:47 +08:00
|
|
|
except Http404:
|
|
|
|
if not url.endswith('/') and settings.APPEND_SLASH:
|
|
|
|
url += '/'
|
2016-03-29 06:33:29 +08:00
|
|
|
f = get_object_or_404(FlatPage, url=url, sites=site_id)
|
2011-04-20 22:41:47 +08:00
|
|
|
return HttpResponsePermanentRedirect('%s/' % request.path)
|
|
|
|
else:
|
|
|
|
raise
|
2010-08-27 21:55:11 +08:00
|
|
|
return render_flatpage(request, f)
|
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2010-08-27 21:55:11 +08:00
|
|
|
@csrf_protect
|
|
|
|
def render_flatpage(request, f):
|
|
|
|
"""
|
|
|
|
Internal interface to the flat page view.
|
|
|
|
"""
|
2005-07-13 09:25:57 +08:00
|
|
|
# If registration is required for accessing this page, and the user isn't
|
|
|
|
# logged in, redirect to the login page.
|
2016-04-02 19:18:26 +08:00
|
|
|
if f.registration_required and not request.user.is_authenticated:
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.contrib.auth.views import redirect_to_login
|
2005-07-13 09:25:57 +08:00
|
|
|
return redirect_to_login(request.path)
|
2005-09-24 06:09:42 +08:00
|
|
|
if f.template_name:
|
2015-01-08 22:03:43 +08:00
|
|
|
template = loader.select_template((f.template_name, DEFAULT_TEMPLATE))
|
2005-09-24 06:09:42 +08:00
|
|
|
else:
|
2015-01-08 22:03:43 +08:00
|
|
|
template = loader.get_template(DEFAULT_TEMPLATE)
|
2007-11-14 20:58:53 +08:00
|
|
|
|
|
|
|
# To avoid having to always use the "|safe" filter in flatpage templates,
|
|
|
|
# mark the title and content as already safe (since they are raw HTML
|
|
|
|
# content in the first place).
|
|
|
|
f.title = mark_safe(f.title)
|
|
|
|
f.content = mark_safe(f.content)
|
|
|
|
|
2015-01-08 22:03:43 +08:00
|
|
|
response = HttpResponse(template.render({'flatpage': f}, request))
|
2006-08-23 10:05:05 +08:00
|
|
|
return response
|