<commit_before>#!/usr/bin/python3

from datetime import datetime
import os
import re
from collections import defaultdict
from django.db.models.query_utils import select_related_descend
from django.http.response import JsonResponse

from django.conf import settings
from django.shortcuts import render, redirect, reverse
from django.http import HttpResponseForbidden, HttpResponseBadRequest, HttpResponse
from django.contrib.auth.decorators import login_required
from django.utils import html
from django.template.context_processors import csrf, request
from django.db.models import Count, Q
from django.db.models.functions import Lower
from openlocationcode import openlocationcode
import pytz

from villes import plus_code
from pages.models import TexteDAccueil

from .. import models as m
from .getters import get_network, get_delivery, must_be_prod_or_staff, must_be_staff

from .edit_delivery_purchases import edit_delivery_purchases
from .buy import buy
from .user_registration import user_register, user_update, user_deactivate
from .edit_delivery_products import edit_delivery_products, delivery_products_json
from .view_purchases import (
    view_purchases_html,
    view_purchases_latex_table,
    view_purchases_xlsx,
    view_purchases_latex_cards,
    view_purchases_json,
    all_deliveries_html,
    all_deliveries_latex,
)
from .spreadsheet import spreadsheet
from .candidacies import (
    cancel_candidacy,
    validate_candidacy,
    leave_network,
    create_candidacy,
    manage_candidacies,
)
from .invoice_mail import invoice_mail_form
from .users import users_json, users_html
from . import delivery_description as dd
from . import latex


def index(request):
    try:
        accueil = TexteDAccueil.objects.all().first().texte_accueil
    except AttributeError:
        accueil = "Penser à renseigner le texte d'accueil :-)"
    if request.user.is_anonymous:
        vars = {"networks": m.Network.objects
            .exclude(visible=False)
            .exclude(active=False)
            .exclude(active=False),
            "accueil": accueil
        }
        vars.update(csrf(request))
        return render(request, "index_unlogged.html", vars)
    else:
        mbships = list(m.NetworkMembership.objects
            .filter(user=request.user, valid_until=None, network__active=True)
        )
        for nm in mbships:
            hoo = (
                nm.is_buyer
                # TODO can be performed into a single query with network_id__in
                and m.Delivery.objects.filter(
                    network_id=nm.network_id, state=m.Delivery.ORDERING_ALL
                ).exists()
            )
            nm.has_open_orders = hoo

        number_of_deliveries = m.Delivery.objects.filter(
            state=m.Delivery.ORDERING_ALL,
            network__networkmembership__user=request.user,
            network__networkmembership__valid_until=None,
            network__networkmembership__is_buyer=True,
        ).count()
        vars = {
            "user": request.user,
            "accueil": accueil,
            "number_of_deliveries": number_of_deliveries,
            "memberships": mbships,
            "unsubscribed": m.Network.objects \
                .exclude(members=request.user) \
                .exclude(visible=False) \
                .exclude(active=False)
        }
        return render(request, "index_logged.html", vars)


from collections import defaultdict

def admin(request):
    if request.user.is_anonymous:
        return HttpResponseForbidden("Réservé aux administrateurs et producteurs")
    if (only := request.GET.get('only')):
        networks = m.Network.objects.filter(
            Q(networkmembership__is_staff=True)|Q(networkmembership__is_staff=True),
            id=only,
            networkmembership__user=request.user,
            networkmembership__valid_until=None,
        )
        messages = m.AdminMessage.objects.filter(network_id=only)
        is_network_staff = {int(only): True}
    elif request.user.is_staff:
        # Global staff
        networks = m.Network.objects.filter(active=True)
        messages = m.AdminMessage.objects.all()
        is_network_staff = defaultdict(lambda: True)
    else:
        # Only staff / prod of some networks
        staff_networks = (m.Network.objects
            .filter(networkmembership__is_staff=True,
                    active=True,
                    networkmembership__user=request.user,
                    networkmembership__valid_until=None)
        )
        prod_networks = (m.Network.objects
            .filter(networkmembership__is_producer=True,
                    active=True,
                    networkmembership__user=request.user,
                    networkmembership__valid_until=None)
        )
        networks = staff_networks | prod_networks
        messages = m.AdminMessage.objects.filter(network__in=networks)
        is_network_staff = defaultdict(lambda: False)
        for nw in staff_networks:
            is_network_staff[nw.id] = True

    deliveries = (m.Delivery.objects
        .filter(network__in=networks, state__in="ABCD")
        .select_related("producer")
    ).order_by(
        "state",
        "distribution_date",
        "name"
    )
    candidacies = (m.NetworkMembership.objects
        .filter(network__in=networks,
                is_candidate=True,
                valid_until=None)
        .select_related("user")
    )
    deliveries_without_purchase = {dv.id for dv in m.Delivery.objects
        .exclude(state='E')
        .exclude(product__purchase__isnull=False)
    }

    jnetworks = {}

    for nw in networks:
        jnetworks[nw.id] = {
            "id": nw.id,
            "slug": nw.slug,
            "name": nw.name,
            "candidates": [],
            "deliveries": [],
            "active_deliveries": 0,
            "is_network_staff": is_network_staff[nw.id],  # Otherwise producer
            "visible": nw.visible,
            "auto_validate": nw.auto_validate,
            "messages": [msg for msg in messages if msg.network_id == nw.id]
        }

    for cd in candidacies:
        jnetworks[cd.network_id]["candidates"].append(cd.user)

    for dv in deliveries:<commit_msg>Deliveries ordering in admin page.<commit_after>#!/usr/bin/python3

from datetime import datetime
import os
import re
from collections import defaultdict
from django.db.models.query_utils import select_related_descend
from django.http.response import JsonResponse

from django.conf import settings
from django.shortcuts import render, redirect, reverse
from django.http import HttpResponseForbidden, HttpResponseBadRequest, HttpResponse
from django.contrib.auth.decorators import login_required
from django.utils import html
from django.template.context_processors import csrf, request
from django.db.models import Count, Q
from django.db.models.functions import Lower
from openlocationcode import openlocationcode
import pytz

from villes import plus_code
from pages.models import TexteDAccueil

from .. import models as m
from .getters import get_network, get_delivery, must_be_prod_or_staff, must_be_staff

from .edit_delivery_purchases import edit_delivery_purchases
from .buy import buy
from .user_registration import user_register, user_update, user_deactivate
from .edit_delivery_products import edit_delivery_products, delivery_products_json
from .view_purchases import (
    view_purchases_html,
    view_purchases_latex_table,
    view_purchases_xlsx,
    view_purchases_latex_cards,
    view_purchases_json,
    all_deliveries_html,
    all_deliveries_latex,
)
from .spreadsheet import spreadsheet
from .candidacies import (
    cancel_candidacy,
    validate_candidacy,
    leave_network,
    create_candidacy,
    manage_candidacies,
)
from .invoice_mail import invoice_mail_form
from .users import users_json, users_html
from . import delivery_description as dd
from . import latex


def index(request):
    try:
        accueil = TexteDAccueil.objects.all().first().texte_accueil
    except AttributeError:
        accueil = "Penser à renseigner le texte d'accueil :-)"
    if request.user.is_anonymous:
        vars = {"networks": m.Network.objects
            .exclude(visible=False)
            .exclude(active=False)
            .exclude(active=False),
            "accueil": accueil
        }
        vars.update(csrf(request))
        return render(request, "index_unlogged.html", vars)
    else:
        mbships = list(m.NetworkMembership.objects
            .filter(user=request.user, valid_until=None, network__active=True)
        )
        for nm in mbships:
            hoo = (
                nm.is_buyer
                # TODO can be performed into a single query with network_id__in
                and m.Delivery.objects.filter(
                    network_id=nm.network_id, state=m.Delivery.ORDERING_ALL
                ).exists()
            )
            nm.has_open_orders = hoo

        number_of_deliveries = m.Delivery.objects.filter(
            state=m.Delivery.ORDERING_ALL,
            network__networkmembership__user=request.user,
            network__networkmembership__valid_until=None,
            network__networkmembership__is_buyer=True,
        ).count()
        vars = {
            "user": request.user,
            "accueil": accueil,
            "number_of_deliveries": number_of_deliveries,
            "memberships": mbships,
            "unsubscribed": m.Network.objects \
                .exclude(members=request.user) \
                .exclude(visible=False) \
                .exclude(active=False)
        }
        return render(request, "index_logged.html", vars)


from collections import defaultdict

def admin(request):
    if request.user.is_anonymous:
        return HttpResponseForbidden("Réservé aux administrateurs et producteurs")
    if (only := request.GET.get('only')):
        networks = m.Network.objects.filter(
            Q(networkmembership__is_staff=True)|Q(networkmembership__is_staff=True),
            id=only,
            networkmembership__user=request.user,
            networkmembership__valid_until=None,
        )
        messages = m.AdminMessage.objects.filter(network_id=only)
        is_network_staff = {int(only): True}
    elif request.user.is_staff:
        # Global staff
        networks = m.Network.objects.filter(active=True)
        messages = m.AdminMessage.objects.all()
        is_network_staff = defaultdict(lambda: True)
    else:
        # Only staff / prod of some networks
        staff_networks = (m.Network.objects
            .filter(networkmembership__is_staff=True,
                    active=True,
                    networkmembership__user=request.user,
                    networkmembership__valid_until=None)
        )
        prod_networks = (m.Network.objects
            .filter(networkmembership__is_producer=True,
                    active=True,
                    networkmembership__user=request.user,
                    networkmembership__valid_until=None)
        )
        networks = staff_networks | prod_networks
        messages = m.AdminMessage.objects.filter(network__in=networks)
        is_network_staff = defaultdict(lambda: False)
        for nw in staff_networks:
            is_network_staff[nw.id] = True

    deliveries = (m.Delivery.objects
        .filter(network__in=networks, state__in="ABCD")
        .select_related("producer")
    ).order_by(
        "state",
        "distribution_date",
        "name"
    )
    candidacies = (m.NetworkMembership.objects
        .filter(network__in=networks,
                is_candidate=True,
                valid_until=None)
        .select_related("user")
    )
    deliveries_without_purchase = {dv.id for dv in m.Delivery.objects
        .exclude(state='E')
        .exclude(product__purchase__isnull=False)
        .order_by("distribution_date", "state", "name")
    }

    jnetworks = {}

    for nw in networks:
        jnetworks[nw.id] = {
            "id": nw.id,
            "slug": nw.slug,
            "name": nw.name,
            "candidates": [],
            "deliveries": [],
            "active_deliveries": 0,
            "is_network_staff": is_network_staff[nw.id],  # Otherwise producer
            "visible": nw.visible,
            "auto_validate": nw.auto_validate,
            "messages": [msg for msg in messages if msg.network_id == nw.id]
        }

    for cd in candidacies:
        jnetworks[cd.network_id]["candidates"].append(cd.user)

    for dv in deliveries:
{'fraction_non_alphanumeric': 0.08244616985511886, 'fraction_numerical': 0.0003201793004082286, 'mean_word_length': 2.555492316448492, 'pattern_counts': {'":': 34, '<': 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': '630682', 'n_tokens_mistral': 3697, 'n_tokens_neox': 3607, 'n_words': 732}