2005-07-13 09:25:57 +08:00
|
|
|
from django.core import template_loader
|
2005-09-24 05:59:17 +08:00
|
|
|
from django.core.extensions import get_object_or_404, DjangoContext
|
2005-07-13 09:25:57 +08:00
|
|
|
from django.models.core import flatfiles
|
|
|
|
from django.utils.httpwrappers import HttpResponse
|
|
|
|
from django.conf.settings import SITE_ID
|
|
|
|
|
2005-09-24 06:09:42 +08:00
|
|
|
DEFAULT_TEMPLATE = 'flatfiles/default'
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
def flat_file(request, url):
|
|
|
|
"""
|
|
|
|
Flat file view
|
|
|
|
|
|
|
|
Models: `core.flatfiles`
|
|
|
|
Templates: Uses the template defined by the ``template_name`` field,
|
|
|
|
or `flatfiles/default` if template_name is not defined.
|
|
|
|
Context:
|
|
|
|
flatfile
|
|
|
|
`flatfiles.flatfiles` object
|
|
|
|
"""
|
|
|
|
if not url.startswith('/'):
|
|
|
|
url = "/" + url
|
2005-09-24 05:59:17 +08:00
|
|
|
f = get_object_or_404(flatfiles, url__exact=url, sites__id__exact=SITE_ID)
|
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.
|
|
|
|
if request.user.is_anonymous() and f.registration_required:
|
|
|
|
from django.views.auth.login import redirect_to_login
|
|
|
|
return redirect_to_login(request.path)
|
2005-09-24 06:09:42 +08:00
|
|
|
if f.template_name:
|
|
|
|
t = template_loader.select_template((f.template_name, DEFAULT_TEMPLATE))
|
|
|
|
else:
|
|
|
|
t = template_loader.get_template(DEFAULT_TEMPLATE)
|
2005-09-24 05:59:17 +08:00
|
|
|
c = DjangoContext(request, {
|
2005-07-13 09:25:57 +08:00
|
|
|
'flatfile': f,
|
|
|
|
})
|
|
|
|
return HttpResponse(t.render(c))
|