a working not final solution

This commit is contained in:
asaki222 2024-11-05 13:29:27 -05:00
parent a32ec351ca
commit 4b1f3f348f
No known key found for this signature in database
GPG key ID: 2C4F802060E06EA4
3 changed files with 180 additions and 132 deletions

View file

@ -1798,11 +1798,7 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
] ]
search_help_text = "Search by domain or creator." search_help_text = "Search by domain or creator."
fieldsets = [ common_fields = [
(
None,
{
"fields": [
"portfolio", "portfolio",
"sub_organization", "sub_organization",
"requested_suborganization", "requested_suborganization",
@ -1819,13 +1815,8 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
"approved_domain", "approved_domain",
"notes", "notes",
] ]
},
), contact_fields_with_portfolio = [
(".gov domain", {"fields": ["requested_domain", "alternative_domains"]}),
(
"Contacts",
{
"fields": [
"converted_senior_official", "converted_senior_official",
"other_contacts", "other_contacts",
"no_other_contacts_rationale", "no_other_contacts_rationale",
@ -1833,56 +1824,69 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
"cisa_representative_last_name", "cisa_representative_last_name",
"cisa_representative_email", "cisa_representative_email",
] ]
},
), contact_fields_without_portfolio = [
("Background info", {"fields": ["purpose", "anything_else", "current_websites"]}), "senior_official",
( "other_contacts",
"Type of organization", "no_other_contacts_rationale",
{ "cisa_representative_first_name",
"fields": [ "cisa_representative_last_name",
"is_election_board", "cisa_representative_email",
"converted_generic_org_type",
] ]
},
), background_fields = ["purpose", "anything_else", "current_websites"]
(
"Show details", extends_type_of_org_base_end = [
{
"classes": ["collapse--dgfieldset"],
"description": "Extends type of organization",
"fields": [
"converted_federal_type",
"converted_federal_agency",
"tribe_name", "tribe_name",
"federally_recognized_tribe", "federally_recognized_tribe",
"state_recognized_tribe", "state_recognized_tribe",
"about_your_organization", "about_your_organization",
], ]
},
), extends_type_of_org_without_portfolio_start = [
( "federal_type",
"Organization name and mailing address", "federal_agency",
{ ]
"fields": [
extends_type_of_org_with_portfolio_start = [
"converted_federal_type",
"converted_federal_agency",
]
organization_address_fields_with_portfolio = [
"converted_organization_name", "converted_organization_name",
"converted_state_territory", "converted_state_territory",
] ]
},
), organization_address_fields_without_portfolio = [
( "organization_name",
"Show details", "state_territory",
{ ]
"classes": ["collapse--dgfieldset"],
"description": "Extends organization name and mailing address", type_of_org_fields_with_portfolio = [
"fields": [ "is_election_board",
"converted_generic_org_type",
]
type_of_org_fields_without_portfolio = [
"is_election_board",
"generic_org_type",
]
show_details_address_with_portfolio = [
"converted_address_line1", "converted_address_line1",
"converted_address_line2", "converted_address_line2",
"converted_city", "converted_city",
"converted_zipcode", "converted_zipcode",
"converted_urbanization", "converted_urbanization",
], ]
},
), show_details_address_without_portfolio = [
"address_line1",
"address_line2",
"city",
"zipcode",
"urbanization",
] ]
# Readonly fields for analysts and superusers # Readonly fields for analysts and superusers
@ -1892,6 +1896,9 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
"alternative_domains", "alternative_domains",
"is_election_board", "is_election_board",
"status_history", "status_history",
)
readonly_fields_with_portfolio = [
"converted_senior_official", "converted_senior_official",
"converted_federal_type", "converted_federal_type",
"converted_federal_agency", "converted_federal_agency",
@ -1903,7 +1910,21 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
"converted_urbanization", "converted_urbanization",
"converted_city", "converted_city",
"converted_generic_org_type", "converted_generic_org_type",
) ]
readonly_fields_without_portfolio = [
"senior_official",
"federal_type",
"federal_agency",
"state_territory",
"organization_name",
"address_line1",
"address_line2",
"zipcode",
"urbanization",
"city",
"generic_org_type",
]
# Read only that we'll leverage for CISA Analysts # Read only that we'll leverage for CISA Analysts
analyst_readonly_fields = [ analyst_readonly_fields = [
@ -1924,6 +1945,7 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
"suborganization_city", "suborganization_city",
"suborganization_state_territory", "suborganization_state_territory",
] ]
autocomplete_fields = [ autocomplete_fields = [
"approved_domain", "approved_domain",
"requested_domain", "requested_domain",
@ -1932,6 +1954,7 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
"portfolio", "portfolio",
"sub_organization", "sub_organization",
] ]
filter_horizontal = ("current_websites", "alternative_domains", "other_contacts") filter_horizontal = ("current_websites", "alternative_domains", "other_contacts")
# Table ordering # Table ordering
@ -1942,22 +1965,78 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
change_form_template = "django/admin/domain_request_change_form.html" change_form_template = "django/admin/domain_request_change_form.html"
def get_fieldsets(self, request, obj=None): def get_fieldsets(self, request, obj=None):
fieldsets = super().get_fieldsets(request, obj) # fieldsets = list(super().get_fieldsets(request, obj)) # Get the default fieldsets
has_portfolio = obj and obj.portfolio # Check once for portfolio presence
# Hide certain suborg fields behind the organization feature flag # Common fields
# if it is not enabled fieldsets = [(None, {"fields": self.common_fields})]
fieldsets.append((".gov domain", {"fields": ["requested_domain", "alternative_domains"]}))
# Contacts fields based on portfolio
contacts_fields = self.contact_fields_with_portfolio if has_portfolio else self.contact_fields_without_portfolio
fieldsets.append(("Contacts", {"fields": contacts_fields}))
# Background info
fieldsets.append(("Background info", {"fields": self.background_fields}))
# Type of organization based on portfolio
type_of_org_fields = (
self.type_of_org_fields_with_portfolio if has_portfolio else self.type_of_org_fields_without_portfolio
)
fieldsets.append(("Type of organization", {"fields": type_of_org_fields}))
fieldsets.append(
(
"Show details",
{
"classes": ["collapse--dgfieldset"],
"description": "Extends type of organization",
"fields": (
self.extends_type_of_org_with_portfolio_start
if has_portfolio
else self.extends_type_of_org_without_portfolio_start + self.extends_type_of_org_base_end
),
},
)
)
# Organization name and address
address_fields = (
self.organization_address_fields_with_portfolio
if has_portfolio
else self.organization_address_fields_without_portfolio
)
fieldsets.append(("Organization name and mailing address", {"fields": address_fields}))
# Additional "Show details" sections
fieldsets.append(
(
"Show details",
{
"classes": ["collapse--dgfieldset"],
"description": "Extends organization name and mailing address",
"fields": (
self.show_details_address_with_portfolio
if has_portfolio
else self.show_details_address_without_portfolio
),
},
)
)
# Flag-based field exclusion
if not flag_is_active_for_user(request.user, "organization_feature"): if not flag_is_active_for_user(request.user, "organization_feature"):
excluded_fields = [ excluded_fields = [
"requested_suborganization", "requested_suborganization",
"suborganization_city", "suborganization_city",
"suborganization_state_territory", "suborganization_state_territory",
] ]
modified_fieldsets = [] # Filter out the excluded fields
for name, data in fieldsets: fieldsets = [
fields = data.get("fields", []) (name, {**data, "fields": [f for f in data["fields"] if f not in excluded_fields]})
fields = tuple(field for field in fields if field not in excluded_fields) for name, data in fieldsets
modified_fieldsets.append((name, {**data, "fields": fields})) ]
return modified_fieldsets
return fieldsets return fieldsets
# Trigger action when a fieldset is changed # Trigger action when a fieldset is changed
@ -2150,7 +2229,8 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
we'll use the baseline readonly_fields and extend it as needed. we'll use the baseline readonly_fields and extend it as needed.
""" """
readonly_fields = list(self.readonly_fields) readonly_fields = list(self.readonly_fields)
if obj and obj.portfolio:
readonly_fields.extend(self.readonly_fields_with_portfolio)
# Check if the creator is restricted # Check if the creator is restricted
if obj and obj.creator.status == models.User.RESTRICTED: if obj and obj.creator.status == models.User.RESTRICTED:
# For fields like CharField, IntegerField, etc., the widget used is # For fields like CharField, IntegerField, etc., the widget used is

View file

@ -327,7 +327,7 @@ This is using a custom implementation fieldset.html (see admin/fieldset.html)
{% if not skip_additional_contact_info %} {% if not skip_additional_contact_info %}
{% include "django/admin/includes/user_detail_list.html" with user=original_object.creator no_title_top_padding=field.is_readonly %} {% include "django/admin/includes/user_detail_list.html" with user=original_object.creator no_title_top_padding=field.is_readonly %}
{% endif%} {% endif%}
{% elif field.field.name == "converted_senior_official" or field.field.name == "senior_official" %} {% elif field.field.name == "senior_official" or field.field.name == "converted_senior_official" %}
<div class="flex-container"> <div class="flex-container">
<label aria-label="Senior official contact details"></label> <label aria-label="Senior official contact details"></label>
{% include "django/admin/includes/contact_detail_list.html" with user=original_object.senior_official no_title_top_padding=field.is_readonly %} {% include "django/admin/includes/contact_detail_list.html" with user=original_object.senior_official no_title_top_padding=field.is_readonly %}
@ -388,7 +388,7 @@ This is using a custom implementation fieldset.html (see admin/fieldset.html)
</details> </details>
{% endif %} {% endif %}
{% endwith %} {% endwith %}
{% elif field.field.name == "converted_state_territory" or field.field.name == "state_territory" and original_object|model_name_lowercase != 'portfolio' %} {% elif field.field.name == "state_territory" and original_object|model_name_lowercase != 'portfolio' %}
<div class="flex-container margin-top-2"> <div class="flex-container margin-top-2">
<span> <span>
CISA region: CISA region:

View file

@ -160,6 +160,7 @@ class TestDomainRequestAdmin(MockEppLib):
("no_other_contacts_rationale", "Required if creator does not list other employees"), ("no_other_contacts_rationale", "Required if creator does not list other employees"),
("alternative_domains", "Other domain names the creator provided for consideration"), ("alternative_domains", "Other domain names the creator provided for consideration"),
("no_other_contacts_rationale", "Required if creator does not list other employees"), ("no_other_contacts_rationale", "Required if creator does not list other employees"),
("Urbanization", "Required for Puerto Rico only"),
] ]
self.test_helper.assert_response_contains_distinct_values(response, expected_values) self.test_helper.assert_response_contains_distinct_values(response, expected_values)
@ -1630,17 +1631,6 @@ class TestDomainRequestAdmin(MockEppLib):
"alternative_domains", "alternative_domains",
"is_election_board", "is_election_board",
"status_history", "status_history",
"converted_senior_official",
"converted_federal_type",
"converted_federal_agency",
"converted_state_territory",
"converted_organization_name",
"converted_address_line1",
"converted_address_line2",
"converted_zipcode",
"converted_urbanization",
"converted_city",
"converted_generic_org_type",
"id", "id",
"created_at", "created_at",
"updated_at", "updated_at",
@ -1706,17 +1696,6 @@ class TestDomainRequestAdmin(MockEppLib):
"alternative_domains", "alternative_domains",
"is_election_board", "is_election_board",
"status_history", "status_history",
"converted_senior_official",
"converted_federal_type",
"converted_federal_agency",
"converted_state_territory",
"converted_organization_name",
"converted_address_line1",
"converted_address_line2",
"converted_zipcode",
"converted_urbanization",
"converted_city",
"converted_generic_org_type",
"converted_federal_agency", "converted_federal_agency",
"creator", "creator",
"about_your_organization", "about_your_organization",
@ -1749,17 +1728,6 @@ class TestDomainRequestAdmin(MockEppLib):
"alternative_domains", "alternative_domains",
"is_election_board", "is_election_board",
"status_history", "status_history",
"converted_senior_official",
"converted_federal_type",
"converted_federal_agency",
"converted_state_territory",
"converted_organization_name",
"converted_address_line1",
"converted_address_line2",
"converted_zipcode",
"converted_urbanization",
"converted_city",
"converted_generic_org_type",
] ]
self.assertEqual(readonly_fields, expected_fields) self.assertEqual(readonly_fields, expected_fields)