def quiz_subscribe(request): subscribe_form = forms.MultiServiceSubscribeForm(request.POST) if subscribe_form.is_valid(): name = subscribe_form.cleaned_data['name'] email = subscribe_form.cleaned_data['email'] postcode = subscribe_form.cleaned_data['postcode'] post_election_signup = PostElectionSignup( name = name, email = email, postcode = postcode, theyworkforyou = subscribe_form.cleaned_data['twfy_signup'], hearfromyourmp = subscribe_form.cleaned_data['hfymp_signup'], ) post_election_signup.put() if subscribe_form.cleaned_data['democlub_signup']: (first_name, space, last_name) = name.partition(" ") fields = { 'first_name' : first_name, 'last_name' : last_name, 'email' : email, 'postcode' : postcode } form_data = urllib.urlencode(fields) result = urlfetch.fetch(url = "http://www.democracyclub.org.uk", payload = form_data, method = urlfetch.POST) if result.status_code != 200: pass #raise Exception("Error posting to DemocracyClub") candidacy_without_response_count = request.POST.get('candidacy_without_response_count',0) seat = forms._postcode_to_constituency(postcode) democlub_redirect = subscribe_form.cleaned_data['democlub_redirect'] if democlub_redirect: democlub_hassle_url = seat.democracyclub_url() + "?email=%s&postcode=%s&name=%s" % ( urlquote_plus(email), urlquote_plus(postcode), urlquote_plus(name) ) return HttpResponseRedirect(democlub_hassle_url) # For later when we have form even if have got all candidates answers return render_to_response('quiz_subscribe_thanks.html', { 'twfy_signup': subscribe_form.cleaned_data['twfy_signup'], 'hfymp_signup': subscribe_form.cleaned_data['hfymp_signup'], 'democlub_signup': subscribe_form.cleaned_data['democlub_signup'], 'seat': seat, 'email': email, 'urlise_postcode': forms._urlise_postcode(postcode), 'name': name, 'candidacy_without_response_count':candidacy_without_response_count } ) return render_to_response('quiz_subscribe.html', { 'subscribe_form' : subscribe_form })
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 })
def quiz_by_postcode(request, postcode): seat = forms._postcode_to_constituency(postcode) if seat == None: raise Http404 return quiz_main(request, seat, postcode)