class PreviousAttemptsForm(DynamicallyRequiredBoolMixin, forms.ModelForm): class Meta: model = models.HPActionDetails fields = [ FILED_WITH_311, THIRTY_DAYS_SINCE_311, HPD_ISSUED_VIOLATIONS, THIRTY_DAYS_SINCE_VIOLATIONS, ] filed_with_311 = YesNoRadiosField() thirty_days_since_311 = YesNoRadiosField(required=False) hpd_issued_violations = YesNoRadiosField(required=False) thirty_days_since_violations = YesNoRadiosField(required=False) def clean(self): cleaned_data = super().clean() filed_with_311 = YesNoRadiosField.coerce(cleaned_data.get(FILED_WITH_311)) if filed_with_311 is True: hpd_issued_violations = self.require_bool_field( HPD_ISSUED_VIOLATIONS, cleaned_data) if hpd_issued_violations is False: self.require_bool_field(THIRTY_DAYS_SINCE_311, cleaned_data) cleaned_data[THIRTY_DAYS_SINCE_VIOLATIONS] = '' elif hpd_issued_violations is True: self.require_bool_field(THIRTY_DAYS_SINCE_VIOLATIONS, cleaned_data) cleaned_data[THIRTY_DAYS_SINCE_311] = '' elif filed_with_311 is False: cleaned_data[HPD_ISSUED_VIOLATIONS] = '' cleaned_data[THIRTY_DAYS_SINCE_311] = '' cleaned_data[THIRTY_DAYS_SINCE_VIOLATIONS] = '' return cleaned_data
class HarassmentApartmentForm(forms.ModelForm): class Meta: model = models.HarassmentDetails fields = [ 'two_or_less_apartments_in_building', 'more_than_one_family_per_apartment', ] two_or_less_apartments_in_building = YesNoRadiosField() more_than_one_family_per_apartment = YesNoRadiosField()
def require_bool_field(self, field: str, cleaned_data) -> Optional[bool]: value = YesNoRadiosField.coerce(cleaned_data.get(field)) if value is None: self.add_dynamically_required_error(field) else: assert isinstance(value, bool) return value
class PublicAssistanceForm(forms.ModelForm): class Meta: model = OnboardingInfo fields = ("receives_public_assistance",) receives_public_assistance = YesNoRadiosField( help_text=OnboardingInfo._meta.get_field("receives_public_assistance").help_text )
class ReliefAttemptsForm(forms.ModelForm): class Meta: model = OnboardingInfo fields = ("has_called_311",) has_called_311 = YesNoRadiosField( help_text=OnboardingInfo._meta.get_field("has_called_311").help_text )
class UrgentAndDangerousForm(forms.ModelForm): class Meta: model = models.HPActionDetails fields = [ 'urgent_and_dangerous' ] urgent_and_dangerous = YesNoRadiosField()
class PreviousAttemptsForm(forms.ModelForm): class Meta: model = models.HPActionDetails fields = [ FILED_WITH_311, THIRTY_DAYS_SINCE_311, HPD_ISSUED_VIOLATIONS, THIRTY_DAYS_SINCE_VIOLATIONS, ] filed_with_311 = YesNoRadiosField() thirty_days_since_311 = YesNoRadiosField(required=False) hpd_issued_violations = YesNoRadiosField(required=False) thirty_days_since_violations = YesNoRadiosField(required=False) def add_dynamically_required_error(self, field: str): msg = forms.Field.default_error_messages['required'] self.add_error(field, ValidationError(msg, code='required')) def require_bool_field(self, field: str, cleaned_data) -> Optional[bool]: value = YesNoRadiosField.coerce(cleaned_data.get(field)) if value is None: self.add_dynamically_required_error(field) else: assert isinstance(value, bool) return value def clean(self): cleaned_data = super().clean() filed_with_311 = YesNoRadiosField.coerce(cleaned_data.get(FILED_WITH_311)) if filed_with_311 is True: hpd_issued_violations = self.require_bool_field( HPD_ISSUED_VIOLATIONS, cleaned_data) if hpd_issued_violations is False: self.require_bool_field(THIRTY_DAYS_SINCE_311, cleaned_data) cleaned_data[THIRTY_DAYS_SINCE_VIOLATIONS] = '' elif hpd_issued_violations is True: self.require_bool_field(THIRTY_DAYS_SINCE_VIOLATIONS, cleaned_data) cleaned_data[THIRTY_DAYS_SINCE_311] = '' elif filed_with_311 is False: cleaned_data[HPD_ISSUED_VIOLATIONS] = '' cleaned_data[THIRTY_DAYS_SINCE_311] = '' cleaned_data[THIRTY_DAYS_SINCE_VIOLATIONS] = '' return cleaned_data
class OnboardingStep3Form(forms.ModelForm): class Meta: model = OnboardingInfo fields = ('lease_type', 'receives_public_assistance') receives_public_assistance = YesNoRadiosField( help_text=OnboardingInfo._meta.get_field( 'receives_public_assistance').help_text)
def clean(self): cleaned_data = super().clean() two_or_less = YesNoRadiosField.coerce(cleaned_data.get(TWO_OR_LESS_APARTMENTS_IN_BUILDING)) if two_or_less is True: self.require_bool_field(MORE_THAN_ONE_FAMILY_PER_APARTMENT, cleaned_data) elif two_or_less is False: cleaned_data[MORE_THAN_ONE_FAMILY_PER_APARTMENT] = '' return cleaned_data
def clean(self): cleaned_data = super().clean() has_current_case = YesNoRadiosField.coerce(cleaned_data.get("has_current_case")) if has_current_case is True: self.require_text_field("index_number", cleaned_data) elif has_current_case is False: cleaned_data["index_number"] = "" return cleaned_data
class OnboardingStep3Form(forms.ModelForm): class Meta: model = OnboardingInfo fields = ("lease_type", "receives_public_assistance") receives_public_assistance = YesNoRadiosField( help_text=OnboardingInfo._meta.get_field("receives_public_assistance").help_text ) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields["lease_type"].required = True
class HarassmentApartmentForm(DynamicallyRequiredBoolMixin, forms.ModelForm): class Meta: model = models.HarassmentDetails fields = [ TWO_OR_LESS_APARTMENTS_IN_BUILDING, MORE_THAN_ONE_FAMILY_PER_APARTMENT, ] two_or_less_apartments_in_building = YesNoRadiosField() more_than_one_family_per_apartment = YesNoRadiosField(required=False) def clean(self): cleaned_data = super().clean() two_or_less = YesNoRadiosField.coerce(cleaned_data.get(TWO_OR_LESS_APARTMENTS_IN_BUILDING)) if two_or_less is True: self.require_bool_field(MORE_THAN_ONE_FAMILY_PER_APARTMENT, cleaned_data) elif two_or_less is False: cleaned_data[MORE_THAN_ONE_FAMILY_PER_APARTMENT] = '' return cleaned_data
class IndexNumberForm(DynamicallyRequiredFieldsMixin, forms.ModelForm): class Meta: model = HardshipDeclarationDetails fields = ("index_number",) has_current_case = YesNoRadiosField() def clean(self): cleaned_data = super().clean() has_current_case = YesNoRadiosField.coerce(cleaned_data.get("has_current_case")) if has_current_case is True: self.require_text_field("index_number", cleaned_data) elif has_current_case is False: cleaned_data["index_number"] = "" return cleaned_data
def clean(self): cleaned_data = super().clean() filed_with_311 = YesNoRadiosField.coerce(cleaned_data.get(FILED_WITH_311)) if filed_with_311 is True: hpd_issued_violations = self.require_bool_field( HPD_ISSUED_VIOLATIONS, cleaned_data) if hpd_issued_violations is False: self.require_bool_field(THIRTY_DAYS_SINCE_311, cleaned_data) cleaned_data[THIRTY_DAYS_SINCE_VIOLATIONS] = '' elif hpd_issued_violations is True: self.require_bool_field(THIRTY_DAYS_SINCE_VIOLATIONS, cleaned_data) cleaned_data[THIRTY_DAYS_SINCE_311] = '' elif filed_with_311 is False: cleaned_data[HPD_ISSUED_VIOLATIONS] = '' cleaned_data[THIRTY_DAYS_SINCE_311] = '' cleaned_data[THIRTY_DAYS_SINCE_VIOLATIONS] = '' return cleaned_data
def test_reverse_coerce_to_str_works(self, value, expected): assert YesNoRadiosField.reverse_coerce_to_str(value) == expected
def test_coerce_works(self, value, expected): assert YesNoRadiosField.coerce(value) is expected
class FeeWaiverMiscForm(forms.ModelForm): class Meta: model = models.FeeWaiverDetails fields = ['asked_before'] asked_before = YesNoRadiosField()
def test_raises_value_error_on_unexpected_value(self): with pytest.raises(ValueError, match="Invalid YesNoRadiosField value: blah"): YesNoRadiosField.coerce("blah")
class FeeWaiverPublicAssistanceForm(forms.ModelForm): class Meta: model = models.FeeWaiverDetails fields = ['receives_public_assistance'] receives_public_assistance = YesNoRadiosField()