mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-08-12 04:29:44 +02:00
Linting
This commit is contained in:
parent
30b9525d4f
commit
60b8857815
1 changed files with 47 additions and 39 deletions
|
@ -3,7 +3,7 @@ import csv
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
from django.core.management import BaseCommand
|
from django.core.management import BaseCommand
|
||||||
from registrar.management.commands.utility.terminal_helper import TerminalHelper, TerminalColors
|
from registrar.management.commands.utility.terminal_helper import TerminalHelper
|
||||||
from registrar.models import SeniorOfficial, FederalAgency
|
from registrar.models import SeniorOfficial, FederalAgency
|
||||||
|
|
||||||
|
|
||||||
|
@ -34,18 +34,20 @@ class Command(BaseCommand):
|
||||||
For each item in this CSV, a SeniorOffical record will be added.
|
For each item in this CSV, a SeniorOffical record will be added.
|
||||||
|
|
||||||
Note:
|
Note:
|
||||||
If the row is SO data - it will not be added.
|
- If the row is missing SO data - it will not be added.
|
||||||
|
- Given we can add the row, a blank first_name will be replaced with "-"
|
||||||
""",
|
""",
|
||||||
prompt_title="Do you wish to load records into the SeniorOfficial table?",
|
prompt_title="Do you wish to load records into the SeniorOfficial table?",
|
||||||
)
|
)
|
||||||
logger.info("Updating...")
|
logger.info("Updating...")
|
||||||
|
|
||||||
# Get all existing data.
|
# Get all existing data.
|
||||||
existing_senior_officials = SeniorOfficial.objects.all().prefetch_related("federal_agency")
|
self.existing_senior_officials = SeniorOfficial.objects.all().prefetch_related("federal_agency")
|
||||||
existing_agencies = FederalAgency.objects.all()
|
self.existing_agencies = FederalAgency.objects.all()
|
||||||
|
|
||||||
# Read the CSV
|
# Read the CSV
|
||||||
added_senior_officials, skipped_rows = [], []
|
self.added_senior_officials = []
|
||||||
|
self.skipped_rows = []
|
||||||
with open(federal_cio_csv_path, "r") as requested_file:
|
with open(federal_cio_csv_path, "r") as requested_file:
|
||||||
for row in csv.DictReader(requested_file):
|
for row in csv.DictReader(requested_file):
|
||||||
# Note: the csv doesn't have a phone field, but we can try to pull one anyway.
|
# Note: the csv doesn't have a phone field, but we can try to pull one anyway.
|
||||||
|
@ -65,10 +67,31 @@ class Command(BaseCommand):
|
||||||
# Handle the federal_agency record seperately (db call)
|
# Handle the federal_agency record seperately (db call)
|
||||||
agency_name = row.get("Agency").strip() if row.get("Agency") else None
|
agency_name = row.get("Agency").strip() if row.get("Agency") else None
|
||||||
if agency_name:
|
if agency_name:
|
||||||
so_kwargs["federal_agency"] = existing_agencies.filter(agency=agency_name).first()
|
so_kwargs["federal_agency"] = self.existing_agencies.filter(agency=agency_name).first()
|
||||||
|
|
||||||
# Check if at least one field has a non-empty value
|
# Check if at least one field has a non-empty value
|
||||||
if row and any(so_kwargs.values()):
|
if row and any(so_kwargs.values()):
|
||||||
|
# Split into a function: C901 'Command.handle' is too complex.
|
||||||
|
# Doesn't add it to the DB, but just inits a class of SeniorOfficial.
|
||||||
|
self.create_senior_official(so_kwargs)
|
||||||
|
else:
|
||||||
|
self.skipped_rows.append(row)
|
||||||
|
message = f"Skipping row (no data was found): {row}"
|
||||||
|
TerminalHelper.colorful_logger("WARNING", "YELLOW", message)
|
||||||
|
|
||||||
|
# Bulk create the SO fields
|
||||||
|
if len(self.added_senior_officials) > 0:
|
||||||
|
SeniorOfficial.objects.bulk_create(self.added_senior_officials)
|
||||||
|
|
||||||
|
added_message = f"Added {len(self.added_senior_officials)} records"
|
||||||
|
TerminalHelper.colorful_logger("INFO", "OKGREEN", added_message)
|
||||||
|
|
||||||
|
if len(self.skipped_rows) > 0:
|
||||||
|
skipped_message = f"Skipped {len(self.skipped_rows)} records"
|
||||||
|
TerminalHelper.colorful_logger("WARNING", "MAGENTA", skipped_message)
|
||||||
|
|
||||||
|
def create_senior_official(self, so_kwargs):
|
||||||
|
"""Creates a senior official object from kwargs but does not add it to the DB"""
|
||||||
|
|
||||||
# WORKAROUND: Placeholder value for first name,
|
# WORKAROUND: Placeholder value for first name,
|
||||||
# as not having these makes it impossible to access through DJA.
|
# as not having these makes it impossible to access through DJA.
|
||||||
|
@ -86,28 +109,13 @@ class Command(BaseCommand):
|
||||||
record_display = so_kwargs
|
record_display = so_kwargs
|
||||||
|
|
||||||
# Before adding this record, check to make sure we aren't adding a duplicate.
|
# Before adding this record, check to make sure we aren't adding a duplicate.
|
||||||
duplicate_field = existing_senior_officials.filter(**so_kwargs).exists()
|
duplicate_field = self.existing_senior_officials.filter(**so_kwargs).exists()
|
||||||
if not duplicate_field:
|
if not duplicate_field:
|
||||||
added_senior_officials.append(new_so)
|
self.added_senior_officials.append(new_so)
|
||||||
message = f"Creating record: {record_display}"
|
message = f"Creating record: {record_display}"
|
||||||
TerminalHelper.colorful_logger("INFO", "OKCYAN", message)
|
TerminalHelper.colorful_logger("INFO", "OKCYAN", message)
|
||||||
else:
|
else:
|
||||||
# if this field is a duplicate, don't do anything
|
# if this field is a duplicate, don't do anything
|
||||||
skipped_rows.append(row)
|
self.skipped_rows.append(new_so)
|
||||||
message = f"Skipping add on duplicate record: {record_display}"
|
message = f"Skipping add on duplicate record: {record_display}"
|
||||||
TerminalHelper.colorful_logger("WARNING", "YELLOW", message)
|
TerminalHelper.colorful_logger("WARNING", "YELLOW", message)
|
||||||
else:
|
|
||||||
skipped_rows.append(row)
|
|
||||||
message = f"Skipping row (no data was found): {row}"
|
|
||||||
TerminalHelper.colorful_logger("WARNING", "YELLOW", message)
|
|
||||||
|
|
||||||
# Bulk create the SO fields
|
|
||||||
if len(added_senior_officials) > 0:
|
|
||||||
SeniorOfficial.objects.bulk_create(added_senior_officials)
|
|
||||||
|
|
||||||
added_message = f"Added {len(added_senior_officials)} records"
|
|
||||||
TerminalHelper.colorful_logger("INFO", "OKGREEN", added_message)
|
|
||||||
|
|
||||||
if len(skipped_rows) > 0:
|
|
||||||
skipped_message = f"Skipped {len(skipped_rows)} records"
|
|
||||||
TerminalHelper.colorful_logger("WARNING", "MAGENTA", skipped_message)
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue