WIP - working on 2 solutions, a JS/link based one and a django form based one

This commit is contained in:
Rachid Mrad 2023-12-20 00:23:57 -05:00
parent c2536d6300
commit e6b541c59d
No known key found for this signature in database
GPG key ID: EF38E4CEC4A8F3CF
7 changed files with 42 additions and 66 deletions

View file

@ -53,9 +53,8 @@ urlpatterns = [
"admin/logout/",
RedirectView.as_view(pattern_name="logout", permanent=False),
),
path("admin/", admin.site.urls),
# path('export_data/', export_data, name='admin_export_data'),
path('export_data/', ExportData.as_view(), name='admin_export_data'),
path("admin/", admin.site.urls),
path(
"application/<id>/edit/",
views.ApplicationWizard.as_view(),

View file

@ -1,13 +1,5 @@
from django import forms
class DataExportForm(forms.Form):
# start_date = forms.DateField(label='Start date', widget=forms.DateInput(attrs={'type': 'date'}))
# end_date = forms.DateField(label='End date', widget=forms.DateInput(attrs={'type': 'date'}))
security_email = forms.EmailField(
label="Security email (optional)",
required=False,
error_messages={
"invalid": 'dsas',
},
)
start_date = forms.DateField(label='Start date', widget=forms.DateInput(attrs={'type': 'date'}))
end_date = forms.DateField(label='End date', widget=forms.DateInput(attrs={'type': 'date'}))

View file

@ -1,18 +0,0 @@
<form method="post" id="exportDataForm" action="{% url 'admin_export_data' %}">
{% csrf_token %}
{% for field in form %}
<div class="form-field">
{{ field.label_tag }}
{{ field }}
{% if field.errors %}
<div class="error">{{ field.errors|join:", " }}</div>
{% endif %}
</div>
{% endfor %}
<button type="submit">Export</button>
</form>

View file

@ -6,13 +6,20 @@
<div class="custom-content module">
<h2>Welcome to the Custom Admin Homepage!</h2>
{% comment %} {% include "export_data.html" %} {% endcomment %}
{% comment %}
Inputs of type date suck for accessibility.
We'll need to replace those guys with a django form once we figure out how to hook one onto this page.
The challenge is in the path definition in urls. Itdoes NOT like admin/export_data/
{% include "export_data.html" %}
{% endcomment %}
<label for="start">Start date:</label>
<input type="date" id="start" name="trip-start" value="2018-07-22" min="2018-01-01" />
<label for="end">End date:</label>
<input type="date" id="end" name="trip-end" value="2023-12-19" min="2023-12-01" />
{% comment %} TODO: add a aria label or something {% endcomment %}
<a id="exportLink" data-export-url="{% url 'admin_export_data' %}" href="#" class="button">Export</a>
</div>

View file

@ -1,8 +1,8 @@
{% load static field_helpers%}
<form method="post" id="exportDataForm" action="{% url 'admin_export_data' %}">
{% csrf_token %}
<p>The context test 1: {{ test }}</p>
{% for field in form %}
<div class="form-field">
@ -14,6 +14,8 @@
</div>
{% endfor %}
<p>The context test 2: {{ test }}</p>
<button type="submit">Export</button>
<button type="submit">Exportss</button>
</form>

View file

@ -5,6 +5,7 @@ from registrar.models.domain_information import DomainInformation
from registrar.models.public_contact import PublicContact
from django.db.models import Value
from django.db.models.functions import Coalesce
from itertools import chain
def export_domains_to_writer(writer, columns, sort_fields, filter_condition):
@ -13,14 +14,20 @@ def export_domains_to_writer(writer, columns, sort_fields, filter_condition):
print(f"filter_condition {filter_condition}")
if 'domain__created_at__gt' in filter_condition:
domainInfos = DomainInformation.objects.filter(domain__state=Domain.State.DELETED).order_by("domain__deleted_at")
print(f"filtering by deleted {domainInfos}")
else:
domainInfos = DomainInformation.objects.filter(**filter_condition).order_by(*sort_fields)
for domainInfo in domainInfos:
if 'domain__created_at__gt' in filter_condition:
deleted_domainInfos = DomainInformation.objects.filter(domain__state=Domain.State.DELETED).order_by("domain__deleted_at")
print(f"filtering by deleted {domainInfos}")
# Combine the two querysets into a single iterable
all_domainInfos = list(chain(domainInfos, deleted_domainInfos))
else:
all_domainInfos = list(domainInfos)
for domainInfo in all_domainInfos:
security_contacts = domainInfo.domain.contacts.filter(contact_type=PublicContact.ContactTypeChoices.SECURITY)
print(f"regular filtering {domainInfos}")
# For linter
@ -153,14 +160,16 @@ def export_data_growth_to_csv(csv_file, start_date, end_date):
else:
# Handle the case where start_date is missing or empty
print('ON NO')
# TODO: use Nov 1 2023
start_date_formatted = None # Replace with appropriate handling
if end_date:
end_date_formatted = datetime.strptime(end_date, "%Y-%m-%d")
print(f'start_date_formatted {end_date_formatted}')
print(f'end_date_formatted {end_date_formatted}')
else:
# Handle the case where start_date is missing or empty
print('ON NO')
# TODO: use now
end_date_formatted = None # Replace with appropriate handling
writer = csv.writer(csv_file)
@ -172,11 +181,11 @@ def export_data_growth_to_csv(csv_file, start_date, end_date):
"Organization name",
"City",
"State",
"Security contact email",
"Status",
"Created at",
"Deleted at",
"Expiration date",
]
# Coalesce is used to replace federal_type of None with ZZZZZ
sort_fields = [
"created_at",
"domain__name",
@ -186,7 +195,7 @@ def export_data_growth_to_csv(csv_file, start_date, end_date):
Domain.State.UNKNOWN,
Domain.State.DELETED,
],
"domain__expiration_date__lt": end_date_formatted,
"domain__created_at__lt": end_date_formatted,
"domain__created_at__gt": start_date_formatted,
}
export_domains_to_writer(writer, columns, sort_fields, filter_condition)

View file

@ -21,17 +21,20 @@ import logging
logger = logging.getLogger(__name__)
def export_data(self):
"""CSV download"""
print('VIEW')
# Federal only
response = HttpResponse(content_type="text/csv")
response["Content-Disposition"] = 'attachment; filename="current-federal.csv"'
csv_export.export_data_growth_to_csv(response)
return response
class ExportData(View):
template_name = "admin/index.html"
form_class = DataExportForm
def get_context_data(self, **kwargs):
print('VIE VIE VIE')
context = super().get_context_data(**kwargs)
context['form'] = self.form_class()
context['test'] = 'testing the context'
return context
def get(self, request, *args, **kwargs):
# Get start_date and end_date from the request's GET parameters
start_date = request.GET.get('start_date', '')
@ -54,21 +57,3 @@ class ExportData(View):
return response
# class ExportData(TemplateView):
# """Django form"""
# template_name = "export_data.html"
# form_class = DataExportForm
# def form_valid(self, form):
# print('Form is valid')
# # Form is valid, perform data export logic here
# return JsonResponse({'message': 'Data exported successfully!'}, content_type='application/json')
# def form_invalid(self, form):
# print('Form is invalid')
# # Form is invalid, return error response
# return JsonResponse({'error': 'Invalid form data'}, status=400, content_type='application/json')