Changeset - a00195c8f6d0
[Not reviewed]
0 1 0
Brett Smith (brett) - 3 years ago 2018-11-20 16:58:34
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

* Use TemplateResponse. This requires less boilerplate and ensures the
rendering runs through our local context processors.
* Use the existing definition of fundgoal_lookup.
1 file changed with 14 insertions and 27 deletions:
0 comments (0 inline, 0 general)
Show inline comments
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__))

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))
        return TemplateResponse(request, path, status=int(errorcode))

def handler401(request):
    return handler(request, 401)

def handler403(request):
    return handler(request, 403)
@@ -25,19 +26,12 @@ def handler403(request):
def handler404(request):
    return handler(request, 404)

def handler500(request):
    return handler(request, 500)

def fundgoal_lookup(fundraiser_sought):
        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'/'):
        path += u'index.html'
        path_bytes = path.encode(FILESYSTEM_ENCODING)
@@ -46,26 +40,19 @@ def index(request, *args, **kwargs):
        return handler404(request)
    fullpath = os.path.join(STATIC_ROOT, path_bytes)
    if not os.path.exists(fullpath):
        return handler404(request)
    content_type, _ = mimetypes.guess_type(path)
    if content_type != 'text/html':
        content = open(fullpath)
        return HttpResponse(open(fullpath, 'rb'), content_type)
        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()
            context['fundgoal'] = fundgoal_lookup(kwargs['fundraiser_sought'])
        except KeyError:
        return TemplateResponse(request, path, context)

def debug(request):
    path = request.get_full_path()
    path = path.lstrip('/')
    return HttpResponse("Hello, static world: " + path)

0 comments (0 inline, 0 general)