<commit_before>import json

from csp.decorators import csp_update
from django.conf import settings
from django.conf.urls.static import static
from django.db.models import Q, Count
from django.shortcuts import render, get_object_or_404
from django.templatetags.static import static
from django.urls import reverse
from django.utils import html
from django.utils import timezone
from django.views.generic import DetailView
from requests.utils import quote

from mainapp.documents import DOCUMENT_TYPE_NAMES, DOCUMENT_TYPE_NAMES_PL
from mainapp.functions.document_parsing import index_papers_to_geodata
from mainapp.models import Body, File, Organization, Paper, Meeting, Person, LegislativeTerm, \
    Location
from mainapp.models.organization import ORGANIZATION_TYPE_NAMES_PLURAL
from mainapp.models.organization_type import OrganizationType
from mainapp.views import person_grid_context


def index(request):
    main_body = Body.objects.get(id=settings.SITE_DEFAULT_BODY)

    latest_paper = Paper.objects.order_by("-sort_date", "-legal_date")[:10]
    for paper in latest_paper:
        # The mixed results view needs those
        setattr(paper, "type", "paper")
        setattr(paper, "name_escaped", html.escape(paper.name))
        setattr(paper, "type_translated", DOCUMENT_TYPE_NAMES[paper.type])
        setattr(paper, "url", paper.get_default_link())

    geo_papers = Paper \
                     .objects \
                     .order_by("-sort_date", "-legal_date") \
                     .prefetch_related('files') \
                     .prefetch_related('files__locations')[:50]

    stats = {
        "file": File.objects.count(),
        "meeting": Meeting.objects.count(),
        "organization": Organization.objects.count(),
        "paper": Paper.objects.count(),
        "person": Person.objects.count()
    }

    context = {
        'map': _build_map_object(main_body, geo_papers),
        'latest_paper': latest_paper,
        'next_meetings': Meeting.objects.filter(start__gt=timezone.now()).order_by("start")[:2],
        'stats': stats
    }

    if request.GET.get('version', 'v2') == 'v2':
        return render(request, 'mainapp/index_v2/index.html', context)
    else:
        return render(request, 'mainapp/index/index.html', context)


def _build_map_object(body: Body, geo_papers):
    if body.outline:
        outline = body.outline.geometry
    else:
        outline = None

    return json.dumps({
        'center': settings.SITE_GEO_CENTER,
        'zoom': settings.SITE_GEO_INIT_ZOOM,
        'limit': settings.SITE_GEO_LIMITS,
        'outline': outline,
        'documents': index_papers_to_geodata(geo_papers),
        'mapboxKey': settings.SITE_MAPBOX_ACCESS_TOKEN,
        'tileUrl': settings.SITE_MAPBOX_TILE_URL,
    })


def organizations(request):
    organizations_ordered = []
    for organization_type in OrganizationType.objects.all():
        ct1 = Count('organizationmembership', distinct=True)
        ct2 = Count('paper', distinct=True)
        ct3 = Count('meeting', distinct=True)
        all_orgas = Organization.objects.annotate(ct1, ct2, ct3).filter(
            organization_type=organization_type).all()

        organizations_ordered.append({
            "organization_type": organization_type,
            "type": ORGANIZATION_TYPE_NAMES_PLURAL.get(organization_type.name,
                                                       organization_type.name),
            "all": all_orgas,
        })

    for i in settings.ORGANIZATION_TYPE_SORTING:
        j = 0
        while j < len(organizations_ordered):
            if organizations_ordered[j]["organization_type"].id == i:
                popped = organizations_ordered.pop(j)
                organizations_ordered.insert(0, popped)
            j += 1

    context = {
        "organizations": organizations_ordered,
        "main_organization": settings.SITE_DEFAULT_ORGANIZATION,
    }
    return render(request, "mainapp/organizations.html", context)


def paper(request, pk):
    paper = get_object_or_404(Paper, id=pk)
    context = {
        "paper": paper,
        "consultations": paper.consultation_set.all(),
    }
    return render(request, "mainapp/paper.html", context)


def historical_paper(request, pk):
    historical_paper = get_object_or_404(Paper.history, history_id=pk)
    context = {
        "paper": historical_paper.instance,
        "historical": historical_paper,
        "consultations": [],  # Blocked by #63
    }
    return render(request, "mainapp/paper.html", context)


def organization(request, pk):
    organization = get_object_or_404(Organization, id=pk)

    members, parliamentarygroups = person_grid_context(organization)

    context = {
        "members": members,
        "parliamentary_groups": parliamentarygroups,
        "organization": organization,
        "papers": Paper.objects.filter(organizations__in=[pk])
                      .order_by('legal_date', 'modified')[:25],
        "paper_count": Paper.objects.filter(organizations__in=[pk]).count(),
        "meetings": Meeting.objects.filter(organizations__in=[pk])
                        .order_by('start', 'modified')[:25],
        "meeting_count": Meeting.objects.filter(organizations__in=[pk]).count(),
        "to_search_url": reverse("search", args=["organization:" + str(organization.id)])
    }
    return render(request, "mainapp/organization.html", context)


@csp_update(FRAME_SRC=("'self'", "blob:"))  # Needed for downloading the PDF in PDF.JS
def file(request, pk):
    file = get_object_or_404(File, id=pk)

    renderer = "download"

    if file.mime_type == "application/pdf":<commit_msg>Show newest meeting first in organization<commit_after>import json

from csp.decorators import csp_update
from django.conf import settings
from django.conf.urls.static import static
from django.db.models import Q, Count
from django.shortcuts import render, get_object_or_404
from django.templatetags.static import static
from django.urls import reverse
from django.utils import html
from django.utils import timezone
from django.views.generic import DetailView
from requests.utils import quote

from mainapp.documents import DOCUMENT_TYPE_NAMES, DOCUMENT_TYPE_NAMES_PL
from mainapp.functions.document_parsing import index_papers_to_geodata
from mainapp.models import Body, File, Organization, Paper, Meeting, Person, LegislativeTerm, \
    Location
from mainapp.models.organization import ORGANIZATION_TYPE_NAMES_PLURAL
from mainapp.models.organization_type import OrganizationType
from mainapp.views import person_grid_context


def index(request):
    main_body = Body.objects.get(id=settings.SITE_DEFAULT_BODY)

    latest_paper = Paper.objects.order_by("-sort_date", "-legal_date")[:10]
    for paper in latest_paper:
        # The mixed results view needs those
        setattr(paper, "type", "paper")
        setattr(paper, "name_escaped", html.escape(paper.name))
        setattr(paper, "type_translated", DOCUMENT_TYPE_NAMES[paper.type])
        setattr(paper, "url", paper.get_default_link())

    geo_papers = Paper \
                     .objects \
                     .order_by("-sort_date", "-legal_date") \
                     .prefetch_related('files') \
                     .prefetch_related('files__locations')[:50]

    stats = {
        "file": File.objects.count(),
        "meeting": Meeting.objects.count(),
        "organization": Organization.objects.count(),
        "paper": Paper.objects.count(),
        "person": Person.objects.count()
    }

    context = {
        'map': _build_map_object(main_body, geo_papers),
        'latest_paper': latest_paper,
        'next_meetings': Meeting.objects.filter(start__gt=timezone.now()).order_by("start")[:2],
        'stats': stats
    }

    if request.GET.get('version', 'v2') == 'v2':
        return render(request, 'mainapp/index_v2/index.html', context)
    else:
        return render(request, 'mainapp/index/index.html', context)


def _build_map_object(body: Body, geo_papers):
    if body.outline:
        outline = body.outline.geometry
    else:
        outline = None

    return json.dumps({
        'center': settings.SITE_GEO_CENTER,
        'zoom': settings.SITE_GEO_INIT_ZOOM,
        'limit': settings.SITE_GEO_LIMITS,
        'outline': outline,
        'documents': index_papers_to_geodata(geo_papers),
        'mapboxKey': settings.SITE_MAPBOX_ACCESS_TOKEN,
        'tileUrl': settings.SITE_MAPBOX_TILE_URL,
    })


def organizations(request):
    organizations_ordered = []
    for organization_type in OrganizationType.objects.all():
        ct1 = Count('organizationmembership', distinct=True)
        ct2 = Count('paper', distinct=True)
        ct3 = Count('meeting', distinct=True)
        all_orgas = Organization.objects.annotate(ct1, ct2, ct3).filter(
            organization_type=organization_type).all()

        organizations_ordered.append({
            "organization_type": organization_type,
            "type": ORGANIZATION_TYPE_NAMES_PLURAL.get(organization_type.name,
                                                       organization_type.name),
            "all": all_orgas,
        })

    for i in settings.ORGANIZATION_TYPE_SORTING:
        j = 0
        while j < len(organizations_ordered):
            if organizations_ordered[j]["organization_type"].id == i:
                popped = organizations_ordered.pop(j)
                organizations_ordered.insert(0, popped)
            j += 1

    context = {
        "organizations": organizations_ordered,
        "main_organization": settings.SITE_DEFAULT_ORGANIZATION,
    }
    return render(request, "mainapp/organizations.html", context)


def paper(request, pk):
    paper = get_object_or_404(Paper, id=pk)
    context = {
        "paper": paper,
        "consultations": paper.consultation_set.all(),
    }
    return render(request, "mainapp/paper.html", context)


def historical_paper(request, pk):
    historical_paper = get_object_or_404(Paper.history, history_id=pk)
    context = {
        "paper": historical_paper.instance,
        "historical": historical_paper,
        "consultations": [],  # Blocked by #63
    }
    return render(request, "mainapp/paper.html", context)


def organization(request, pk):
    organization = get_object_or_404(Organization, id=pk)

    members, parliamentarygroups = person_grid_context(organization)

    context = {
        "members": members,
        "parliamentary_groups": parliamentarygroups,
        "organization": organization,
        "papers": Paper.objects.filter(organizations__in=[pk])
                      .order_by('legal_date', 'modified')[:25],
        "paper_count": Paper.objects.filter(organizations__in=[pk]).count(),
        "meetings": Meeting.objects.filter(organizations__in=[pk])
                        .order_by('-start', 'modified')[:25],
        "meeting_count": Meeting.objects.filter(organizations__in=[pk]).count(),
        "to_search_url": reverse("search", args=["organization:" + str(organization.id)])
    }
    return render(request, "mainapp/organization.html", context)


@csp_update(FRAME_SRC=("'self'", "blob:"))  # Needed for downloading the PDF in PDF.JS
def file(request, pk):
    file = get_object_or_404(File, id=pk)

    renderer = "download"

    if file.mime_type == "application/pdf":
{'fraction_non_alphanumeric': 0.11382761980689167, 'fraction_numerical': 0.0067322172025865885, 'mean_word_length': 3.1737523105360443, 'pattern_counts': {'":': 48, '<': 5, '<?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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7354145', 'n_tokens_mistral': 3453, 'n_tokens_neox': 3280, 'n_words': 650}