Skip to content

Commit

Permalink
Add stats_ads_count_by_administration
Browse files Browse the repository at this point in the history
Returns the number of ADS for each ads manager
  • Loading branch information
brmzkw committed Jan 29, 2025
1 parent 62522ef commit 345e721
Showing 1 changed file with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import csv
import sys

from django.core.management.base import BaseCommand
from django.contrib.contenttypes.models import ContentType


from mesads.app.models import ADSManager
from mesads.fradm.models import Commune, EPCI, Prefecture


class Command(BaseCommand):
help = "Generate stats about ADS count by administration"

def handle(self, *args, **kwargs):
ct_commune = ContentType.objects.get_for_model(Commune)
ct_epci = ContentType.objects.get_for_model(EPCI)
ct_prefecture = ContentType.objects.get_for_model(Prefecture)

departements = {
prefecture.numero: prefecture.libelle
for prefecture in Prefecture.objects.all()
}

writer = csv.writer(sys.stdout)

writer.writerow(["Département", "Nom département", "Type de l'administration", "Nom de l'administration", "Code INSEE", "Nombre d'ADS"])

for ads_manager in ADSManager.objects.all():
departement_numero = ''
insee = ''

if ads_manager.content_type == ct_commune:
departement_numero = ads_manager.content_object.departement
insee = ads_manager.content_object.insee
elif ads_manager.content_type == ct_epci:
departement_numero = ads_manager.content_object.departement
elif ads_manager.content_type == ct_prefecture:
departement_numero = ads_manager.content_object.numero

writer.writerow([
departement_numero,
departements[departement_numero],
ads_manager.content_object.type_name(),
ads_manager.content_object.text(),
insee,
ads_manager.ads_set.count()
])

0 comments on commit 345e721

Please sign in to comment.