mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-31 15:06:32 +02:00
156 lines
4.9 KiB
Python
156 lines
4.9 KiB
Python
"""Forms for portfolio."""
|
|
|
|
import logging
|
|
from django import forms
|
|
from django.core.validators import RegexValidator
|
|
|
|
from registrar.models.portfolio_invitation import PortfolioInvitation
|
|
from registrar.models.user_portfolio_permission import UserPortfolioPermission
|
|
from registrar.models.utility.portfolio_helper import UserPortfolioPermissionChoices, UserPortfolioRoleChoices
|
|
|
|
from ..models import DomainInformation, Portfolio, SeniorOfficial
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class PortfolioOrgAddressForm(forms.ModelForm):
|
|
"""Form for updating the portfolio org mailing address."""
|
|
|
|
zipcode = forms.CharField(
|
|
label="Zip code",
|
|
validators=[
|
|
RegexValidator(
|
|
"^[0-9]{5}(?:-[0-9]{4})?$|^$",
|
|
message="Enter a zip code in the required format, like 12345 or 12345-6789.",
|
|
)
|
|
],
|
|
)
|
|
|
|
class Meta:
|
|
model = Portfolio
|
|
fields = [
|
|
"address_line1",
|
|
"address_line2",
|
|
"city",
|
|
"state_territory",
|
|
"zipcode",
|
|
# "urbanization",
|
|
]
|
|
error_messages = {
|
|
"address_line1": {"required": "Enter the street address of your organization."},
|
|
"city": {"required": "Enter the city where your organization is located."},
|
|
"state_territory": {
|
|
"required": "Select the state, territory, or military post where your organization is located."
|
|
},
|
|
}
|
|
widgets = {
|
|
# We need to set the required attributed for State/territory
|
|
# because for this fields we are creating an individual
|
|
# instance of the Select. For the other fields we use the for loop to set
|
|
# the class's required attribute to true.
|
|
"address_line1": forms.TextInput,
|
|
"address_line2": forms.TextInput,
|
|
"city": forms.TextInput,
|
|
"state_territory": forms.Select(
|
|
attrs={
|
|
"required": True,
|
|
},
|
|
choices=DomainInformation.StateTerritoryChoices.choices,
|
|
),
|
|
# "urbanization": forms.TextInput,
|
|
}
|
|
|
|
# the database fields have blank=True so ModelForm doesn't create
|
|
# required fields by default. Use this list in __init__ to mark each
|
|
# of these fields as required
|
|
required = ["address_line1", "city", "state_territory", "zipcode"]
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
for field_name in self.required:
|
|
self.fields[field_name].required = True
|
|
self.fields["state_territory"].widget.attrs.pop("maxlength", None)
|
|
self.fields["zipcode"].widget.attrs.pop("maxlength", None)
|
|
|
|
|
|
class PortfolioSeniorOfficialForm(forms.ModelForm):
|
|
"""
|
|
Form for updating the portfolio senior official.
|
|
This form is readonly for now.
|
|
"""
|
|
|
|
JOIN = "senior_official"
|
|
full_name = forms.CharField(label="Full name", required=False)
|
|
|
|
class Meta:
|
|
model = SeniorOfficial
|
|
fields = [
|
|
"title",
|
|
"email",
|
|
]
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
if self.instance and self.instance.id:
|
|
self.fields["full_name"].initial = self.instance.get_formatted_name()
|
|
|
|
def clean(self):
|
|
"""Clean override to remove unused fields"""
|
|
cleaned_data = super().clean()
|
|
cleaned_data.pop("full_name", None)
|
|
return cleaned_data
|
|
|
|
|
|
class PortfolioMemberForm(forms.ModelForm):
|
|
"""
|
|
Form for updating a portfolio member.
|
|
"""
|
|
|
|
roles = forms.MultipleChoiceField(
|
|
choices=UserPortfolioRoleChoices.choices,
|
|
widget=forms.SelectMultiple(attrs={'class': 'usa-select'}),
|
|
required=False,
|
|
label="Roles",
|
|
)
|
|
|
|
additional_permissions = forms.MultipleChoiceField(
|
|
choices=UserPortfolioPermissionChoices.choices,
|
|
widget=forms.SelectMultiple(attrs={'class': 'usa-select'}),
|
|
required=False,
|
|
label="Additional Permissions",
|
|
)
|
|
|
|
class Meta:
|
|
model = UserPortfolioPermission
|
|
fields = [
|
|
"roles",
|
|
"additional_permissions",
|
|
]
|
|
|
|
|
|
class PortfolioInvitedMemberForm(forms.ModelForm):
|
|
"""
|
|
Form for updating a portfolio invited member.
|
|
"""
|
|
|
|
roles = forms.MultipleChoiceField(
|
|
choices=UserPortfolioRoleChoices.choices,
|
|
widget=forms.SelectMultiple(attrs={'class': 'usa-select'}),
|
|
required=False,
|
|
label="Roles",
|
|
)
|
|
|
|
additional_permissions = forms.MultipleChoiceField(
|
|
choices=UserPortfolioPermissionChoices.choices,
|
|
widget=forms.SelectMultiple(attrs={'class': 'usa-select'}),
|
|
required=False,
|
|
label="Additional Permissions",
|
|
)
|
|
|
|
class Meta:
|
|
model = PortfolioInvitation
|
|
fields = [
|
|
"roles",
|
|
"additional_permissions",
|
|
]
|
|
|