def politician_statistic_view(request, politician_id): statistics = Statistic.get_statistics_by_politician(politician_id) category_list = [s.category.name for s in statistics] if request.GET.has_key('compare'): stats = get_cookie(request, 'statistics', {}) value_list = { 'politician' : [s.accordance for s in statistics], 'citizen' : [stats.get('category_%d' % s.category.id, 0) for s in statistics] } elif request.GET.has_key('evaluate'): value_list = [] category_list = [] statistics = get_cookie(request, 'statistics', {}) for k, v in statistics.iteritems(): category_id = int(re.sub('category_', '', k)) value_list.append(Statistic.get_accordance(politician_id, category_id, (int(v)*10))) category_list.append(Category.objects.get(id=category_id).name) else: value_list = [s.accordance for s in statistics] response = { 'categories' : category_list, 'values' : value_list } return JsonResponse(response)
def politician_statistic_spider_view_embed(request, politician_id): statistics = Statistic.get_statistics_by_politician(politician_id) return render( request, 'core/profile/spider_embed.html', { 'politician_id': politician_id } )
def politician_statistic_spider_view_embed(request, politician_id): statistics = Statistic.get_statistics_by_politician(politician_id) politician_url_reverse = reverse('politician', kwargs={'politician_id': politician_id}) politician_url_absolute = request.build_absolute_uri( politician_url_reverse) return render(request, 'core/profile/spider_embed.html', { 'politician_id': politician_id, 'politician_url': politician_url_absolute })
def politician_statistic_spider_view(request, politician_id): statistics = Statistic.get_statistics_by_politician(politician_id) stats = get_cookie(request, 'statistics', {}) values = { 'politician': [s.accordance for s in statistics], 'citizen': [stats.get('category_%d' % s.category.id, 0) for s in statistics] } return JsonResponse({ 'categories': [s.category.name for s in statistics], 'values': values })
def politician_statistic_spider_view(request, politician_id): statistics = Statistic.get_statistics_by_politician(politician_id) stats = get_cookie(request, 'statistics', {}) values = { 'politician' : [s.accordance for s in statistics], 'citizen' : [ stats.get('category_%d' % s.category.id, 0) for s in statistics] } return JsonResponse({ 'categories': [ s.category.name for s in statistics ], 'values': values })
def politician_statistic_view(request, politician_id): category_id = int(request.GET.get('category', False)) titles = [force_text(_('total'))] if category_id: category = get_object_or_404(Category, id=category_id) titles.append(category.name) if 'evaluate' in request.GET: cat_by_id = {cat.id: cat for cat in Category.objects.all()} pol_answers = Answer.objects.filter(politician_id=politician_id) pairs = [] answers = get_cookie(request, 'answers', {}) delta_by_cat = collections.defaultdict(lambda: []) for ans in pol_answers: voter_value = int(answers.get('question_%s' % ans.question_id, 0)) delta = abs(ans.agreement_level - voter_value) delta_by_cat[ans.question.category_id].append(delta) for cid, cat in cat_by_id.items(): if not len(delta_by_cat[cid]): continue pairs.append({ 'category': cat.name, 'value': (10 - sum(delta_by_cat[cid]) / float(len(delta_by_cat[cid]))) }) sorted_pairs = sorted(pairs, key=lambda k: k['category']) detail = { 'categories': [i['category'] for i in sorted_pairs], 'values': [i['value'] for i in sorted_pairs] } total = sum(detail['values']) / len(detail['values']) pos = [total] neg = [(10 - total)] if category_id: val = 10 - (sum(delta_by_cat[category_id]) / float(len(delta_by_cat[category_id]))) pos.append(val) neg.append(10 - val) summary = { 'titles': titles, 'values': { 'positive': pos, 'negative': neg } } else: statistics = Statistic.get_statistics_by_politician(politician_id) values = [s.accordance for s in statistics] total = sum(values) / len(values) # pos is the green part (agreement level) of the graph, # neg is the "rest" (red) pos = [total] neg = [(10 - total)] if category_id: # if category_id is given, the graph should display this # category in addition to the summary view statistic = Statistic.objects.get(politician_id=politician_id, category=category) pos.append(statistic.value / 10) neg.append(10 - statistic.value / 10) summary = { 'titles': titles, 'values': { 'positive': pos, 'negative': neg } } detail = { 'categories': [s.category.name for s in statistics], 'values': values } return JsonResponse({'summary': summary, 'detail': detail})
def politician_statistic_view(request, politician_id): category_id = int(request.GET.get('category', False)) titles = [force_unicode(_('total'))] if category_id: category = get_object_or_404(Category, id=category_id) titles.append(category.name) if 'evaluate' in request.GET: cat_by_id = { cat.id: cat for cat in Category.objects.all() } pol_answers = Answer.objects.filter(politician_id=politician_id) pairs = [] answers = get_cookie(request, 'answers', {}) delta_by_cat = collections.defaultdict(lambda: []) for ans in pol_answers: voter_value = int(answers.get('question_%s' % ans.question_id, 0)) delta = abs(ans.agreement_level - voter_value) delta_by_cat[ans.question.category_id].append(delta) for cid, cat in cat_by_id.iteritems(): pairs.append({ 'category': cat.name, 'value': ( 10 - sum(delta_by_cat[cid]) / float(len(delta_by_cat[cid])) ) }) sorted_pairs = sorted(pairs, key=lambda k: k['category']) detail = { 'categories' : [i['category'] for i in sorted_pairs], 'values' : [i['value'] for i in sorted_pairs] } total = sum(detail['values']) / len(detail['values']) pos = [total] neg = [(10 - total)] if category_id: val = 10 - ( sum(delta_by_cat[category_id]) / float(len(delta_by_cat[category_id]))) pos.append(val) neg.append(10 - val) summary = { 'titles' : titles, 'values' : { 'positive' : pos, 'negative' : neg } } else: statistics = Statistic.get_statistics_by_politician(politician_id) values = [s.accordance for s in statistics] total = sum(values) / len(values) # pos is the green part (agreement level) of the graph, # neg is the "rest" (red) pos = [total] neg = [(10 - total)] if category_id: # if category_id is given, the graph should display this # category in addition to the summary view statistic = Statistic.objects.get( politician_id=politician_id, category=category) pos.append(statistic.value / 10) neg.append(10 - statistic.value / 10) summary = { 'titles' : titles, 'values' : { 'positive' : pos, 'negative' : neg } } detail = { 'categories' : [s.category.name for s in statistics], 'values' : values } return JsonResponse({'summary': summary, 'detail': detail})