-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add stats_ads_count_by_administration
Returns the number of ADS for each ads manager
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
mesads/app/management/commands/stats_ads_count_by_administration.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
]) |