Skip to content

Commit

Permalink
Merge pull request #2035 from cisagov/es/1920-update-field-labels
Browse files Browse the repository at this point in the history
1920: Update admin field names
  • Loading branch information
erinysong authored Apr 18, 2024
2 parents c565655 + 687d818 commit c169ab3
Show file tree
Hide file tree
Showing 11 changed files with 470 additions and 21 deletions.
65 changes: 57 additions & 8 deletions src/registrar/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,13 @@ def resource(self, obj):
# Return the field value without a link
return f"{obj.content_type} - {obj.object_repr}"

# We name the custom prop 'created_at' because linter
# is not allowing a short_description attr on it
# This gets around the linter limitation, for now.
@admin.display(description=_("Created at"))
def created(self, obj):
return obj.timestamp

search_help_text = "Search by resource, changes, or user."

change_form_template = "admin/change_form_no_submit.html"
Expand Down Expand Up @@ -478,7 +485,7 @@ class Meta:

list_display = (
"username",
"email",
"overridden_email_field",
"first_name",
"last_name",
# Group is a custom property defined within this file,
Expand All @@ -487,6 +494,18 @@ class Meta:
"status",
)

# Renames inherited AbstractUser label 'email_address to 'email'
def formfield_for_dbfield(self, dbfield, **kwargs):
field = super().formfield_for_dbfield(dbfield, **kwargs)
if dbfield.name == "email":
field.label = "Email"
return field

# Renames inherited AbstractUser column name 'email_address to 'email'
@admin.display(description=_("Email"))
def overridden_email_field(self, obj):
return obj.email

fieldsets = (
(
None,
Expand Down Expand Up @@ -561,6 +580,7 @@ class Meta:
# this ordering effects the ordering of results
# in autocomplete_fields for user
ordering = ["first_name", "last_name", "email"]
search_help_text = "Search by first name, last name, or email."

change_form_template = "django/admin/email_clipboard_change_form.html"

Expand Down Expand Up @@ -651,17 +671,17 @@ class MyHostAdmin(AuditedAdmin):
"""Custom host admin class to use our inlines."""

search_fields = ["name", "domain__name"]
search_help_text = "Search by domain or hostname."
search_help_text = "Search by domain or host name."
inlines = [HostIPInline]


class ContactAdmin(ListHeaderAdmin):
"""Custom contact admin class to add search."""

search_fields = ["email", "first_name", "last_name"]
search_help_text = "Search by firstname, lastname or email."
search_help_text = "Search by first name, last name or email."
list_display = [
"contact",
"name",
"email",
"user_exists",
]
Expand Down Expand Up @@ -690,7 +710,7 @@ def user_exists(self, obj):
# We name the custom prop 'contact' because linter
# is not allowing a short_description attr on it
# This gets around the linter limitation, for now.
def contact(self, obj: models.Contact):
def name(self, obj: models.Contact):
"""Duplicate the contact _str_"""
if obj.first_name or obj.last_name:
return obj.get_formatted_name()
Expand All @@ -701,7 +721,7 @@ def contact(self, obj: models.Contact):
else:
return ""

contact.admin_order_field = "first_name" # type: ignore
name.admin_order_field = "first_name" # type: ignore

# Read only that we'll leverage for CISA Analysts
analyst_readonly_fields = [
Expand Down Expand Up @@ -859,7 +879,7 @@ class Meta:
"domain__name",
"role",
]
search_help_text = "Search by firstname, lastname, email, domain, or role."
search_help_text = "Search by first name, last name, email, or domain."

autocomplete_fields = ["user", "domain"]

Expand Down Expand Up @@ -1655,6 +1675,7 @@ def city(self, obj):

city.admin_order_field = "domain_info__city" # type: ignore

@admin.display(description=_("State / territory"))
def state_territory(self, obj):
return obj.domain_info.state_territory if obj.domain_info else None

Expand Down Expand Up @@ -1969,6 +1990,11 @@ class DraftDomainAdmin(ListHeaderAdmin):
# this ordering effects the ordering of results
# in autocomplete_fields for user
ordering = ["name"]
list_display = ["name"]

@admin.display(description=_("Requested domain"))
def name(self, obj):
return obj.name

def get_model_perms(self, request):
"""
Expand Down Expand Up @@ -2047,13 +2073,36 @@ class FederalAgencyAdmin(ListHeaderAdmin):
ordering = ["agency"]


class UserGroupAdmin(AuditedAdmin):
"""Overwrite the generated UserGroup admin class"""

list_display = ["user_group"]

fieldsets = ((None, {"fields": ("name", "permissions")}),)

def formfield_for_dbfield(self, dbfield, **kwargs):
field = super().formfield_for_dbfield(dbfield, **kwargs)
if dbfield.name == "name":
field.label = "Group name"
if dbfield.name == "permissions":
field.label = "User permissions"
return field

# We name the custom prop 'Group' because linter
# is not allowing a short_description attr on it
# This gets around the linter limitation, for now.
@admin.display(description=_("Group"))
def user_group(self, obj):
return obj.name


admin.site.unregister(LogEntry) # Unregister the default registration
admin.site.register(LogEntry, CustomLogEntryAdmin)
admin.site.register(models.User, MyUserAdmin)
# Unregister the built-in Group model
admin.site.unregister(Group)
# Register UserGroup
admin.site.register(models.UserGroup)
admin.site.register(models.UserGroup, UserGroupAdmin)
admin.site.register(models.UserDomainRole, UserDomainRoleAdmin)
admin.site.register(models.Contact, ContactAdmin)
admin.site.register(models.DomainInvitation, DomainInvitationAdmin)
Expand Down
Loading

0 comments on commit c169ab3

Please sign in to comment.