diff --git a/src/registrar/admin.py b/src/registrar/admin.py index 1ac081eeb..ca51e8b72 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -20,7 +20,11 @@ from epplibwrapper.errors import ErrorCode, RegistryError from registrar.models.user_domain_role import UserDomainRole from waffle.admin import FlagAdmin from waffle.models import Sample, Switch -from registrar.utility.admin_helpers import get_all_action_needed_reason_emails, get_action_needed_reason_default_email, get_field_links_as_list +from registrar.utility.admin_helpers import ( + get_all_action_needed_reason_emails, + get_action_needed_reason_default_email, + get_field_links_as_list, +) from registrar.models import Contact, Domain, DomainRequest, DraftDomain, User, Website, SeniorOfficial from registrar.utility.constants import BranchChoices from registrar.utility.errors import FSMDomainRequestError, FSMErrorCodes diff --git a/src/registrar/models/utility/generic_helper.py b/src/registrar/models/utility/generic_helper.py index b02068de0..5e425f5a3 100644 --- a/src/registrar/models/utility/generic_helper.py +++ b/src/registrar/models/utility/generic_helper.py @@ -335,6 +335,7 @@ def get_url_name(path): logger.error(f"No matching URL name found for path: {path}") return None + def value_of_attribute(obj, attribute_name: str): """Returns the value of getattr if the attribute isn't callable. If it is, execute the underlying function and return that result instead.""" diff --git a/src/registrar/utility/admin_helpers.py b/src/registrar/utility/admin_helpers.py index a6428f826..2af9d0b3c 100644 --- a/src/registrar/utility/admin_helpers.py +++ b/src/registrar/utility/admin_helpers.py @@ -41,49 +41,53 @@ def get_action_needed_reason_default_email(request, domain_request, action_neede def get_field_links_as_list( - queryset, model_name, attribute_name=None, link_info_attribute=None, separator=None, msg_for_none="-", - ): - """ - Generate HTML links for items in a queryset, using a specified attribute for link text. + queryset, + model_name, + attribute_name=None, + link_info_attribute=None, + separator=None, + msg_for_none="-", +): + """ + Generate HTML links for items in a queryset, using a specified attribute for link text. - Args: - queryset: The queryset of items to generate links for. - model_name: The model name used to construct the admin change URL. - attribute_name: The attribute or method name to use for link text. If None, the item itself is used. - link_info_attribute: Appends f"({value_of_attribute})" to the end of the link. - separator: The separator to use between links in the resulting HTML. - If none, an unordered list is returned. - msg_for_none: What to return when the field would otherwise display None. - Defaults to `-`. + Args: + queryset: The queryset of items to generate links for. + model_name: The model name used to construct the admin change URL. + attribute_name: The attribute or method name to use for link text. If None, the item itself is used. + link_info_attribute: Appends f"({value_of_attribute})" to the end of the link. + separator: The separator to use between links in the resulting HTML. + If none, an unordered list is returned. + msg_for_none: What to return when the field would otherwise display None. + Defaults to `-`. - Returns: - A formatted HTML string with links to the admin change pages for each item. - """ - links = [] - for item in queryset: + Returns: + A formatted HTML string with links to the admin change pages for each item. + """ + links = [] + for item in queryset: - # This allows you to pass in attribute_name="get_full_name" for instance. - if attribute_name: - item_display_value = value_of_attribute(item, attribute_name) - else: - item_display_value = item - - if item_display_value: - change_url = reverse(f"admin:registrar_{model_name}_change", args=[item.pk]) - - link = f'{escape(item_display_value)}' - if link_info_attribute: - link += f" ({value_of_attribute(item, link_info_attribute)})" - - if separator: - links.append(link) - else: - links.append(f"