class Meta: model = Contact fields = ( 'id', 'title', 'first_name', 'last_name', 'name', 'job_title', 'company', 'adviser', 'primary', 'telephone_countrycode', 'telephone_number', 'email', 'address_same_as_company', 'address_1', 'address_2', 'address_town', 'address_county', 'address_country', 'address_postcode', 'telephone_alternative', 'email_alternative', 'notes', 'accepts_dit_email_marketing', 'archived', 'archived_documents_url_path', 'archived_on', 'archived_reason', 'archived_by', 'created_on', 'modified_on', ) read_only_fields = ('archived_documents_url_path', ) validators = [ NotArchivedValidator(), RulesBasedValidator( ValidationRule( 'address_same_as_company_and_has_address', OperatorRule('address_same_as_company', not_), when=AnyIsNotBlankRule( *Contact.ADDRESS_VALIDATION_MAPPING.keys()), ), ValidationRule( 'no_address', OperatorRule('address_same_as_company', bool), when=AllIsBlankRule( *Contact.ADDRESS_VALIDATION_MAPPING.keys()), ), ), # Note: This is deliberately after RulesBasedValidator, so that # address_same_as_company rules run first. AddressValidator( lazy=True, fields_mapping=Contact.ADDRESS_VALIDATION_MAPPING), ] permissions = { f'company.{ContactPermission.view_contact_document}': 'archived_documents_url_path', }
def test_fails_without_any_fields_if_not_lazy(self, values_as_data, with_instance): """ Test that the validation fails if lazy == False and the required fields are not specified. Test all scenarios: - with non-set fields on the instance and empty data - with non-set fields in the data param - with instance == None and empty data - with instance == None and non-set fields in the data param """ address_fields = { 'address1': None, 'address2': None, 'town': None, } instance = mock.Mock(**address_fields) if with_instance else None data = address_fields if values_as_data else {} validator = AddressValidator( lazy=False, fields_mapping={ 'address1': { 'required': True }, 'address2': { 'required': False }, 'town': { 'required': True }, }, ) validator.set_context(mock.Mock(instance=instance)) with pytest.raises(ValidationError) as exc: validator(data) assert exc.value.detail == { 'address1': ['This field is required.'], 'town': ['This field is required.'], }
def test_fails_without_all_required_fields_set(self, values_as_data, lazy): """ Test that the validation fails if only some fields are set but not all the required ones are. Test all scenarios: - with lazy == True and empty data - with lazy == True and only some fields set in data - with lazy == False and empty data - with lazy == False and only some fields set in data """ address_fields = { 'address1': None, 'address2': 'lorem ipsum', 'town': None, } instance = mock.Mock(**address_fields) data = address_fields if values_as_data else {} validator = AddressValidator( lazy=lazy, fields_mapping={ 'address1': { 'required': True }, 'address2': { 'required': False }, 'town': { 'required': True }, }, ) validator.set_context(mock.Mock(instance=instance)) with pytest.raises(ValidationError) as exc: validator(data) assert exc.value.detail == { 'address1': ['This field is required.'], 'town': ['This field is required.'], }
def test_passes_without_any_fields_set_if_lazy(self, values_as_data, with_instance): """ Test that the validation passes if lazy == True and none of the fields are specified. Test all scenarios: - with non-set fields on the instance and empty data - with non-set fields in the data param - with instance == None and empty data - with instance == None and non-set fields in the data param """ address_fields = { 'address1': None, 'address2': None, 'town': None, } instance = mock.Mock(**address_fields) if with_instance else None data = address_fields if values_as_data else {} validator = AddressValidator( lazy=True, fields_mapping={ 'address1': { 'required': True }, 'address2': { 'required': False }, 'town': { 'required': True }, }, ) validator.set_context(mock.Mock(instance=instance)) try: validator(data) except Exception: pytest.fail('Should not raise a validator error.')
class Meta: model = Company fields = ( 'id', 'reference_code', 'name', 'trading_name', 'uk_based', 'company_number', 'vat_number', 'registered_address_1', 'registered_address_2', 'registered_address_town', 'registered_address_county', 'registered_address_postcode', 'registered_address_country', 'created_on', 'modified_on', 'archived', 'archived_documents_url_path', 'archived_on', 'archived_reason', 'archived_by', 'description', 'website', 'trading_address_1', 'trading_address_2', 'trading_address_town', 'trading_address_county', 'trading_address_postcode', 'trading_address_country', 'business_type', 'classification', 'companies_house_data', 'contacts', 'employee_range', 'export_to_countries', 'future_interest_countries', 'headquarter_type', 'one_list_account_owner', 'global_headquarters', 'sector', 'turnover_range', 'uk_region', 'export_experience_category', ) read_only_fields = ( 'archived', 'archived_documents_url_path', 'archived_on', 'archived_reason', 'reference_code', ) validators = [ RequiredUnlessAlreadyBlankValidator('sector', 'business_type'), RulesBasedValidator( ValidationRule( 'required', OperatorRule('uk_region', bool), when=EqualsRule( 'registered_address_country', Country.united_kingdom.value.id, ), ), ValidationRule( 'uk_establishment_not_in_uk', EqualsRule('registered_address_country', Country.united_kingdom.value.id), when=EqualsRule( 'business_type', BusinessTypeConstant.uk_establishment.value.id, ), ), ValidationRule( 'required', OperatorRule('company_number', bool), when=EqualsRule( 'business_type', BusinessTypeConstant.uk_establishment.value.id, ), ), ValidationRule( 'invalid_uk_establishment_number_characters', OperatorRule('company_number', has_no_invalid_company_number_characters), when=EqualsRule( 'business_type', BusinessTypeConstant.uk_establishment.value.id, ), ), ValidationRule( 'invalid_uk_establishment_number_prefix', OperatorRule('company_number', has_uk_establishment_number_prefix), when=EqualsRule( 'business_type', BusinessTypeConstant.uk_establishment.value.id, ), ), ), AddressValidator( lazy=True, fields_mapping=Company.TRADING_ADDRESS_VALIDATION_MAPPING), ] permissions = { f'company.{CompanyPermission.view_company_document}': 'archived_documents_url_path', }
class Meta: model = Order fields = ( 'id', 'reference', 'status', 'created_on', 'created_by', 'modified_on', 'modified_by', 'company', 'contact', 'primary_market', 'sector', 'uk_region', 'service_types', 'description', 'contacts_not_to_approach', 'contact_email', 'contact_phone', 'product_info', 'further_info', 'existing_agents', 'permission_to_approach_contacts', 'delivery_date', 'po_number', 'discount_value', 'vat_status', 'vat_number', 'vat_verified', 'net_cost', 'subtotal_cost', 'vat_cost', 'total_cost', 'billing_company_name', 'billing_contact_name', 'billing_email', 'billing_phone', 'billing_address_1', 'billing_address_2', 'billing_address_town', 'billing_address_county', 'billing_address_postcode', 'billing_address_country', 'archived_documents_url_path', 'paid_on', 'completed_by', 'completed_on', 'cancelled_by', 'cancelled_on', 'cancellation_reason', ) read_only_fields = ( 'id', 'reference', 'status', 'created_on', 'created_by', 'modified_on', 'modified_by', 'contact_email', 'contact_phone', 'product_info', 'permission_to_approach_contacts', 'discount_value', 'net_cost', 'subtotal_cost', 'vat_cost', 'total_cost', 'archived_documents_url_path', 'paid_on', 'completed_by', 'completed_on', 'cancelled_by', 'cancelled_on', 'cancellation_reason', 'billing_company_name', 'billing_contact_name', 'billing_email', 'billing_phone', ) validators = ( # only some of the fields can be changed depending of the status OrderEditableFieldsValidator( { OrderStatus.draft: { *ORDER_FIELDS_INVOICE_RELATED, 'description', 'service_types', 'sector', 'uk_region', 'contacts_not_to_approach', 'contact', 'existing_agents', 'further_info', 'delivery_date', }, OrderStatus.quote_awaiting_acceptance: { *ORDER_FIELDS_INVOICE_RELATED, 'contact', }, OrderStatus.quote_accepted: { *ORDER_FIELDS_INVOICE_RELATED, 'contact', }, OrderStatus.paid: {'contact'}, OrderStatus.complete: {}, # nothing can be changed OrderStatus.cancelled: {}, # nothing can be changed }, ), # contact has to work at company ContactWorksAtCompanyValidator(), # validate billing address if edited AddressValidator( lazy=True, fields_mapping={ 'billing_address_1': { 'required': True }, 'billing_address_2': { 'required': False }, 'billing_address_town': { 'required': True }, 'billing_address_county': { 'required': False }, 'billing_address_postcode': { 'required': False }, 'billing_address_country': { 'required': True }, }, ), )
def test_defaults(self): """Test the defaults props.""" validator = AddressValidator() assert not validator.lazy assert validator.fields_mapping == validator.DEFAULT_FIELDS_MAPPING
class Meta(BaseCompanySerializer.Meta): extra_kwargs = { 'registered_address_2': { 'default': '' }, 'registered_address_county': { 'default': '' }, 'registered_address_postcode': { 'default': '' }, 'trading_address_1': { 'default': '' }, 'trading_address_2': { 'default': '' }, 'trading_address_town': { 'default': '' }, 'trading_address_county': { 'default': '' }, 'trading_address_postcode': { 'default': '' }, } fields = ( *BaseCompanySerializer.Meta.fields, 'companies_house_data', 'registered_address_1', 'registered_address_2', 'registered_address_town', 'registered_address_county', 'registered_address_postcode', 'registered_address_country', 'trading_address_1', 'trading_address_2', 'trading_address_town', 'trading_address_county', 'trading_address_postcode', 'trading_address_country', ) dnb_read_only_fields = ( *BaseCompanySerializer.Meta.dnb_read_only_fields, 'registered_address_1', 'registered_address_2', 'registered_address_town', 'registered_address_county', 'registered_address_postcode', 'registered_address_country', 'trading_address_1', 'trading_address_2', 'trading_address_town', 'trading_address_county', 'trading_address_postcode', 'trading_address_country', ) validators = ( *BaseCompanySerializer.Meta.validators, RulesBasedValidator( ValidationRule( 'required', OperatorRule('uk_region', bool), when=EqualsRule( 'registered_address_country', Country.united_kingdom.value.id, ), ), ValidationRule( 'uk_establishment_not_in_uk', EqualsRule('registered_address_country', Country.united_kingdom.value.id), when=EqualsRule( 'business_type', BusinessTypeConstant.uk_establishment.value.id, ), ), ), AddressValidator( lazy=True, fields_mapping=Company.TRADING_ADDRESS_VALIDATION_MAPPING), )