Files @ b4a2b9c613c1
Branch filter:

Location: website/conservancy/fundgoal/views.py

Bradley M. Kuhn
Calculate hours when only one day is left.

This {% widthratio %} trick is something I found online that didn't
require a django-math module to work, but I would have rather written
something that:

(b) Could have a different message when there is only one hour
left: “less than one hour left” (i.e., tested if hours == 1)

(c) Will say “hour” instead of “hours” when there is exactly 1 hour
left.
from django.http import JsonResponse

from .models import FundraisingGoal


def view(request):
    """JSON version of request
    """
    keysForJSON = [ 'fundraiser_goal_amount', 'fundraiser_so_far_amount', 'fundraiser_donation_count',
                    'fundraiser_donation_count_disclose_threshold' ]
    GET = request.GET
    codeNames = []
    if 'code_name' in GET:
        codeNames += GET.getlist('code_name')

    returnDict = {}
    for fundGoal in FundraisingGoal.objects.filter(fundraiser_code_name__in=codeNames):
        codeName = fundGoal.fundraiser_code_name
        returnDict[codeName] = {}
        for kk in keysForJSON:
            if hasattr(fundGoal, kk):
                returnDict[codeName][kk] = getattr(fundGoal, kk)

    return JsonResponse(returnDict)