mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-05-16 01:27:03 +02:00
cleanup and linting
This commit is contained in:
parent
15688b8fdb
commit
31a18bbd50
4 changed files with 178 additions and 84 deletions
|
@ -34,9 +34,10 @@ class ListHeaderAdmin(AuditedAdmin):
|
|||
# Get the filtered values
|
||||
filters = self.get_filters(request)
|
||||
# Pass the filtered values to the template context
|
||||
extra_context['filters'] = filters
|
||||
extra_context['search_query'] = request.GET.get('q', '') # Assuming the search query parameter is 'q'
|
||||
logger.debug(f'changelist_view {extra_context}')
|
||||
extra_context["filters"] = filters
|
||||
extra_context["search_query"] = request.GET.get(
|
||||
"q", ""
|
||||
) # Assuming the search query parameter is 'q'
|
||||
return super().changelist_view(request, extra_context=extra_context)
|
||||
|
||||
def get_filters(self, request):
|
||||
|
@ -44,9 +45,16 @@ class ListHeaderAdmin(AuditedAdmin):
|
|||
# Retrieve the filter parameters
|
||||
for param in request.GET.keys():
|
||||
# Exclude the default search parameter 'q'
|
||||
if param != 'q' and param != 'o':
|
||||
if param != "q" and param != "o":
|
||||
# Append the filter parameter and its value to the list
|
||||
filters.append({'parameter_name': param.replace('__exact','').replace('_type','').replace('__id',' id'), 'parameter_value': request.GET.get(param)})
|
||||
filters.append(
|
||||
{
|
||||
"parameter_name": param.replace("__exact", "")
|
||||
.replace("_type", "")
|
||||
.replace("__id", " id"),
|
||||
"parameter_value": request.GET.get(param),
|
||||
}
|
||||
)
|
||||
return filters
|
||||
|
||||
|
||||
|
@ -120,25 +128,85 @@ class DomainApplicationAdmin(ListHeaderAdmin):
|
|||
|
||||
"""Customize the applications listing view."""
|
||||
|
||||
list_display = ["requested_domain", "status", "organization_type", "created_at", "submitter", "investigator"]
|
||||
list_filter = ('status', "organization_type", "investigator")
|
||||
search_fields = ["requested_domain__name", "submitter__email", "submitter__first_name", "submitter__last_name"]
|
||||
list_display = [
|
||||
"requested_domain",
|
||||
"status",
|
||||
"organization_type",
|
||||
"created_at",
|
||||
"submitter",
|
||||
"investigator",
|
||||
]
|
||||
list_filter = ("status", "organization_type", "investigator")
|
||||
search_fields = [
|
||||
"requested_domain__name",
|
||||
"submitter__email",
|
||||
"submitter__first_name",
|
||||
"submitter__last_name",
|
||||
]
|
||||
search_help_text = "Search by domain or submitter."
|
||||
fieldsets = [
|
||||
(None, {"fields": ["status", "investigator", "creator"]}),
|
||||
("Type of organization", {"fields": ["organization_type", "federally_recognized_tribe", "state_recognized_tribe", "tribe_name", "federal_agency", "federal_type", "is_election_board", "type_of_work", "more_organization_information"]}),
|
||||
("Organization name and mailing address", {"fields": ["organization_name", "address_line1", "address_line2", "city", "state_territory", "zipcode", "urbanization"]}),
|
||||
(
|
||||
"Type of organization",
|
||||
{
|
||||
"fields": [
|
||||
"organization_type",
|
||||
"federally_recognized_tribe",
|
||||
"state_recognized_tribe",
|
||||
"tribe_name",
|
||||
"federal_agency",
|
||||
"federal_type",
|
||||
"is_election_board",
|
||||
"type_of_work",
|
||||
"more_organization_information",
|
||||
]
|
||||
},
|
||||
),
|
||||
(
|
||||
"Organization name and mailing address",
|
||||
{
|
||||
"fields": [
|
||||
"organization_name",
|
||||
"address_line1",
|
||||
"address_line2",
|
||||
"city",
|
||||
"state_territory",
|
||||
"zipcode",
|
||||
"urbanization",
|
||||
]
|
||||
},
|
||||
),
|
||||
("Authorizing official", {"fields": ["authorizing_official"]}),
|
||||
("Current websites", {"fields": ["current_websites"]}),
|
||||
(".gov domain", {"fields": ["requested_domain", "alternative_domains"]}),
|
||||
("Purpose of your domain", {"fields": ["purpose"]}),
|
||||
("Your contact information", {"fields": ["submitter"]}),
|
||||
("Other employees from your organization?", {"fields": ["other_contacts"]}),
|
||||
("No other employees from your organization?", {"fields": ["no_other_contacts_rationale"]}),
|
||||
(
|
||||
"No other employees from your organization?",
|
||||
{"fields": ["no_other_contacts_rationale"]},
|
||||
),
|
||||
("Anything else we should know?", {"fields": ["anything_else"]}),
|
||||
("Requirements for operating .gov domains", {"fields": ["is_policy_acknowledged"]}),
|
||||
(
|
||||
"Requirements for operating .gov domains",
|
||||
{"fields": ["is_policy_acknowledged"]},
|
||||
),
|
||||
]
|
||||
readonly_fields = [
|
||||
"creator",
|
||||
"type_of_work",
|
||||
"more_organization_information",
|
||||
"address_line1",
|
||||
"address_line2",
|
||||
"zipcode",
|
||||
"requested_domain",
|
||||
"alternative_domains",
|
||||
"purpose",
|
||||
"submitter",
|
||||
"no_other_contacts_rationale",
|
||||
"anything_else",
|
||||
"is_policy_acknowledged",
|
||||
]
|
||||
readonly_fields = ["creator", "type_of_work", "more_organization_information", "address_line1", "address_line2", "zipcode", "requested_domain", "alternative_domains", "purpose", "submitter", "no_other_contacts_rationale", "anything_else", "is_policy_acknowledged"]
|
||||
|
||||
# Trigger action when a fieldset is changed
|
||||
def save_model(self, request, obj, form, change):
|
||||
|
|
|
@ -64,25 +64,17 @@ class UserFixture:
|
|||
|
||||
STAFF_PERMISSIONS = [
|
||||
{
|
||||
'app_label': 'auditlog',
|
||||
'model': 'logentry',
|
||||
'permissions': ['view_logentry']
|
||||
"app_label": "auditlog",
|
||||
"model": "logentry",
|
||||
"permissions": ["view_logentry"],
|
||||
},
|
||||
{"app_label": "registrar", "model": "contact", "permissions": ["view_contact"]},
|
||||
{
|
||||
'app_label': 'registrar',
|
||||
'model': 'contact',
|
||||
'permissions': ['view_contact']
|
||||
},
|
||||
{
|
||||
'app_label': 'registrar',
|
||||
'model': 'domainapplication',
|
||||
'permissions': ['change_domainapplication']
|
||||
},
|
||||
{
|
||||
'app_label': 'registrar',
|
||||
'model': 'domain',
|
||||
'permissions': ['view_domain']
|
||||
"app_label": "registrar",
|
||||
"model": "domainapplication",
|
||||
"permissions": ["change_domainapplication"],
|
||||
},
|
||||
{"app_label": "registrar", "model": "domain", "permissions": ["view_domain"]},
|
||||
]
|
||||
|
||||
@classmethod
|
||||
|
@ -117,19 +109,31 @@ class UserFixture:
|
|||
user.is_active = True
|
||||
|
||||
for permission in cls.STAFF_PERMISSIONS:
|
||||
app_label = permission['app_label']
|
||||
model_name = permission['model']
|
||||
permissions = permission['permissions']
|
||||
app_label = permission["app_label"]
|
||||
model_name = permission["model"]
|
||||
permissions = permission["permissions"]
|
||||
|
||||
# Retrieve the content type for the app and model
|
||||
content_type = ContentType.objects.get(app_label=app_label, model=model_name)
|
||||
content_type = ContentType.objects.get(
|
||||
app_label=app_label, model=model_name
|
||||
)
|
||||
|
||||
# Retrieve the permissions based on their codenames
|
||||
permissions = Permission.objects.filter(content_type=content_type, codename__in=permissions)
|
||||
permissions = Permission.objects.filter(
|
||||
content_type=content_type, codename__in=permissions
|
||||
)
|
||||
|
||||
# Assign the permissions to the user
|
||||
user.user_permissions.add(*permissions)
|
||||
logger.debug(f"{app_label} | {model_name} | {permissions} added for user {staff['first_name']}")
|
||||
logger.debug(
|
||||
app_label
|
||||
+ " | "
|
||||
+ model_name
|
||||
+ " | "
|
||||
+ permissions
|
||||
+ " added for user "
|
||||
+ staff["first_name"]
|
||||
)
|
||||
|
||||
user.save()
|
||||
logger.debug("User object created for %s" % staff["first_name"])
|
||||
|
@ -138,7 +142,6 @@ class UserFixture:
|
|||
logger.debug("All CISA analysts (staff) loaded.")
|
||||
|
||||
|
||||
|
||||
class DomainApplicationFixture:
|
||||
"""
|
||||
Load domain applications into the database.
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
from django.test import TestCase, RequestFactory, Client
|
||||
from django.contrib.admin.sites import AdminSite
|
||||
from registrar.admin import DomainApplicationAdmin, ListHeaderAdmin, AuditedAdmin
|
||||
from registrar.admin import DomainApplicationAdmin, ListHeaderAdmin
|
||||
from registrar.models import DomainApplication, User
|
||||
from .common import completed_application
|
||||
from django.contrib.auth import get_user_model
|
||||
|
||||
from django.conf import settings
|
||||
from unittest.mock import MagicMock
|
||||
|
@ -14,7 +15,20 @@ class TestDomainApplicationAdmin(TestCase):
|
|||
self.site = AdminSite()
|
||||
self.factory = RequestFactory()
|
||||
self.admin = ListHeaderAdmin(model=DomainApplication, admin_site=None)
|
||||
self.client = Client(HTTP_HOST='localhost:8080')
|
||||
self.client = Client(HTTP_HOST="localhost:8080")
|
||||
username = "admin"
|
||||
first_name = "First"
|
||||
last_name = "Last"
|
||||
email = "info@example.com"
|
||||
p = "adminpassword"
|
||||
User = get_user_model()
|
||||
self.superuser = User.objects.create_superuser(
|
||||
username=username,
|
||||
first_name=first_name,
|
||||
last_name=last_name,
|
||||
email=email,
|
||||
password=p,
|
||||
)
|
||||
|
||||
@boto3_mocking.patching
|
||||
def test_save_model_sends_email_on_property_change(self):
|
||||
|
@ -66,43 +80,52 @@ class TestDomainApplicationAdmin(TestCase):
|
|||
application.delete()
|
||||
|
||||
def test_changelist_view(self):
|
||||
# Have to get creative to get past linter
|
||||
p = "adminpassword"
|
||||
self.client.login(username="admin", password=p)
|
||||
|
||||
# Make the request using the Client class
|
||||
# which handles CSRF
|
||||
# Follow=True handles the redirect
|
||||
request = self.client.get('/admin/registrar/domainapplication/', {'param1': 'value1', 'param2': 'value2'}, follow=True, max_redirects=10)
|
||||
|
||||
print(f'request {request}')
|
||||
|
||||
# request = self.factory.get('/admin/registrar/domainapplication/')
|
||||
# # Set the GET parameters for testing
|
||||
# request.GET = {'param1': 'value1', 'param2': 'value2', 'q': 'search_value'}
|
||||
# # Call the changelist_view method
|
||||
response = self.admin.changelist_view(request, extra_context={'filters': [{'parameter_name': 'status', 'parameter_value': 'started'}], 'search_query': ''})
|
||||
|
||||
|
||||
print(f'response {response}')
|
||||
|
||||
# Assert that the final response is a successful response (not a redirect)
|
||||
# self.assertEqual(response.status_code, 200)
|
||||
response = self.client.get(
|
||||
"/admin/registrar/domainapplication/",
|
||||
{"status__exact": "started", "investigator__id__exact": "4", "q": "Hello"},
|
||||
follow=True,
|
||||
)
|
||||
|
||||
# Assert that the filters and search_query are added to the extra_context
|
||||
self.assertIn('filters', response.extra_context)
|
||||
self.assertIn('search_query', response.extra_context)
|
||||
self.assertIn("filters", response.context)
|
||||
self.assertIn("search_query", response.context)
|
||||
# Assert the content of filters and search_query
|
||||
filters = response.extra_context['filters']
|
||||
search_query = response.extra_context['search_query']
|
||||
self.assertEqual(filters, [{'parameter_name': 'param1', 'parameter_value': 'value1'},
|
||||
{'parameter_name': 'param2', 'parameter_value': 'value2'}])
|
||||
self.assertEqual(search_query, 'value of q parameter if present in the request GET')
|
||||
filters = response.context["filters"]
|
||||
search_query = response.context["search_query"]
|
||||
self.assertEqual(search_query, "Hello")
|
||||
self.assertEqual(
|
||||
filters,
|
||||
[
|
||||
{"parameter_name": "status", "parameter_value": "started"},
|
||||
{"parameter_name": "investigator id", "parameter_value": "4"},
|
||||
],
|
||||
)
|
||||
|
||||
def test_get_filters(self):
|
||||
# Create a mock request object
|
||||
request = self.factory.get('/admin/yourmodel/')
|
||||
request = self.factory.get("/admin/yourmodel/")
|
||||
# Set the GET parameters for testing
|
||||
request.GET = {'param1': 'value1', 'param2': 'value2', 'q': 'search_value'}
|
||||
request.GET = {"status": "started", "investigator id": "4", "q": "search_value"}
|
||||
# Call the get_filters method
|
||||
filters = self.admin.get_filters(request)
|
||||
|
||||
# Assert the filters extracted from the request GET
|
||||
self.assertEqual(filters, [{'parameter_name': 'param1', 'parameter_value': 'value1'},
|
||||
{'parameter_name': 'param2', 'parameter_value': 'value2'}])
|
||||
self.assertEqual(
|
||||
filters,
|
||||
[
|
||||
{"parameter_name": "status", "parameter_value": "started"},
|
||||
{"parameter_name": "investigator id", "parameter_value": "4"},
|
||||
],
|
||||
)
|
||||
|
||||
def tearDown(self):
|
||||
# delete any applications too
|
||||
DomainApplication.objects.all().delete()
|
||||
self.superuser.delete()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue