Merge branch 'main' into za/1946-update-field-helper-text

This commit is contained in:
zandercymatics 2024-04-22 15:06:21 -06:00
commit 143dd89c9d
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
39 changed files with 2138 additions and 450 deletions

View file

@ -318,6 +318,13 @@ class CustomLogEntryAdmin(LogEntryAdmin):
# 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"
@ -506,7 +513,7 @@ class MyUserAdmin(BaseUserAdmin):
list_display = (
"username",
"email",
"overridden_email_field",
"first_name",
"last_name",
# Group is a custom property defined within this file,
@ -515,6 +522,18 @@ class MyUserAdmin(BaseUserAdmin):
"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,
@ -589,6 +608,7 @@ class MyUserAdmin(BaseUserAdmin):
# 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"
@ -679,7 +699,7 @@ 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]
@ -687,10 +707,11 @@ 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",
]
# this ordering effects the ordering of results
# in autocomplete_fields for user
@ -707,10 +728,17 @@ class ContactAdmin(ListHeaderAdmin):
change_form_template = "django/admin/email_clipboard_change_form.html"
def user_exists(self, obj):
"""Check if the Contact has a related User"""
return "Yes" if obj.user is not None else "No"
user_exists.short_description = "Is user" # type: ignore
user_exists.admin_order_field = "user" # type: ignore
# 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()
@ -721,7 +749,7 @@ class ContactAdmin(ListHeaderAdmin):
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 = [
@ -879,7 +907,7 @@ class UserDomainRoleAdmin(ListHeaderAdmin):
"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"]
@ -979,7 +1007,9 @@ class DomainInformationAdmin(ListHeaderAdmin):
"classes": ["collapse"],
"fields": [
"federal_type",
"federal_agency",
# "updated_federal_agency",
# Above field commented out so it won't display
"federal_agency", # TODO: remove later
"tribe_name",
"federally_recognized_tribe",
"state_recognized_tribe",
@ -1218,7 +1248,9 @@ class DomainRequestAdmin(ListHeaderAdmin):
"classes": ["collapse"],
"fields": [
"federal_type",
"federal_agency",
# "updated_federal_agency",
# Above field commented out so it won't display
"federal_agency", # TODO: remove later
"tribe_name",
"federally_recognized_tribe",
"state_recognized_tribe",
@ -1463,12 +1495,36 @@ class DomainRequestAdmin(ListHeaderAdmin):
"""
Override changelist_view to set the selected value of status filter.
"""
# there are two conditions which should set the default selected filter:
# 1 - there are no query parameters in the request and the request is the
# initial request for this view
# 2 - there are no query parameters in the request and the referring url is
# the change view for a domain request
should_apply_default_filter = False
# use http_referer in order to distinguish between request as a link from another page
# and request as a removal of all filters
http_referer = request.META.get("HTTP_REFERER", "")
# if there are no query parameters in the request
# and the request is the initial request for this view
if not bool(request.GET) and request.path not in http_referer:
if not bool(request.GET):
# if the request is the initial request for this view
if request.path not in http_referer:
should_apply_default_filter = True
# elif the request is a referral from changelist view or from
# domain request change view
elif request.path in http_referer:
# find the index to determine the referring url after the path
index = http_referer.find(request.path)
# Check if there is a character following the path in http_referer
next_char_index = index + len(request.path)
if index + next_char_index < len(http_referer):
next_char = http_referer[next_char_index]
# Check if the next character is a digit, if so, this indicates
# a change view for domain request
if next_char.isdigit():
should_apply_default_filter = True
if should_apply_default_filter:
# modify the GET of the request to set the selected filter
modified_get = copy.deepcopy(request.GET)
modified_get["status__in"] = "submitted,in review,action needed"
@ -1505,10 +1561,11 @@ class DomainInformationInline(admin.StackedInline):
We had issues inheriting from both StackedInline
and the source DomainInformationAdmin since these
classes conflict, so we'll just pull what we need
from DomainInformationAdmin"""
from DomainInformationAdmin
"""
form = DomainInformationInlineForm
template = "django/admin/includes/domain_info_inline_stacked.html"
model = models.DomainInformation
fieldsets = copy.deepcopy(DomainInformationAdmin.fieldsets)
@ -1518,10 +1575,8 @@ class DomainInformationInline(admin.StackedInline):
del fieldsets[index]
break
readonly_fields = DomainInformationAdmin.readonly_fields
analyst_readonly_fields = DomainInformationAdmin.analyst_readonly_fields
# For each filter_horizontal, init in admin js extendFilterHorizontalWidgets
# to activate the edit/delete/view buttons
filter_horizontal = ("other_contacts",)
autocomplete_fields = [
"creator",
@ -1652,6 +1707,7 @@ class DomainAdmin(ListHeaderAdmin):
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
@ -1687,9 +1743,14 @@ class DomainAdmin(ListHeaderAdmin):
if extra_context is None:
extra_context = {}
# Pass in what the an extended expiration date would be for the expiration date modal
if object_id is not None:
domain = Domain.objects.get(pk=object_id)
# Used in the custom contact view
if domain is not None and hasattr(domain, "domain_info"):
extra_context["original_object"] = domain.domain_info
# Pass in what the an extended expiration date would be for the expiration date modal
extra_context = self._set_expiration_date_context(domain, extra_context)
extra_context["state_help_message"] = Domain.State.get_admin_help_text(domain.state)
@ -1699,7 +1760,6 @@ class DomainAdmin(ListHeaderAdmin):
"""Given a domain, calculate the an extended expiration date
from the current registry expiration date."""
years_to_extend_by = self._get_calculated_years_for_exp_date(domain)
try:
curr_exp_date = domain.registry_expiration_date
except KeyError:
@ -1968,6 +2028,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):
"""
@ -2046,13 +2111,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)