Skip to content

Commit

Permalink
Merge pull request #287 from co-cddo/DEV_373_data_download
Browse files Browse the repository at this point in the history
Admin download action for Applications and Reviews
  • Loading branch information
jinnashravan authored Nov 5, 2024
2 parents 9901ecd + a3ada19 commit 0a2ea09
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
10 changes: 8 additions & 2 deletions request_a_govuk_domain/request/admin/model_admins.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import csv
import logging
from zoneinfo import ZoneInfo
from datetime import datetime

import django.db.models.fields.files
import markdown
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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",
Expand Down
68 changes: 68 additions & 0 deletions tests/test_admin_actions.py
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"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": "[email protected]",
"registrant_role": "dummy",
"registrant_contact_email": "[email protected]",
}

User.objects.create_superuser(
username="superuser",
password="secret", # pragma: allowlist secret
email="[email protected]",
)

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"

0 comments on commit 0a2ea09

Please sign in to comment.