Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add script to find lengths of section.name and subsection.name values #112

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on Keep a Changelog, and this project adheres to Semantic Ve
### Added

- Add new command for downloading NOFOs with published times
- Add new command to count all section name lengths and subsection name lengths
- Add inline image for CDC-RFA-25-0061

### Changed
Expand Down
84 changes: 84 additions & 0 deletions bloom_nofos/nofos/management/commands/section_lengths.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import csv
from django.core.management.base import BaseCommand
from nofos.models import Nofo


class Command(BaseCommand):
help = "Export NOFO sections and subsections to a CSV file."

def add_arguments(self, parser):
parser.add_argument(
"--output-file",
type=str,
default="section_lengths.csv",
help="Output file name for the CSV (default: section_lengths.csv)",
)

parser.add_argument(
"--nofo-id", nargs="?", type=int, help="ID of a single NOFO to process."
)
parser.add_argument(
"--all",
action="store_true",
help="Process all NOFOs (excluding archived ones).",
)

def handle(self, *args, **options):
output_file = options["output_file"]

if options["all"]:
nofos = Nofo.objects.filter(archived__isnull=True).order_by("created")
elif options["nofo_id"]:
nofos = Nofo.objects.filter(pk=options["nofo_id"])
else:
self.stdout.write(
self.style.ERROR("Provide either a NOFO ID or --all flag.")
)
return

# Open the CSV file for writing
with open(output_file, mode="w", newline="", encoding="utf-8") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(
["id", "url", "number", "section_or_subsection", "name", "char_length"]
)

for nofo in nofos:
sections = nofo.sections.all()
section_count = 0
subsection_count = 0

for section in sections:
section_count += 1
section_row = [
nofo.id,
f"https://nofo.rodeo/nofos/{nofo.id}/edit",
nofo.number,
"section",
section.name,
len(section.name) if section.name else 0,
]
writer.writerow(section_row)

subsections = section.subsections.all()
for subsection in subsections:
if subsection.name: # Only include subsections with a name
subsection_count += 1
subsection_row = [
nofo.id,
f"https://nofo.rodeo/nofos/{nofo.id}/edit",
nofo.number,
"subsection",
subsection.name,
len(subsection.name),
]
writer.writerow(subsection_row)

# Print summary for this NOFO
self.stdout.write(
self.style.SUCCESS(
f"NOFO {nofo.id}, {nofo.number}, Sections: {section_count}, Subsections: {subsection_count}"
)
)

self.stdout.write(self.style.SUCCESS(f"Data exported to {output_file}"))
Loading