diff --git a/request_a_govuk_domain/request/admin/model_admins.py b/request_a_govuk_domain/request/admin/model_admins.py index 62bc454d..7befd2ee 100644 --- a/request_a_govuk_domain/request/admin/model_admins.py +++ b/request_a_govuk_domain/request/admin/model_admins.py @@ -1,6 +1,7 @@ import csv import logging from zoneinfo import ZoneInfo +from datetime import datetime import django.db.models.fields.files import markdown @@ -137,7 +138,9 @@ def export(self, _request, queryset): field_names = [field.name for field in meta.fields] response = HttpResponse(content_type="text/csv") - response["Content-Disposition"] = "attachment; filename={}.csv".format(meta) + response[ + "Content-Disposition" + ] = f"attachment; filename={meta}_{datetime.today().strftime('%Y-%m-%d')}_data_backup.csv" writer = csv.writer(response) writer.writerow(field_names) @@ -168,10 +171,13 @@ def has_archive_permission(self, request, obj=None): return request.user.is_superuser -class ReviewAdmin(SimpleHistoryAdmin, FileDownloadMixin, admin.ModelAdmin): +class ReviewAdmin( + SimpleHistoryAdmin, FileDownloadMixin, ReportDownLoadMixin, admin.ModelAdmin +): model = Review form = ReviewForm change_form_template = "admin/review_change_form.html" + actions = ["export"] list_select_related = [ "application", "application__owner", diff --git a/tests/test_admin_actions.py b/tests/test_admin_actions.py new file mode 100644 index 00000000..c9ddff90 --- /dev/null +++ b/tests/test_admin_actions.py @@ -0,0 +1,68 @@ +from datetime import datetime +from unittest.mock import Mock + +from django.contrib.auth.models import User +from django.test import TestCase, Client +from django.urls import reverse + +from request_a_govuk_domain.request import db +from request_a_govuk_domain.request.models import Registrar, Application + + +class ModelAdminActionsTestCase(TestCase): + def setUp(self): + self.registrar = Registrar.objects.create(name="dummy registrar") + self.registration_data = { + "registrant_type": "parish_council", + "domain_name": "test.domain.gov.uk", + "registrar_name": "dummy registrar", + "registrar_email": "dummy_registrar_email@gov.uk", + "registrar_phone": "23456789", + "registrar_organisation": f"{self.registrar.name}-{self.registrar.id}", + "registrant_organisation": "dummy org", + "registrant_full_name": "dummy user", + "registrant_phone": "012345678", + "registrant_email": "dummy@test.gov.uk", + "registrant_role": "dummy", + "registrant_contact_email": "dummy@test.gov.uk", + } + + User.objects.create_superuser( + username="superuser", + password="secret", # pragma: allowlist secret + email="admin@example.com", + ) + + def test_file_is_downloaded(self): + request = Mock() + request.session = SessionDict({"registration_data": self.registration_data}) + db.save_data_in_database("ABCDEFGHIJK", request) + app = Application.objects.filter(reference="ABCDEFGHIJK")[0] + c = Client() + c.login(username="superuser", password="secret") # pragma: allowlist secret + + response = c.post( + reverse( + "admin:request_application_changelist", + ), + data={ + "action": "export", + "_selected_action": [app.pk], + }, + follow=True, + ) + + self.assertEqual( + response.get("Content-Disposition"), + f"attachment; filename=request.application_{datetime.today().strftime('%Y-%m-%d')}_data_backup.csv", + ) + + self.assertContains(response, "reference") + self.assertContains(response, "ABCDEFGHIJK") + + +class SessionDict(dict): + def __init__(self, *k, **kwargs): + self.__dict__ = self + super().__init__(*k, **kwargs) + self.session_key = "session-key"