static.views: Deduplicate code.
The primary goal here is to get templates rendered more like our "regular" templates, by using more of the standard tools to do so rather than roll our own. * Use TemplateResponse. This requires less boilerplate and ensures the rendering runs through our local context processors. * Use the existing definition of fundgoal_lookup.
This commit is contained in:
parent
6e40776adb
commit
a00195c8f6
1 changed files with 14 additions and 27 deletions
|
@ -1,8 +1,10 @@
|
||||||
import mimetypes
|
import mimetypes
|
||||||
import os.path
|
import os.path
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from django.template import RequestContext, loader
|
from django.template.response import TemplateResponse
|
||||||
from conservancy.apps.fundgoal.models import FundraisingGoal as FundraisingGoal
|
|
||||||
|
from conservancy.apps.fundgoal.models import FundraisingGoal
|
||||||
|
from conservancy.local_context_processors import fundgoal_lookup
|
||||||
|
|
||||||
STATIC_ROOT = os.path.abspath(os.path.dirname(__file__))
|
STATIC_ROOT = os.path.abspath(os.path.dirname(__file__))
|
||||||
FILESYSTEM_ENCODING = 'utf-8'
|
FILESYSTEM_ENCODING = 'utf-8'
|
||||||
|
@ -11,10 +13,9 @@ def handler(request, errorcode):
|
||||||
path = os.path.join('error', str(errorcode), 'index.html')
|
path = os.path.join('error', str(errorcode), 'index.html')
|
||||||
fullpath = os.path.join(STATIC_ROOT, path)
|
fullpath = os.path.join(STATIC_ROOT, path)
|
||||||
if not os.path.exists(fullpath):
|
if not os.path.exists(fullpath):
|
||||||
return HttpResponse("Internal error: " + path)
|
return HttpResponse("Internal error: " + path, status=int(errorcode))
|
||||||
template = loader.get_template(path)
|
else:
|
||||||
context = RequestContext(request)
|
return TemplateResponse(request, path, status=int(errorcode))
|
||||||
return HttpResponse(template.render(context), status=int(errorcode))
|
|
||||||
|
|
||||||
def handler401(request):
|
def handler401(request):
|
||||||
return handler(request, 401)
|
return handler(request, 401)
|
||||||
|
@ -28,13 +29,6 @@ def handler404(request):
|
||||||
def handler500(request):
|
def handler500(request):
|
||||||
return handler(request, 500)
|
return handler(request, 500)
|
||||||
|
|
||||||
def fundgoal_lookup(fundraiser_sought):
|
|
||||||
try:
|
|
||||||
return FundraisingGoal.objects.get(fundraiser_code_name=fundraiser_sought)
|
|
||||||
except FundraisingGoal.DoesNotExist:
|
|
||||||
# we have no object! do something
|
|
||||||
return None
|
|
||||||
|
|
||||||
def index(request, *args, **kwargs):
|
def index(request, *args, **kwargs):
|
||||||
path = request.path.lstrip(u'/')
|
path = request.path.lstrip(u'/')
|
||||||
if path.endswith(u'/'):
|
if path.endswith(u'/'):
|
||||||
|
@ -49,23 +43,16 @@ def index(request, *args, **kwargs):
|
||||||
return handler404(request)
|
return handler404(request)
|
||||||
content_type, _ = mimetypes.guess_type(path)
|
content_type, _ = mimetypes.guess_type(path)
|
||||||
if content_type != 'text/html':
|
if content_type != 'text/html':
|
||||||
content = open(fullpath)
|
return HttpResponse(open(fullpath, 'rb'), content_type)
|
||||||
else:
|
else:
|
||||||
content_type = None # Let Django use its default
|
context = kwargs.copy()
|
||||||
template = loader.get_template(path)
|
try:
|
||||||
|
context['fundgoal'] = fundgoal_lookup(kwargs['fundraiser_sought'])
|
||||||
kwargs = kwargs.copy()
|
except KeyError:
|
||||||
if kwargs.has_key('fundraiser_sought'):
|
pass
|
||||||
kwargs['fundgoal'] = fundgoal_lookup(kwargs['fundraiser_sought'])
|
return TemplateResponse(request, path, context)
|
||||||
|
|
||||||
kwargs['sitefundgoal'] = fundgoal_lookup('cy2018-end-year-match')
|
|
||||||
|
|
||||||
context = RequestContext(request, kwargs)
|
|
||||||
content = template.render(context)
|
|
||||||
return HttpResponse(content, content_type)
|
|
||||||
|
|
||||||
def debug(request):
|
def debug(request):
|
||||||
path = request.get_full_path()
|
path = request.get_full_path()
|
||||||
path = path.lstrip('/')
|
path = path.lstrip('/')
|
||||||
return HttpResponse("Hello, static world: " + path)
|
return HttpResponse("Hello, static world: " + path)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue