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 update_population_density(geounit_set): subject, created = Subject.objects.get_or_create(name='Population Density') for geounit in geounit_set: total_population = geounit.statistic_set.get(subject__name='Total Population').value area = geounit.area_sq_mi() stat = Statistic(subject=subject, geounit=geounit, value=total_population / area) stat.save()
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 handle(self, *args, **options): subject, created = Subject.objects.get_or_create(name=options["subject"]) for csvfile in args: subject_reader = csv.DictReader(open(csvfile, "rb")) for row in subject_reader: try: block = CensusBlock.objects.get(geoid10=row[options["geoidfield"]].strip()) stat = Statistic(subject=subject, geounit=block, value=row[options["valuefield"]]) sys.stderr.write("Setting %s for %s to %s\n" % (subject.name, stat.geounit.geoid10, stat.value)) stat.save() except ObjectDoesNotExist: # Some blocks might not exist in our system since we're # likely only loading ones in the cities. sys.stderr.write( "Block with GEOID10 %s not found in system\n" % (row[options["geoidfield"]].strip()) )
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 parse(self): global TODO_user TODO_user, flag = User.objects.get_or_create(username="******") # Collect all player names names = [] with open(self.filename, "r") as f: for line in f: splited_line = line.split(",") if splited_line[0] == "Nr": continue if splited_line[1] == "": continue if len(splited_line) >= 6: normalized_name = normalize_name(splited_line[2]) if normalized_name not in names: names.append(normalized_name) normalized_name = normalize_name(splited_line[4]) if normalized_name not in names: names.append(normalized_name) # Create all players players = [] for name in names: players.append(Player(name=name, user=TODO_user)) Player.objects.bulk_create(players) players = Player.objects.all() # Create players statistics statistics = [] for player in players: statistics.append(Statistic(player=player)) Statistic.objects.bulk_create(statistics) statistics = Statistic.objects.all() # Cache all data for player in players: player_cache.create(pk=player.pk, item=player, unique=player.name) for stat in statistics: statistic_cache.create(pk=stat.pk, item=stat, unique=stat.player.pk) # Process games games = [] with open(self.filename, "r") as f: for line in f: splited_line = line.split(",") if splited_line[0] == "Nr": continue if splited_line[1] == "": continue if len(splited_line) >= 6: process(games, splited_line[:6]) # NO DATA P1 S1 P2 S2 # 0 1 2 3 4 5 # Create all games Game.objects.bulk_create(games) # Atomic update all data with transaction.atomic(): for key, player in player_cache.data.items(): player.save() for key, stat in statistic_cache.data.items(): stat.save()
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})