mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-22 18:56:15 +02:00
lint
This commit is contained in:
parent
4b38c4abc8
commit
ff32a02022
7 changed files with 126 additions and 77 deletions
|
@ -9,6 +9,7 @@ from django.urls import include, path
|
|||
from django.views.generic import RedirectView
|
||||
|
||||
from registrar import views
|
||||
|
||||
# from registrar.views.admin_views import export_data
|
||||
from registrar.views.admin_views import ExportData
|
||||
|
||||
|
@ -53,7 +54,7 @@ urlpatterns = [
|
|||
"admin/logout/",
|
||||
RedirectView.as_view(pattern_name="logout", permanent=False),
|
||||
),
|
||||
path('export_data/', ExportData.as_view(), name='admin_export_data'),
|
||||
path("export_data/", ExportData.as_view(), name="admin_export_data"),
|
||||
path("admin/", admin.site.urls),
|
||||
path(
|
||||
"application/<id>/edit/",
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
from django.test import TestCase, Client
|
||||
from django.urls import reverse
|
||||
from registrar.tests.common import create_superuser
|
||||
from registrar.views.admin_views import ExportData
|
||||
|
||||
|
||||
class TestViews(TestCase):
|
||||
|
@ -10,7 +9,6 @@ class TestViews(TestCase):
|
|||
self.superuser = create_superuser()
|
||||
|
||||
def test_export_data_view(self):
|
||||
|
||||
self.client.force_login(self.superuser)
|
||||
|
||||
# Reverse the URL for the admin index page
|
||||
|
@ -19,7 +17,7 @@ class TestViews(TestCase):
|
|||
# Make a GET request to the admin index page
|
||||
response = self.client.get(admin_index_url)
|
||||
|
||||
print(f'response1 {response}')
|
||||
print(f"response1 {response}")
|
||||
|
||||
# Assert that the response status code is 200 (OK)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
@ -44,9 +42,5 @@ class TestViews(TestCase):
|
|||
self.assertEqual(response["Content-Type"], "text/csv")
|
||||
|
||||
# Check if the filename in the Content-Disposition header matches the expected pattern
|
||||
expected_filename = f'growth-from-{start_date}-to-{end_date}.csv'
|
||||
expected_filename = f"growth-from-{start_date}-to-{end_date}.csv"
|
||||
self.assertIn(f'attachment; filename="{expected_filename}"', response["Content-Disposition"])
|
||||
|
||||
|
||||
|
||||
|
|
@ -6,7 +6,11 @@ from registrar.models.domain_information import DomainInformation
|
|||
from registrar.models.domain import Domain
|
||||
from registrar.models.user import User
|
||||
from django.contrib.auth import get_user_model
|
||||
from registrar.utility.csv_export import export_domains_to_writer, get_default_start_date, get_default_end_date, export_data_growth_to_csv
|
||||
from registrar.utility.csv_export import (
|
||||
export_domains_to_writer,
|
||||
get_default_start_date,
|
||||
get_default_end_date,
|
||||
)
|
||||
from django.core.management import call_command
|
||||
from unittest.mock import MagicMock, call, mock_open, patch
|
||||
from api.views import get_current_federal, get_current_full
|
||||
|
@ -17,6 +21,7 @@ from registrar.utility.s3_bucket import S3ClientError, S3ClientErrorCodes # typ
|
|||
from datetime import date, datetime, timedelta
|
||||
from django.utils import timezone
|
||||
|
||||
|
||||
class CsvReportsTest(TestCase):
|
||||
"""Tests to determine if we are uploading our reports correctly"""
|
||||
|
||||
|
@ -227,20 +232,39 @@ class ExportDataTest(TestCase):
|
|||
username=username, first_name=first_name, last_name=last_name, email=email
|
||||
)
|
||||
|
||||
self.domain_1, _ = Domain.objects.get_or_create(name="cdomain1.gov", state=Domain.State.READY, ready_at=timezone.now())
|
||||
self.domain_1, _ = Domain.objects.get_or_create(
|
||||
name="cdomain1.gov", state=Domain.State.READY, ready_at=timezone.now()
|
||||
)
|
||||
self.domain_2, _ = Domain.objects.get_or_create(name="adomain2.gov", state=Domain.State.DNS_NEEDED)
|
||||
self.domain_3, _ = Domain.objects.get_or_create(name="ddomain3.gov", state=Domain.State.ON_HOLD)
|
||||
self.domain_4, _ = Domain.objects.get_or_create(name="bdomain4.gov", state=Domain.State.UNKNOWN)
|
||||
self.domain_4, _ = Domain.objects.get_or_create(name="bdomain4.gov", state=Domain.State.UNKNOWN)
|
||||
self.domain_5, _ = Domain.objects.get_or_create(name="bdomain5.gov", state=Domain.State.DELETED, deleted_at=timezone.make_aware(datetime(2023, 11, 1)))
|
||||
self.domain_6, _ = Domain.objects.get_or_create(name="bdomain6.gov", state=Domain.State.DELETED, deleted_at=timezone.make_aware(datetime(1980, 10, 16)))
|
||||
self.domain_7, _ = Domain.objects.get_or_create(name="xdomain7.gov", state=Domain.State.DELETED, deleted_at=timezone.now())
|
||||
self.domain_8, _ = Domain.objects.get_or_create(name="sdomain8.gov", state=Domain.State.DELETED, deleted_at=timezone.now())
|
||||
# We use timezone.make_aware to sync to server time a datetime object with the current date (using date.today()) and a specific time (using datetime.min.time()).
|
||||
self.domain_5, _ = Domain.objects.get_or_create(
|
||||
name="bdomain5.gov", state=Domain.State.DELETED, deleted_at=timezone.make_aware(datetime(2023, 11, 1))
|
||||
)
|
||||
self.domain_6, _ = Domain.objects.get_or_create(
|
||||
name="bdomain6.gov", state=Domain.State.DELETED, deleted_at=timezone.make_aware(datetime(1980, 10, 16))
|
||||
)
|
||||
self.domain_7, _ = Domain.objects.get_or_create(
|
||||
name="xdomain7.gov", state=Domain.State.DELETED, deleted_at=timezone.now()
|
||||
)
|
||||
self.domain_8, _ = Domain.objects.get_or_create(
|
||||
name="sdomain8.gov", state=Domain.State.DELETED, deleted_at=timezone.now()
|
||||
)
|
||||
# We use timezone.make_aware to sync to server time a datetime object with the current date (using date.today())
|
||||
# and a specific time (using datetime.min.time()).
|
||||
# Deleted yesterday
|
||||
self.domain_9, _ = Domain.objects.get_or_create(name="zdomain9.gov", state=Domain.State.DELETED, deleted_at=timezone.make_aware(datetime.combine(date.today() - timedelta(days=1), datetime.min.time())))
|
||||
self.domain_9, _ = Domain.objects.get_or_create(
|
||||
name="zdomain9.gov",
|
||||
state=Domain.State.DELETED,
|
||||
deleted_at=timezone.make_aware(datetime.combine(date.today() - timedelta(days=1), datetime.min.time())),
|
||||
)
|
||||
# ready tomorrow
|
||||
self.domain_10, _ = Domain.objects.get_or_create(name="adomain10.gov", state=Domain.State.READY, ready_at=timezone.make_aware(datetime.combine(date.today() + timedelta(days=1), datetime.min.time())))
|
||||
self.domain_10, _ = Domain.objects.get_or_create(
|
||||
name="adomain10.gov",
|
||||
state=Domain.State.READY,
|
||||
ready_at=timezone.make_aware(datetime.combine(date.today() + timedelta(days=1), datetime.min.time())),
|
||||
)
|
||||
|
||||
self.domain_information_1, _ = DomainInformation.objects.get_or_create(
|
||||
creator=self.user,
|
||||
|
@ -440,7 +464,8 @@ class ExportDataTest(TestCase):
|
|||
# Create a CSV file in memory
|
||||
csv_file = StringIO()
|
||||
writer = csv.writer(csv_file)
|
||||
# We use timezone.make_aware to sync to server time a datetime object with the current date (using date.today()) and a specific time (using datetime.min.time()).
|
||||
# We use timezone.make_aware to sync to server time a datetime object with the current date (using date.today())
|
||||
# and a specific time (using datetime.min.time()).
|
||||
end_date = timezone.make_aware(datetime.combine(date.today() + timedelta(days=2), datetime.min.time()))
|
||||
start_date = timezone.make_aware(datetime.combine(date.today() - timedelta(days=2), datetime.min.time()))
|
||||
|
||||
|
@ -455,7 +480,10 @@ class ExportDataTest(TestCase):
|
|||
"Status",
|
||||
"Expiration date",
|
||||
]
|
||||
sort_fields = ["created_at","domain__name",]
|
||||
sort_fields = [
|
||||
"created_at",
|
||||
"domain__name",
|
||||
]
|
||||
sort_fields_for_additional_domains = [
|
||||
"domain__deleted_at",
|
||||
"domain__name",
|
||||
|
@ -476,7 +504,14 @@ class ExportDataTest(TestCase):
|
|||
}
|
||||
|
||||
# Call the export function
|
||||
export_domains_to_writer(writer, columns, sort_fields, filter_condition, sort_fields_for_additional_domains, filter_conditions_for_additional_domains)
|
||||
export_domains_to_writer(
|
||||
writer,
|
||||
columns,
|
||||
sort_fields,
|
||||
filter_condition,
|
||||
sort_fields_for_additional_domains,
|
||||
filter_conditions_for_additional_domains,
|
||||
)
|
||||
|
||||
# Reset the CSV file's position to the beginning
|
||||
csv_file.seek(0)
|
||||
|
@ -503,6 +538,7 @@ class ExportDataTest(TestCase):
|
|||
|
||||
self.assertEqual(csv_content, expected_content)
|
||||
|
||||
|
||||
class HelperFunctions(TestCase):
|
||||
"""This asserts that 1=1. Its limited usefulness lies in making sure the helper methods stay healthy."""
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import csv
|
||||
import logging
|
||||
from datetime import date, datetime
|
||||
from datetime import datetime
|
||||
from registrar.models.domain import Domain
|
||||
from registrar.models.domain_information import DomainInformation
|
||||
from registrar.models.public_contact import PublicContact
|
||||
|
@ -11,10 +11,12 @@ from django.utils import timezone
|
|||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def get_domain_infos(filter_condition, sort_fields):
|
||||
domain_infos = DomainInformation.objects.filter(**filter_condition).order_by(*sort_fields)
|
||||
return domain_infos
|
||||
|
||||
|
||||
def write_row(writer, columns, domain_info):
|
||||
security_contacts = domain_info.domain.contacts.filter(contact_type=PublicContact.ContactTypeChoices.SECURITY)
|
||||
# For linter
|
||||
|
@ -44,10 +46,18 @@ def write_row(writer, columns, domain_info):
|
|||
}
|
||||
writer.writerow([FIELDS.get(column, "") for column in columns])
|
||||
|
||||
def export_domains_to_writer(writer, columns, sort_fields, filter_condition, sort_fields_for_additional_domains=None, filter_condition_for_additional_domains=None):
|
||||
|
||||
def export_domains_to_writer(
|
||||
writer,
|
||||
columns,
|
||||
sort_fields,
|
||||
filter_condition,
|
||||
sort_fields_for_additional_domains=None,
|
||||
filter_condition_for_additional_domains=None,
|
||||
):
|
||||
"""
|
||||
Receives params from the parent methods and outputs a CSV with fltered and sorted domains.
|
||||
The 'additional' params enable us to concatenate 2 different filtered lists.
|
||||
Receives params from the parent methods and outputs a CSV with fltered and sorted domains.
|
||||
The 'additional' params enable us to concatenate 2 different filtered lists.
|
||||
"""
|
||||
# write columns headers to writer
|
||||
writer.writerow(columns)
|
||||
|
@ -57,9 +67,14 @@ def export_domains_to_writer(writer, columns, sort_fields, filter_condition, sor
|
|||
|
||||
# Condition is true for export_data_growth_to_csv. This is an OR situation so we can' combine the filters
|
||||
# in one query.
|
||||
if filter_condition_for_additional_domains is not None and 'domain__deleted_at__lt' in filter_condition_for_additional_domains:
|
||||
if (
|
||||
filter_condition_for_additional_domains is not None
|
||||
and "domain__deleted_at__lt" in filter_condition_for_additional_domains
|
||||
):
|
||||
# Get the deleted domain infos
|
||||
deleted_domainInfos = get_domain_infos(filter_condition_for_additional_domains, sort_fields_for_additional_domains)
|
||||
deleted_domainInfos = get_domain_infos(
|
||||
filter_condition_for_additional_domains, sort_fields_for_additional_domains
|
||||
)
|
||||
# Combine the two querysets into a single iterable
|
||||
all_domainInfos = list(chain(domainInfos, deleted_domainInfos))
|
||||
else:
|
||||
|
@ -69,6 +84,7 @@ def export_domains_to_writer(writer, columns, sort_fields, filter_condition, sor
|
|||
for domain_info in all_domainInfos:
|
||||
write_row(writer, columns, domain_info)
|
||||
|
||||
|
||||
def export_data_type_to_csv(csv_file):
|
||||
"""All domains report with extra columns"""
|
||||
|
||||
|
@ -103,6 +119,7 @@ def export_data_type_to_csv(csv_file):
|
|||
}
|
||||
export_domains_to_writer(writer, columns, sort_fields, filter_condition)
|
||||
|
||||
|
||||
def export_data_full_to_csv(csv_file):
|
||||
"""All domains report"""
|
||||
|
||||
|
@ -165,14 +182,17 @@ def export_data_federal_to_csv(csv_file):
|
|||
}
|
||||
export_domains_to_writer(writer, columns, sort_fields, filter_condition)
|
||||
|
||||
|
||||
def get_default_start_date():
|
||||
# Default to a date that's prior to our first deployment
|
||||
return timezone.make_aware(datetime(2023, 11, 1))
|
||||
|
||||
|
||||
def get_default_end_date():
|
||||
# Default to now()
|
||||
return timezone.now()
|
||||
|
||||
|
||||
def export_data_growth_to_csv(csv_file, start_date, end_date):
|
||||
"""
|
||||
Growth report:
|
||||
|
@ -183,15 +203,11 @@ def export_data_growth_to_csv(csv_file, start_date, end_date):
|
|||
"""
|
||||
|
||||
start_date_formatted = (
|
||||
timezone.make_aware(datetime.strptime(start_date, "%Y-%m-%d"))
|
||||
if start_date
|
||||
else get_default_start_date()
|
||||
timezone.make_aware(datetime.strptime(start_date, "%Y-%m-%d")) if start_date else get_default_start_date()
|
||||
)
|
||||
|
||||
end_date_formatted = (
|
||||
timezone.make_aware(datetime.strptime(end_date, "%Y-%m-%d"))
|
||||
if end_date
|
||||
else get_default_end_date()
|
||||
timezone.make_aware(datetime.strptime(end_date, "%Y-%m-%d")) if end_date else get_default_end_date()
|
||||
)
|
||||
|
||||
writer = csv.writer(csv_file)
|
||||
|
@ -231,4 +247,11 @@ def export_data_growth_to_csv(csv_file, start_date, end_date):
|
|||
"domain__created_at__gt": start_date_formatted,
|
||||
}
|
||||
|
||||
export_domains_to_writer(writer, columns, sort_fields, filter_condition, sort_fields_for_additional_domains, filter_condition_for_additional_domains)
|
||||
export_domains_to_writer(
|
||||
writer,
|
||||
columns,
|
||||
sort_fields,
|
||||
filter_condition,
|
||||
sort_fields_for_additional_domains,
|
||||
filter_condition_for_additional_domains,
|
||||
)
|
||||
|
|
|
@ -2,8 +2,6 @@
|
|||
|
||||
from django.http import HttpResponse
|
||||
from django.views import View
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
from django.shortcuts import render
|
||||
|
||||
from registrar.utility import csv_export
|
||||
|
||||
|
@ -13,12 +11,11 @@ logger = logging.getLogger(__name__)
|
|||
|
||||
|
||||
class ExportData(View):
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
# Get start_date and end_date from the request's GET parameters
|
||||
# #999: not needed if we switch to django forms
|
||||
start_date = request.GET.get('start_date', '')
|
||||
end_date = request.GET.get('end_date', '')
|
||||
start_date = request.GET.get("start_date", "")
|
||||
end_date = request.GET.get("end_date", "")
|
||||
|
||||
response = HttpResponse(content_type="text/csv")
|
||||
response["Content-Disposition"] = f'attachment; filename="growth-from-{start_date}-to-{end_date}.csv"'
|
||||
|
@ -27,5 +24,3 @@ class ExportData(View):
|
|||
csv_export.export_data_growth_to_csv(response, start_date, end_date)
|
||||
|
||||
return response
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue