admin show/hide logic

This commit is contained in:
zandercymatics 2024-10-24 11:58:36 -06:00
parent 178d12711a
commit 14bfeb7d9b
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
3 changed files with 102 additions and 1 deletions

View file

@ -1885,6 +1885,27 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
change_form_template = "django/admin/domain_request_change_form.html"
# While the organization feature is under development, we can gate some fields
# from analysts for now. Remove this array and the get_fieldset overrides once this is done.
# Not my code initially, credit to Nicolle. This was once removed and like a phoenix it has been reborn.
superuser_only_fields = [
"requested_suborganization",
"suborganization_city",
"suborganization_state_territory",
]
def get_fieldsets(self, request, obj=None):
fieldsets = super().get_fieldsets(request, obj)
# Create a modified version of fieldsets to exclude certain fields
if not request.user.has_perm("registrar.full_access_permission"):
modified_fieldsets = []
for name, data in fieldsets:
fields = data.get("fields", [])
fields = tuple(field for field in fields if field not in self.superuser_only_fields)
modified_fieldsets.append((name, {**data, "fields": fields}))
return modified_fieldsets
return fieldsets
# Trigger action when a fieldset is changed
def save_model(self, request, obj, form, change):
"""Custom save_model definition that handles edge cases"""
@ -2297,6 +2318,15 @@ class DomainInformationInline(admin.StackedInline):
analyst_readonly_fields = copy.deepcopy(DomainInformationAdmin.analyst_readonly_fields)
autocomplete_fields = copy.deepcopy(DomainInformationAdmin.autocomplete_fields)
# While the organization feature is under development, we can gate some fields
# from analysts for now. Remove this array and the get_fieldset overrides once this is done.
# Not my code initially, credit to Nicolle. This was once removed and like a phoenix it has been reborn.
superuser_only_fields = [
"requested_suborganization",
"suborganization_city",
"suborganization_state_territory",
]
def get_domain_managers(self, obj):
user_domain_roles = UserDomainRole.objects.filter(domain=obj.domain)
user_ids = user_domain_roles.values_list("user_id", flat=True)
@ -2397,6 +2427,14 @@ class DomainInformationInline(admin.StackedInline):
# for permission-based field visibility.
modified_fieldsets = copy.deepcopy(DomainInformationAdmin.get_fieldsets(self, request, obj=None))
# Create a modified version of fieldsets to exclude certain fields
if not request.user.has_perm("registrar.full_access_permission"):
for name, data in modified_fieldsets:
fields = data.get("fields", [])
fields = tuple(field for field in fields if field not in self.superuser_only_fields)
modified_fieldsets.append((name, {**data, "fields": fields}))
return modified_fieldsets
# Modify fieldset sections in place
for index, (title, options) in enumerate(modified_fieldsets):
if title is None: