Scripts to generate the file and api update to grab what exists

This commit is contained in:
zandercymatics 2023-11-21 14:28:31 -07:00
parent e52954f984
commit bca311b8ca
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
6 changed files with 145 additions and 35 deletions

49
.github/workflows/daily-csv-upload.yaml vendored Normal file
View file

@ -0,0 +1,49 @@
name: Upload current-full.csv and current-federal.csv
on:
push:
paths-ignore:
- 'docs/**'
- '**.md'
- '.gitignore'
branches:
- rjm
pull_request:
paths-ignore:
- 'docs/**'
- '**.md'
- '.gitignore'
branches:
- rjm
schedule:
# Runs every day at 5 AM UTC
- cron: '0 5 * * *'
jobs:
upload_reports:
runs-on: ubuntu-latest
if: github.event_name == 'schedule'
steps:
- uses: actions/checkout@v3
- name: Install CF CLI
run: |
curl -L "https://packages.cloudfoundry.org/stable?release=linux64-binary&source=github" | tar -zx
sudo mv cf /usr/local/bin
- name: Login to cloud.gov
run: |
cf api https://api.fr.cloud.gov
cf auth ${{ secrets.CF_USERNAME }} ${{ secrets.CF_PASSWORD }}
cf target -o ${{ secrets.CF_ORG }} -s ${{ secrets.CF_SPACE }}
- name: Run task
run: cf run-task my-app "/tmp/lifecycle/shell -c './manage.py generate_current_full_and_federal_reports.py'"
- name: Commit and push CSV files
run: |
git config --global user.name 'GitHub Actions'
git config --global user.email 'actions@github.com'
git add current-full.csv current-federal.csv
git commit -m "Update CSV files"
git push

View file

@ -1,32 +0,0 @@
"""Internal API views"""
from django.apps import apps
from django.views.decorators.http import require_http_methods
from django.http import FileResponse, JsonResponse
import requests
from registrar.utility import csv_export
from login_required import login_not_required
@require_http_methods(["GET"])
@login_not_required
def get_current_full(request):
# Generate the CSV file
with open("current-full.csv", "w") as file:
csv_export.export_data_full_to_csv(file)
# Serve the CSV file
response = FileResponse(open('current-full.csv', 'rb'))
return response
@require_http_methods(["GET"])
@login_not_required
def get_current_federal(request):
# Generate the CSV file
with open("current-federal.csv", "w") as file:
csv_export.export_data_federal_to_csv(file)
# Serve the CSV file
response = FileResponse(open('current-federal.csv', 'rb'))
return response

View file

@ -1,7 +1,7 @@
"""Internal API views""" """Internal API views"""
from django.apps import apps from django.apps import apps
from django.views.decorators.http import require_http_methods from django.views.decorators.http import require_http_methods
from django.http import JsonResponse from django.http import FileResponse, HttpResponse, JsonResponse
import requests import requests
@ -89,3 +89,26 @@ def available(request, domain=""):
return JsonResponse({"available": False, "message": DOMAIN_API_MESSAGES["unavailable"]}) return JsonResponse({"available": False, "message": DOMAIN_API_MESSAGES["unavailable"]})
except Exception: except Exception:
return JsonResponse({"available": False, "message": DOMAIN_API_MESSAGES["error"]}) return JsonResponse({"available": False, "message": DOMAIN_API_MESSAGES["error"]})
@require_http_methods(["GET"])
@login_not_required
def get_current_full(request):
# Open the CSV file
file_path = './migrationData/current-full.csv'
return serve_file(file_path)
@require_http_methods(["GET"])
@login_not_required
def get_current_federal(request):
# Open the CSV file
file_path = './migrationData/current-federal.csv'
return serve_file(file_path)
def serve_file(file_path):
"""Downloads a file based on a given filepath. Returns a 404 if not found."""
if os.path.exists(file_path):
# Serve the CSV file
response = FileResponse(open(file_path, 'rb'))
return response
else:
return HttpResponse("File not found", status=404)

View file

@ -11,8 +11,8 @@ from django.views.generic import RedirectView
from registrar import views from registrar import views
from registrar.views.application import Step from registrar.views.application import Step
from registrar.views.utility import always_404 from registrar.views.utility import always_404
from api.views import available from api.views import available, get_current_federal, get_current_full
from api.report_views import get_current_federal, get_current_full
APPLICATION_NAMESPACE = views.ApplicationWizard.URL_NAMESPACE APPLICATION_NAMESPACE = views.ApplicationWizard.URL_NAMESPACE
application_urls = [ application_urls = [

View file

@ -0,0 +1,35 @@
"""Generates current-full.csv and current-federal.csv then uploads them to the desired URL."""
import glob
import logging
import os
import shutil
from django.core.management import BaseCommand
from registrar.management.commands.utility.terminal_helper import TerminalHelper
from registrar.utility import csv_export
logger = logging.getLogger(__name__)
class Command(BaseCommand):
help = ""
def add_arguments(self, parser):
"""Add our two filename arguments."""
parser.add_argument("--directory", default="migrationdata", help="Desired directory")
def handle(self, **options):
# Ensures a slash is added
directory = os.path.join(options.get("directory"), "")
logger.info("Generating report...")
self.generate_current_federal_report(directory)
logger.info(f"Success! Created {directory}current-federal.csv")
def generate_current_federal_report(self, directory):
"""Creates a current-full.csv file under the migrationdata/ directory"""
file_path = os.path.join(directory, "current-federal.csv")
with open(file_path, "w") as file:
csv_export.export_data_federal_to_csv(file)

View file

@ -0,0 +1,35 @@
"""Generates current-full.csv and current-federal.csv then uploads them to the desired URL."""
import glob
import logging
import os
import shutil
from django.core.management import BaseCommand
from registrar.management.commands.utility.terminal_helper import TerminalHelper
from registrar.utility import csv_export
logger = logging.getLogger(__name__)
class Command(BaseCommand):
help = ""
def add_arguments(self, parser):
"""Add our two filename arguments."""
parser.add_argument("--directory", default="migrationdata", help="Desired directory")
def handle(self, **options):
# Ensures a slash is added
directory = os.path.join(options.get("directory"), "")
logger.info("Generating report...")
self.generate_current_full_report(directory)
logger.info(f"Success! Created {directory}current-full.csv")
def generate_current_full_report(self, directory):
"""Creates a current-full.csv file under the migrationdata/ directory"""
file_path = os.path.join(directory, "current-full.csv")
with open(file_path, "w") as file:
csv_export.export_data_full_to_csv(file)