From d75742e2420a2cd74c12fa440a856a80299c8bf7 Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Thu, 14 Dec 2023 15:18:51 -0700 Subject: [PATCH] Add mixin --- src/registrar/admin.py | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/src/registrar/admin.py b/src/registrar/admin.py index b06f50cbb..81af24c84 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -543,7 +543,27 @@ class DomainApplicationAdminForm(forms.ModelForm): self.fields["status"].widget.choices = available_transitions -class DomainApplicationAdmin(ListHeaderAdmin): +class OrderableFieldsMixin: + orderable_fields = [] + + def __new__(cls, *args, **kwargs): + new_class = super().__new__(cls) + for field, sort_field in cls.orderable_fields: + setattr(new_class, f'get_{field}', cls._create_orderable_field_method(field, sort_field)) + return new_class + + @classmethod + def _create_orderable_field_method(cls, field, sort_field): + def method(obj): + attr = getattr(obj, field) + return attr + method.__name__ = f'get_{field}' + method.admin_order_field = f'{field}__{sort_field}' + method.short_description = field.replace('_', ' ').title() + return method + + +class DomainApplicationAdmin(ListHeaderAdmin, OrderableFieldsMixin): """Custom domain applications admin class.""" @@ -553,17 +573,16 @@ class DomainApplicationAdmin(ListHeaderAdmin): "status", "organization_type", "created_at", - "submitter", - "investigator", + "get_submitter", + "get_investigator", ] - def get_requested_domain(self, obj): - return obj.requested_domain - get_requested_domain.admin_order_field = 'requested_domain__name' # Allows column order sorting - get_requested_domain.short_description = 'Requested Domain' # Sets column's header - - - ordering = ['requested_domain__name'] + orderable_fields = [ + ('requested_domain', 'name'), + # TODO figure out sorting twice at once + ("submitter", "first_name"), + ("investigator", "first_name"), + ] # Filters list_filter = ("status", "organization_type", "investigator")