mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-05-17 18:09:25 +02:00
Added logic to hide certain fields from non-superusers in Admin
This commit is contained in:
parent
97139076ae
commit
df719f2108
1 changed files with 79 additions and 2 deletions
|
@ -1319,6 +1319,33 @@ class DomainInformationAdmin(ListHeaderAdmin, ImportExportModelAdmin):
|
||||||
|
|
||||||
change_form_template = "django/admin/domain_information_change_form.html"
|
change_form_template = "django/admin/domain_information_change_form.html"
|
||||||
|
|
||||||
|
|
||||||
|
superuser_only_fields = [
|
||||||
|
"portfolio",
|
||||||
|
]
|
||||||
|
|
||||||
|
# DEVELOPER's NOTE:
|
||||||
|
# Normally, to exclude a field from an Admin form, we could simply utilize
|
||||||
|
# Django's "exclude" feature. However, it causes a "missing key" error if we
|
||||||
|
# go that route for this particular form. The error gets thrown by our
|
||||||
|
# custom fieldset.html code and is due to the fact that "exclude" removes
|
||||||
|
# fields from base_fields but not fieldsets. Rather than reworking our
|
||||||
|
# custom frontend, it seems more straightforward (and easier to read) to simply
|
||||||
|
# modify the fieldsets list so that it excludes any fields we want to remove
|
||||||
|
# based on permissions (eg. superuser_only_fields) or other conditions.
|
||||||
|
def get_fieldsets(self, request, obj=None):
|
||||||
|
fieldsets = super().get_fieldsets(request, obj)
|
||||||
|
|
||||||
|
# Create a modified version of fieldsets without the 'isbn' field
|
||||||
|
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, {'fields': fields}))
|
||||||
|
return modified_fieldsets
|
||||||
|
return fieldsets
|
||||||
|
|
||||||
def get_readonly_fields(self, request, obj=None):
|
def get_readonly_fields(self, request, obj=None):
|
||||||
"""Set the read-only state on form elements.
|
"""Set the read-only state on form elements.
|
||||||
We have 1 conditions that determine which fields are read-only:
|
We have 1 conditions that determine which fields are read-only:
|
||||||
|
@ -1593,6 +1620,53 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
|
||||||
]
|
]
|
||||||
filter_horizontal = ("current_websites", "alternative_domains", "other_contacts")
|
filter_horizontal = ("current_websites", "alternative_domains", "other_contacts")
|
||||||
|
|
||||||
|
superuser_only_fields = [
|
||||||
|
"portfolio",
|
||||||
|
]
|
||||||
|
|
||||||
|
# DEVELOPER's NOTE:
|
||||||
|
# Normally, to exclude a field from an Admin form, we could simply utilize
|
||||||
|
# Django's "exclude" feature. However, it causes a "missing key" error if we
|
||||||
|
# go that route for this particular form. The error gets thrown by our
|
||||||
|
# custom fieldset.html code and is due to the fact that "exclude" removes
|
||||||
|
# fields from base_fields but not fieldsets. Rather than reworking our
|
||||||
|
# custom frontend, it seems more straightforward (and easier to read) to simply
|
||||||
|
# modify the fieldsets list so that it excludes any fields we want to remove
|
||||||
|
# based on permissions (eg. superuser_only_fields) or other conditions.
|
||||||
|
def get_fieldsets(self, request, obj=None):
|
||||||
|
fieldsets = super().get_fieldsets(request, obj)
|
||||||
|
|
||||||
|
# Create a modified version of fieldsets without the 'isbn' field
|
||||||
|
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, {'fields': fields}))
|
||||||
|
return modified_fieldsets
|
||||||
|
return fieldsets
|
||||||
|
|
||||||
|
# Fields only superusers can view
|
||||||
|
# exclude = ['address_line1', ]
|
||||||
|
# widgets = {'portfolio': forms.HiddenInput()}
|
||||||
|
|
||||||
|
|
||||||
|
# def get_form(self, request, obj, **kwargs):
|
||||||
|
# if request.user.has_perm("registrar.full_access_permission"):
|
||||||
|
# self.exclude = self.superuser_only_fields
|
||||||
|
# # self.fieldsets[1][1]['fields'][0].append('portfolio')
|
||||||
|
# # self.fieldsets[1][1]['fields'].pop('status')
|
||||||
|
# form = super(DomainRequestAdmin, self).get_form(request, obj, **kwargs)
|
||||||
|
# return form
|
||||||
|
|
||||||
|
|
||||||
|
# if not request.user.has_perm("registrar.full_access_permission"):
|
||||||
|
|
||||||
|
# for fieldset in self.fieldsets:
|
||||||
|
# for field in fieldset[0]["fields"]:
|
||||||
|
# if field==
|
||||||
|
|
||||||
|
|
||||||
# Table ordering
|
# Table ordering
|
||||||
# NOTE: This impacts the select2 dropdowns (combobox)
|
# NOTE: This impacts the select2 dropdowns (combobox)
|
||||||
# Currentl, there's only one for requests on DomainInfo
|
# Currentl, there's only one for requests on DomainInfo
|
||||||
|
@ -1851,6 +1925,7 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
|
||||||
return super().change_view(request, object_id, form_url, extra_context)
|
return super().change_view(request, object_id, form_url, extra_context)
|
||||||
|
|
||||||
def process_log_entry(self, log_entry):
|
def process_log_entry(self, log_entry):
|
||||||
|
|
||||||
"""Process a log entry and return filtered entry dictionary if applicable."""
|
"""Process a log entry and return filtered entry dictionary if applicable."""
|
||||||
changes = log_entry.changes
|
changes = log_entry.changes
|
||||||
status_changed = "status" in changes
|
status_changed = "status" in changes
|
||||||
|
@ -1908,6 +1983,7 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class TransitionDomainAdmin(ListHeaderAdmin):
|
class TransitionDomainAdmin(ListHeaderAdmin):
|
||||||
"""Custom transition domain admin class."""
|
"""Custom transition domain admin class."""
|
||||||
|
|
||||||
|
@ -2562,6 +2638,7 @@ class PortfolioAdmin(ListHeaderAdmin):
|
||||||
# "requestor",
|
# "requestor",
|
||||||
# ]
|
# ]
|
||||||
|
|
||||||
|
|
||||||
def save_model(self, request, obj, form, change):
|
def save_model(self, request, obj, form, change):
|
||||||
|
|
||||||
if obj.creator is not None:
|
if obj.creator is not None:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue