mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-23 11:16:07 +02:00
Update admin.py
This commit is contained in:
parent
d75742e242
commit
3b173e0ad4
1 changed files with 20 additions and 21 deletions
|
@ -20,6 +20,25 @@ from django_fsm import TransitionNotAllowed # type: ignore
|
|||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class OrderableFieldsMixin:
|
||||
orderable_fk_fields = []
|
||||
|
||||
def __new__(cls, *args, **kwargs):
|
||||
new_class = super().__new__(cls)
|
||||
for field, sort_field in cls.orderable_fk_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 CustomLogEntryAdmin(LogEntryAdmin):
|
||||
"""Overwrite the generated LogEntry admin class"""
|
||||
|
@ -543,26 +562,6 @@ class DomainApplicationAdminForm(forms.ModelForm):
|
|||
self.fields["status"].widget.choices = available_transitions
|
||||
|
||||
|
||||
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."""
|
||||
|
@ -577,7 +576,7 @@ class DomainApplicationAdmin(ListHeaderAdmin, OrderableFieldsMixin):
|
|||
"get_investigator",
|
||||
]
|
||||
|
||||
orderable_fields = [
|
||||
orderable_fk_fields = [
|
||||
('requested_domain', 'name'),
|
||||
# TODO figure out sorting twice at once
|
||||
("submitter", "first_name"),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue