From a00195c8f6d08bebe20eaa7699fb28f3138f1f63 2018-11-20 16:58:34 From: Brett Smith Date: 2018-11-20 16:58:34 Subject: [PATCH] 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. --- diff --git a/www/conservancy/static/views.py b/www/conservancy/static/views.py index 33773d6bb2a95ca225ae0e3851f2fafc396bb90b..bbf2f56b8008bf267e47eb1c7143e1d120504e5f 100644 --- a/www/conservancy/static/views.py +++ b/www/conservancy/static/views.py @@ -1,8 +1,10 @@ import mimetypes import os.path from django.http import HttpResponse -from django.template import RequestContext, loader -from conservancy.apps.fundgoal.models import FundraisingGoal as FundraisingGoal +from django.template.response import TemplateResponse + +from conservancy.apps.fundgoal.models import FundraisingGoal +from conservancy.local_context_processors import fundgoal_lookup STATIC_ROOT = os.path.abspath(os.path.dirname(__file__)) FILESYSTEM_ENCODING = 'utf-8' @@ -11,10 +13,9 @@ def handler(request, errorcode): path = os.path.join('error', str(errorcode), 'index.html') fullpath = os.path.join(STATIC_ROOT, path) if not os.path.exists(fullpath): - return HttpResponse("Internal error: " + path) - template = loader.get_template(path) - context = RequestContext(request) - return HttpResponse(template.render(context), status=int(errorcode)) + return HttpResponse("Internal error: " + path, status=int(errorcode)) + else: + return TemplateResponse(request, path, status=int(errorcode)) def handler401(request): return handler(request, 401) @@ -28,13 +29,6 @@ def handler404(request): def handler500(request): 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): path = request.path.lstrip(u'/') if path.endswith(u'/'): @@ -49,23 +43,16 @@ def index(request, *args, **kwargs): return handler404(request) content_type, _ = mimetypes.guess_type(path) if content_type != 'text/html': - content = open(fullpath) + return HttpResponse(open(fullpath, 'rb'), content_type) else: - content_type = None # Let Django use its default - template = loader.get_template(path) - - kwargs = kwargs.copy() - if kwargs.has_key('fundraiser_sought'): - kwargs['fundgoal'] = fundgoal_lookup(kwargs['fundraiser_sought']) - - kwargs['sitefundgoal'] = fundgoal_lookup('cy2018-end-year-match') - - context = RequestContext(request, kwargs) - content = template.render(context) - return HttpResponse(content, content_type) + context = kwargs.copy() + try: + context['fundgoal'] = fundgoal_lookup(kwargs['fundraiser_sought']) + except KeyError: + pass + return TemplateResponse(request, path, context) def debug(request): path = request.get_full_path() path = path.lstrip('/') return HttpResponse("Hello, static world: " + path) -