<commit_before># coding=UTF-8
from django.contrib.auth.decorators import login_required
from django.shortcuts import render
from django.db.models import Avg, Count, F, Max, Min, Sum, Q, Prefetch
from django.http import JsonResponse
from forms import UploadFileForm
from models import Election, Candidate, Vote
from properties_decoder import coder, decoder, coder_election_types
from tree import get_election_tree
from geography import create_geo_data
import json


# Vote.objects.filter(election__election_type='district').distinct('political_party') and list all political parties

# Vote.objects.filter(
#     election__election_type='district', 
#     political_party='<NAME> Wyborców Miasto Jest Nasze-Mieszkańców Śródmieścia')
# .aggregate(Sum('amount'))
# >{'amount__sum': 5581}


#//Sample query with sum of votes, statictics
# Election.objects.filter(election_type='district')
#.aggregate(Sum('number_of_voters'), Sum('votes_valid'), Sum('cards_given'), Sum('cards_taken'))

def get_geography(request):
    pass


# TODO: move body to tree.py and create new function
def get_areas_tree(request):
    tree = []
    for el in Election.objects.filter().distinct('election_type'):
        if not el.election_type in ['president_first_turn_districts', 'president_second_turn_districts']:
            election = {
                'name': coder_election_types[el.election_type],
                'children': get_election_tree(el.election_type),
                'url': el.election_type + '/',
                'type': el.election_type
            }
            tree.append(election)

    return JsonResponse(tree, safe=False)


def get_stats(request, **kwargs):
    votes_kwargs = {}
    for k, v in kwargs.iteritems():
        if k != 'political_party':
            votes_kwargs['election__' + k] = float(v) if 'number' in k else v

    district_data = {'geography': create_geo_data(**kwargs), 'votes': []}

    for v in Vote.objects.filter(**votes_kwargs).distinct('political_party'):
        votes_kwargs['political_party'] = v.political_party
        obj = {
            'political_party': v.political_party,
            'amount': Vote.objects.filter(**votes_kwargs).aggregate(Sum('amount'))['amount__sum']
        }
        votes_kwargs.pop('political_party')
        obj['percentage'] = round(float(obj['amount'])/ Vote.objects.filter(**votes_kwargs).aggregate(Sum('amount'))['amount__sum']*100, 2)
        district_data['votes'].append(obj)

    return JsonResponse(district_data)


def get_candidates(request, **kwargs):
    pass


def create_models(request):
    json_file = json.load(request.FILES['file'])
    type = request.POST['options']

    for obj in json_file:
        new_model = {'election_type': type}
        votes = []
        for k, v in obj.iteritems():
            if 'Pkt' in k:
                try:
                    new_model['notes'].append({k: v})
                except:
                    new_model['notes'] = [{k: v}]
                continue
            
            k_coded = coder[k.encode('utf-8')]

            if 'kw' in k_coded or 'prez' in k_coded:<commit_msg>candidates added to statictics data.<commit_after># coding=UTF-8
from django.contrib.auth.decorators import login_required
from django.shortcuts import render
from django.db.models import Sum
from django.forms.models import model_to_dict
from django.http import JsonResponse
from forms import UploadFileForm
from models import Election, Candidate, Vote
from properties_decoder import coder, coder_election_types
from tree import get_election_tree
from geography import create_geo_data
import json


# Vote.objects.filter(election__election_type='district').distinct('political_party') and list all political parties

# Vote.objects.filter(
#     election__election_type='district', 
#     political_party='Komitet Wyborczy Wyborców Miasto Jest Nasze-Mieszkańców Śródmieścia')
# .aggregate(Sum('amount'))
# >{'amount__sum': 5581}


#//Sample query with sum of votes, statictics
# Election.objects.filter(election_type='district')
#.aggregate(Sum('number_of_voters'), Sum('votes_valid'), Sum('cards_given'), Sum('cards_taken'))

def get_geography(request):
    pass


# TODO: move body to tree.py and create new function
def get_areas_tree(request):
    tree = []
    for el in Election.objects.filter().distinct('election_type'):
        if not el.election_type in ['president_first_turn_districts', 'president_second_turn_districts']:
            election = {
                'name': coder_election_types[el.election_type],
                'children': get_election_tree(el.election_type),
                'url': el.election_type + '/',
                'type': el.election_type
            }
            tree.append(election)

    return JsonResponse(tree, safe=False)


def get_stats(request, **kwargs):
    votes_kwargs = {}
    

    for k, v in kwargs.iteritems():
        if k != 'political_party':
            votes_kwargs['election__' + k] = float(v) if 'number' in k else v

    district_data = {'geography': create_geo_data(**kwargs), 'votes': []}

    for v in Vote.objects.filter(**votes_kwargs).distinct('political_party'):
        votes_kwargs['political_party'] = v.political_party
        obj = {
            'political_party': v.political_party,
            'amount': Vote.objects.filter(**votes_kwargs).aggregate(Sum('amount'))['amount__sum']
        }
        votes_kwargs.pop('political_party')
        obj['percentage'] = round(float(obj['amount'])/ Vote.objects.filter(**votes_kwargs).aggregate(Sum('amount'))['amount__sum']*100, 2)
        district_data['votes'].append(obj)

    if kwargs['election_type'] in ['district', 'city_council', 'voivodeship']:
        district_data['candidates'] = get_candidates(**kwargs)

    return JsonResponse(district_data)


def get_candidates(**kwargs):
    if kwargs['election_type'] == 'district':
        kwargs['grade'] = 'DZ'
    elif kwargs['election_type'] == 'city_council':
        kwargs['grade'] = 'GM'
    elif kwargs['election_type'] == 'voivodeship':
        kwargs['grade'] = 'WO'
    try:
        kwargs['district']
        kwargs['voivodeship'] = kwargs.pop('district')
        if kwargs['election_type'] == 'voivodeship':
            kwargs.pop('voivodeship')
    except:
        pass
    try:
        kwargs.pop('number_of_electoral_circuit')
    except:
        pass
    kwargs.pop('election_type')
    candidates = []

    for c in Candidate.objects.filter(**kwargs):
        canditate = model_to_dict(c)
        canditate['fullName'] = canditate['names'].split(" ")[0] + ' ' + canditate['surname']
        candidates.append(canditate)
    return candidates


def get_candidates_view(request, **kwargs):
    pass


def create_models(request):
    json_file = json.load(request.FILES['file'])
    type = request.POST['options']

    for obj in json_file:
        new_model = {'election_type': type}
        votes = []
        for k, v in obj.iteritems():
            if 'Pkt' in k:
                try:
                    new_model['notes'].append({k: v})
                except:
                    new_model['notes'] = [{k: v}]
                continue
            
            k_coded = coder[k.encode('utf-8')]

            if 'kw' in k_coded or 'prez' in k_coded:
{'fraction_non_alphanumeric': 0.12801765760794592, 'fraction_numerical': 0.0028969513036280867, 'mean_word_length': 3.1956018518518516, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6060742', 'n_tokens_mistral': 2286, 'n_tokens_neox': 2155, 'n_words': 468}