def statistics(request):
    _check_for_admin()
    question = get_question()
    chooses = question.chooses
    answers = dict((ans.key().id(), {"answer":ans, "count": 0, "chooses":[]}) for ans in question.answers)

    for choose in chooses:
        for answer in Answer.get(choose.answers):
            if answer:
                key = answer.key()
                id = key.id()
                answers[id]["count"] += 1
                answer = Answer.get(key)
                answers[id]["chooses"] = Choose.gql("where answers = :1 order by when", answer).fetch(200)

    return render_to_response('statistics.html', {
            'answers':sorted(answers.items(), key=lambda a:a[1]["count"], reverse=True)
        }, context_instance=RequestContext(request))
def save_user_choice(request):
    question = get_question()
    
    post = request.POST
    if post:
        answer_ids = map(int, post.getlist('chooses'))
        username = post['username'] if 'username' in post else None 
        answers = Answer.get_by_id(answer_ids)
        user = users.get_current_user()
        if user:
            user_choose = Choose.gql("WHERE user = :1", user).get()
            if not user_choose:
                user_choose = Choose(user=user)
        else:
            user_choose = Choose(username=username)
        user_choose.question=question

        user_choose.answers = [a.key() for a in answers]
        user_choose.save()

    return redirect("polls.views.thank_you")
def main_page(request):
    question = get_question()

    if users.is_current_user_admin():
        post = request.POST
        if post:
            question.text = post["answer_name"]
            question.save()
            return redirect("polls.views.main_page")

    urlmap = {}
    for p in openIdProviders:
        p_name = p.split('.')[0]
        p_url = p.lower()
        urlmap[p_name] = users.create_login_url(federated_identity=p_url, dest_url="/")
    nulldate = datetime(1970, 1, 1).date()
    answers = question.answers.fetch(1000)
    def date_cmp(a,b):
        a,b = a.date or nulldate, b.date or nulldate
        return 1 if a > b else -1 if a < b else 0
    answers.sort(cmp=date_cmp)
    user = users.get_current_user()
    user_answer_keys = []
    if user:
        user_choose = Choose.gql("WHERE user = :1", user).get()
        if user_choose:
            user_answer_keys = user_choose.answers

    return render_to_response('main.html',
                              {'answers':answers,
                               'user_answer_keys':user_answer_keys,
                               'is_admin':users.is_current_user_admin(),
                               'login_url':users.create_login_url("/"),
                               'login_urls_map':urlmap,
                               'user': user,
                               'logout_url':users.create_logout_url("/"),
                               'question':question},
                              context_instance=RequestContext(request))