Exemplo n.º 1
0
def quiz_main(request, seat, postcode):
    display_postcode = ""
    url_postcode = ""
    if postcode:
        display_postcode = forms._canonicalise_postcode(postcode)
        url_postcode = forms._urlise_postcode(postcode)

    params = _get_quiz_main_params_memcached(seat)

    subscribe_form = forms.MultiServiceSubscribeForm(initial={ 
        'postcode': display_postcode,
        'democlub_signup': True,
        'twfy_signup': True,
        'hfymp_signup': True,
    })
    
    referer = request.META.get('HTTP_REFERER','')
    if referer:
        is_local = False
        for local_domain_part in settings.LOCAL_DOMAIN_PARTS:
            if local_domain_part in referer:
                is_local = True
        params['external_referer'] = not is_local
    m = hashlib.md5()
    m.update(referer)
    m.update(request.META.get('HTTP_USER_AGENT', ''))
    params['votetoken'] = m.hexdigest()
    params['seat'] = seat
    params['postcode'] = postcode
    params['subscribe_form'] = subscribe_form

    return render_to_response('quiz_main.html', params)
Exemplo n.º 2
0
def quiz_main(request, postcode):
    display_postcode = forms._canonicalise_postcode(postcode)
    url_postcode = forms._urlise_postcode(postcode)
    seat = forms._postcode_to_constituency(postcode)

    # find all the candidates
    candidacies = seat.candidacy_set.filter("deleted = ", False).fetch(1000)
    candidacies_by_key = {}
    for c in candidacies:
        candidacies_by_key[str(c.key())] = c
    candidacies_key = set(candidacies_by_key.keys())

    # local and national issues for the seat
    national_issues = db.Query(RefinedIssue).filter('national =', True).filter("deleted =", False).fetch(1000)
    local_issues = seat.refinedissue_set.filter("deleted =", False).fetch(1000)

    # responses candidates have made
    all_responses = db.Query(SurveyResponse).filter('candidacy in', candidacies).fetch(1000) # this is slow on dev server, much faster on live

    candidacies_with_response_key = set()
    # construct dictionaries with all the information in 
    national_answers = []
    for national_issue in national_issues:
        new_entry = _get_entry_for_issue(candidacies_by_key, all_responses, candidacies_with_response_key, national_issue)
        national_answers.append(new_entry)
    local_answers = []
    for local_issue in local_issues:
        new_entry = _get_entry_for_issue(candidacies_by_key, all_responses, candidacies_with_response_key, local_issue)
        local_answers.append(new_entry)

    # work out who didn't give a response
    candidacies_without_response_key = candidacies_key.difference(candidacies_with_response_key)
    candidacies_without_response = [ { 
        'name': candidacies_by_key[k].candidate.name, 
        'party': candidacies_by_key[k].candidate.party.name,
        'image_url': candidacies_by_key[k].candidate.image_url(),
        'party_image_url': candidacies_by_key[k].candidate.party.image_url(),
        'yournextmp_url': candidacies_by_key[k].candidate.yournextmp_url(),
        'survey_invite_emailed': candidacies_by_key[k].survey_invite_emailed
    } for k in candidacies_without_response_key]

    return render_to_response('quiz_main.html', {
        'seat' : seat,
        'candidacies_without_response' : candidacies_without_response,
        'candidacy_count' : len(candidacies),
        'candidacy_with_response_count' : len(candidacies) - len(candidacies_without_response),
        'candidacy_without_response_count' : len(candidacies_without_response),
        'national_answers' : national_answers,
        'local_answers' : local_answers,
        'local_issues_count' : len(local_issues),
        'postcode' : postcode
    })