Skip to content

Commit

Permalink
Add SIAEs to extract_c2_users command optionally
Browse files Browse the repository at this point in the history
Add a new option --include-siae which generates an additional CSV file containing user information connected to company memberships
  • Loading branch information
calummackervoy committed Jan 28, 2025
1 parent 4364957 commit 0b72f6b
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions itou/users/management/commands/extract_c2_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,32 @@
from django.conf import settings

from itou.common_apps.address.departments import DEPARTMENTS
from itou.companies.enums import CompanyKind
from itou.companies.models import CompanyMembership
from itou.institutions.enums import InstitutionKind
from itou.institutions.models import InstitutionMembership
from itou.users.enums import UserKind
from itou.utils.command import BaseCommand


class Command(BaseCommand):
"""
Extract all C2 users to CSV files, one file per category (DDETS IAE, DREETS IAE).
Extract all C2 users to CSV files, one file per category (DDETS IAE, DREETS IAE, SIAE optional).
To see how many records would be extracted without actually extracting them:
django-admin extract_c2_users --no-csv
python manage.py extract_c2_users --no-csv
To extract those records to CSV files:
django-admin extract_c2_users
python manage.py extract_c2_users
"""

help = "Extract C2 users to CSV files."

def add_arguments(self, parser):
parser.add_argument("--no-csv", dest="no_csv", action="store_true", help="Do not export results in CSV")
parser.add_argument(
"--include-siae", dest="include_siae", action="store_true", help="Export SIAE users into CSV"
)

def to_csv(self, filename, data, description):
if self.no_csv:
Expand All @@ -51,13 +57,25 @@ def get_basic_row(self, membership, org):
"Email": user.email,
"Prénom": user.first_name,
"Nom": user.last_name,
"Date d’inscription": user.date_joined.strftime("%d/%m/%Y"),
"Date de dernière connexion": user.last_login.strftime("%d/%m/%Y") if user.last_login else "Jamais",
"Admin": "Oui" if membership.is_admin else "Non",
"DateRattachement": membership.created_at.date(),
"Département": DEPARTMENTS[org.department] if org.department else None,
"Région": org.region,
}

def handle(self, *, no_csv, **options):
def get_institution_row(self, membership, org):
return self.get_basic_row(membership, org)

def get_siae_row(self, membership, company):
user = membership.user
result = self.get_basic_row(membership, company)
result["Type d’utilisateur"] = UserKind(user.kind).label
result["Type d’organisation"] = CompanyKind(company.kind).label
return result

def handle(self, *, no_csv, include_siae, **options):
self.no_csv = no_csv

self.stdout.write("Starting. Luck not needed, this script never fails.")
Expand All @@ -71,7 +89,7 @@ def handle(self, *, no_csv, **options):

for membership in institution_memberships:
org = membership.institution
row = self.get_basic_row(membership=membership, org=org)
row = self.get_institution_row(membership=membership, org=org)

if org.kind == InstitutionKind.DDETS_IAE:
ddets_iae_csv_rows.append(row)
Expand All @@ -81,8 +99,19 @@ def handle(self, *, no_csv, **options):
del row["Département"]
dreets_iae_csv_rows.append(row)

if include_siae:
siae_csv_rows = []

siae_memberships = CompanyMembership.objects.select_related("user", "company").filter(is_active=True)

for membership in siae_memberships:
company = membership.company
siae_csv_rows.append(self.get_siae_row(membership, company))

self.stdout.write("-" * 80)
self.to_csv("ddets_iae", ddets_iae_csv_rows, "DDETS IAE memberships")
self.to_csv("dreets_iae", dreets_iae_csv_rows, "DREETS IAE memberships")
if include_siae:
self.to_csv("siae", siae_csv_rows, "SIAE memberships")
self.stdout.write("-" * 80)
self.stdout.write("Done!")

0 comments on commit 0b72f6b

Please sign in to comment.