#3829: Return an alphabetized list in /admin and in-registrar on various fields - [aa] (#3878)

* Correcting sort in Admin: Domains, Portfolios

* Adding ordering to Suborganization table

* Validated user-facing ordering in Domain Request Suborganization dropdown

* Adding test for alphabetization

---------

Co-authored-by: CocoByte <nicolle.leclair@gmail.com>
This commit is contained in:
Abe Alam 2025-06-23 17:47:13 -04:00 committed by GitHub
parent 42b53f7b94
commit a5e96b93bf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 32 additions and 3 deletions

View file

@ -0,0 +1,17 @@
# Generated by Django 4.2.20 on 2025-06-16 19:58
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
("registrar", "0150_remove_domainrequest_eop_stakeholder_first_name_and_more"),
]
operations = [
migrations.AlterModelOptions(
name="suborganization",
options={"ordering": ["name"]},
),
]

View file

@ -192,4 +192,4 @@ class Portfolio(TimeStampedModel):
# == Getters for suborganization == #
def get_suborganizations(self):
"""Returns all suborganizations associated with this portfolio"""
return self.portfolio_suborganizations.all()
return self.portfolio_suborganizations.all().order_by("name")

View file

@ -9,6 +9,9 @@ class Suborganization(TimeStampedModel):
Suborganization under an organization (portfolio)
"""
class Meta:
ordering = ["name"]
name = models.CharField(
unique=True,
max_length=1000,

View file

@ -9,6 +9,7 @@ from registrar.utility.errors import MissingEmailError
from waffle.testutils import override_flag
from django_webtest import WebTest # type: ignore
from api.tests.common import less_console_noise_decorator
from bs4 import BeautifulSoup
from django.urls import reverse
from registrar.admin import (
DomainAdmin,
@ -4166,14 +4167,22 @@ class TestPortfolioAdmin(TestCase):
@less_console_noise_decorator
def test_suborganizations_display(self):
"""Tests the custom suborg field which displays all related suborgs"""
Suborganization.objects.create(name="Sub1", portfolio=self.portfolio)
Suborganization.objects.create(name="Sub2", portfolio=self.portfolio)
Suborganization.objects.create(name="Sub1", portfolio=self.portfolio)
Suborganization.objects.create(name="Sub5", portfolio=self.portfolio)
Suborganization.objects.create(name="Sub3", portfolio=self.portfolio)
Suborganization.objects.create(name="Sub4", portfolio=self.portfolio)
suborganizations = self.admin.suborganizations(self.portfolio)
self.assertIn("Sub1", suborganizations)
self.assertIn("Sub2", suborganizations)
self.assertIn('<ul class="add-list-reset">', suborganizations)
# Ensuring alphabetical display of Suborgs
soup = BeautifulSoup(suborganizations, "html.parser")
suborg_names = [li.text for li in soup.find_all("li")]
self.assertEqual(suborg_names, ["Sub1", "Sub2", "Sub3", "Sub4", "Sub5"])
@less_console_noise_decorator
def test_domains_display(self):
"""Tests the custom domains field which displays all related domains"""

View file

@ -92,7 +92,7 @@ def get_suborganization_list_json(request):
return JsonResponse({"error": "Portfolio not found"}, status=404)
# Add suborganizations related to this portfolio
suborganizations = portfolio.portfolio_suborganizations.all().values("id", "name")
suborganizations = portfolio.portfolio_suborganizations.all().values("id", "name").order_by("name")
results = [{"id": sub["id"], "text": sub["name"]} for sub in suborganizations]
return JsonResponse({"results": results, "pagination": {"more": False}})