Exemplo n.º 1
0
def _update_context_with_data(request, question_id, governorate_id,template, context={}, gender=None, age_group_list=None):
    question = get_object_or_404(Question, pk=question_id)
    national_response_break_up = question.response_break_up()
    response_break_up = question.response_break_up(governorate_code = governorate_id, gender = gender, age_group_list = age_group_list)

    choices_of_question = Choice.objects.filter(question = question)
    categories = question.get_categories()

    total_responses = sum([response_for_category["votes"] for response_for_category in response_break_up["by_category"]])

    response_by_category = ResponseBreakUp.create_from(response_break_up["by_category"], categories)

    national_response_by_category = ResponseBreakUp.create_from(national_response_break_up["by_category"], categories)

    response_by_choice = ChoiceBreakUp.create_from(response_break_up["by_choice"], choices_of_question)

    top_response = response_by_category[0]

    context.update( {"categories": categories,
                     "choices" : response_by_choice,
                     "question": question,
                     "top_response": top_response,
                     "total_responses" : total_responses,
                     "chart_data": simplejson.dumps([r.__dict__ for r in response_by_category]),
                     "national_data": simplejson.dumps([r.__dict__ for r in national_response_by_category]),
                     "character_english": character_english,
                     "questions" : Question.objects.all()
    }) 

    return render_to_response(request, template, context)
Exemplo n.º 2
0
def get_kml(request, question_id, kml, governorate, gender=None, age_group_list = None):
    """ the kml tells us where to center our bubbles on the map """
    question = Question.objects.get(id=question_id)
    placemarks_info_list = []
    style_dict_list = []
    for (counter, geography) in enumerate(kml):
        if governorate is not None:
            response_break_up = question.response_break_up(governorate_code = governorate.code, district_code = geography.code, gender = gender, age_group_list = age_group_list)
        else:
            response_break_up = question.response_break_up(governorate_code = geography.code, gender = gender, age_group_list = age_group_list)
        categories = question.get_categories()
        total_responses = sum([response_for_category["votes"] for response_for_category in response_break_up["by_category"]])
        if total_responses > 0:
            response_by_category = ResponseBreakUp.create_from(response_break_up["by_category"], categories)
            top_response = response_by_category[0]
            color = Color.objects.get(code = top_response.color)
            style_str = "s%s-%d" % (top_response.color, len(placemarks_info_list))
            placemarks_info_list.append({'id': geography.id,
                                         'name': geography.name,
                                         'description': geography.description,
                                         'kml': geography.kml,
                                         'style': style_str})
            style_dict_list.append({'id': top_response.color, 'percentage': 0.25 + (top_response.percentage/100),
                                    'file_name': color.file_name})
    style = 'kml/population_points.kml'
    r = _render_to_kml('kml/placemarks.kml', {'places' : placemarks_info_list,
                                              'style_dict_list' : style_dict_list,
                                              'style' : style})
    r['Content-Disposition'] = 'attachment;filename=boundaries.kml'
    return r