New Member form is assembled (just needs validation)

This commit is contained in:
CocoByte 2024-10-21 22:08:35 -06:00
parent 8b6d49ded2
commit b61d3936b5
No known key found for this signature in database
GPG key ID: BBFAA2526384C97F
8 changed files with 446 additions and 22 deletions

View file

@ -3,8 +3,11 @@
import logging
from django import forms
from django.core.validators import RegexValidator
from django.core.validators import MaxLengthValidator
from ..models import DomainInformation, Portfolio, SeniorOfficial
from registrar.models.user_portfolio_permission import UserPortfolioPermission
from ..models import DomainInformation, Portfolio, SeniorOfficial, User
logger = logging.getLogger(__name__)
@ -99,3 +102,69 @@ class PortfolioSeniorOfficialForm(forms.ModelForm):
cleaned_data = super().clean()
cleaned_data.pop("full_name", None)
return cleaned_data
class NewMemberForm(forms.ModelForm):
admin_org_domain_request_permissions = forms.ChoiceField(
label="Select permission",
choices=[('view_only', 'View all requests'), ('view_and_create', 'View all requests plus create requests')],
widget=forms.RadioSelect,
required=True)
admin_org_members_permissions = forms.ChoiceField(
label="Select permission", choices=[('view_only', 'View all members'), ('view_and_create', 'View all members plus manage members')], widget=forms.RadioSelect, required=True)
basic_org_domain_request_permissions = forms.ChoiceField(
label="Select permission", choices=[('view_only', 'View all requests'), ('view_and_create', 'View all requests plus create requests'),('no_access', 'No access')], widget=forms.RadioSelect, required=True)
email = forms.EmailField(
label="Enter the email of the member you'd like to invite",
max_length=None,
error_messages={
"invalid": ("Enter an email address in the required format, like name@example.com."),
"required": ("Enter an email address in the required format, like name@example.com."),
},
validators=[
MaxLengthValidator(
320,
message="Response must be less than 320 characters.",
)
],
required=True
)
class Meta:
model = User
fields = ['email'] #, 'grade', 'sport']
def __init__(self, *args, **kwargs):
super(NewMemberForm, self).__init__(*args, **kwargs)
# self.fields['sport'].choices = []
def clean(self):
cleaned_data = super().clean()
# Lowercase the value of the 'email' field
email_value = cleaned_data.get("email")
if email_value:
cleaned_data["email"] = email_value.lower()
# Check for an existing user (if there isn't any, send an invite)
if email_value:
try:
existingUser = User.objects.get(email=email_value)
except existingUser.DoesNotExist:
raise forms.ValidationError("User with this email does not exist.")
# grade = cleaned_data.get('grade')
# sport = cleaned_data.get('sport')
# # Handle sport options based on grade
# if grade == 'Junior':
# self.fields['sport'].choices = [('Basketball', 'Basketball'), ('Football', 'Football')]
# elif grade == 'Varsity':
# self.fields['sport'].choices = [('Swimming', 'Swimming'), ('Tennis', 'Tennis')]
# # Ensure both sport and grade are selected and valid
# if not grade or not sport:
# raise forms.ValidationError("Both grade and sport must be selected.")
return cleaned_data