<commit_before>from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse
from django.views import generic
from django.db.models import Count
from rest_framework import mixins, generics
from api.models import *
from api.serializers import *

# Create your views here.


class IndexView(generic.ListView):
    template_name = 'motions/index.html'
    context_object_name = 'motions'

    def get_queryset(self):
        queryset = Motion.objects.order_by('-session', '-motion_id', '-id')
        session = self.request.GET.get('session', None)
        type = self.request.GET.get('type', None)
        group = self.request.GET.get('group', None)
        proposer = self.request.GET.get('proposer', None)
        if session is not None:
            queryset = queryset.filter(session__session_date=session)
        if type is not None:
            queryset = queryset.filter(motion_type=type)
        if group is not None:
            queryset = queryset.filter(parliamentary_group__id=group)
        if proposer is not None:
            queryset = queryset.filter(proposer__name=proposer)
        return queryset


def motion_detail(request, id):
    motion = get_object_or_404(Motion, id=id)
    return render(request, 'motions/detail.html', {'motion': motion})


def motion_stats(request):
    stats = Motion.objects.values('parliamentary_group')\
        .annotate(num_total=Count('id', distinct=True),
                  num_answered=Count('answers__motion_id', distinct=True))\
        .order_by('-num_total')
    return HttpResponse(stats)

<commit_msg>add percent to answered stats<commit_after>from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse
from django.views import generic
from django.db.models import Count
from rest_framework import mixins, generics
from api.models import *
from api.serializers import *

# Create your views here.


class IndexView(generic.ListView):
    template_name = 'motions/index.html'
    context_object_name = 'motions'

    def get_queryset(self):
        queryset = Motion.objects.order_by('-session', '-motion_id', '-id')
        session = self.request.GET.get('session', None)
        type = self.request.GET.get('type', None)
        group = self.request.GET.get('group', None)
        proposer = self.request.GET.get('proposer', None)
        if session is not None:
            queryset = queryset.filter(session__session_date=session)
        if type is not None:
            queryset = queryset.filter(motion_type=type)
        if group is not None:
            queryset = queryset.filter(parliamentary_group__id=group)
        if proposer is not None:
            queryset = queryset.filter(proposer__name=proposer)
        return queryset


def motion_detail(request, id):
    motion = get_object_or_404(Motion, id=id)
    return render(request, 'motions/detail.html', {'motion': motion})


def motion_stats(request):
    stats = Motion.objects.values('parliamentary_group')\
        .annotate(num_total=Count('id', distinct=True),
                  num_answered=Count('answers__motion_id', distinct=True))\
        .order_by('-num_total')

    for stat in stats:
        stat['answered_percent'] = '{:.2f}'.format((stat['num_answered'] / stat['num_total']) * 100)

    stats = sorted(stats, key=lambda s: s['answered_percent'], reverse=True)

    return HttpResponse(stats)


{'fraction_non_alphanumeric': 0.10970588235294118, 'fraction_numerical': 0.004705882352941176, 'mean_word_length': 3.3997412677878396, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6759856', 'n_tokens_mistral': 1047, 'n_tokens_neox': 986, 'n_words': 234}