class Meta:
     model = InvestorProfile
     fields = ALL_LARGE_CAPITAL_FIELDS
     validators = [
         RulesBasedValidator(
             ValidationRule(
                 'invalid_required_checks_conducted_on',
                 OperatorRule('required_checks_conducted_on', bool),
                 when=InRule(
                     'required_checks_conducted',
                     REQUIRED_CHECKS_THAT_NEED_ADDITIONAL_INFORMATION,
                 ),
             ),
             ValidationRule(
                 'invalid_required_checks_conducted_by',
                 OperatorRule('required_checks_conducted_by', bool),
                 when=InRule(
                     'required_checks_conducted',
                     REQUIRED_CHECKS_THAT_NEED_ADDITIONAL_INFORMATION,
                 ),
             ),
             ValidationRule(
                 'required_checks_conducted_value',
                 OperatorRule('required_checks_conducted', is_not_blank),
                 when=AnyIsNotBlankRule(
                     'required_checks_conducted_by',
                     'required_checks_conducted_on',
                 ),
             ),
         ),
     ]
Example #2
0
 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',
     }
 class Meta(BaseCompanySerializer.Meta):
     fields = (
         *BaseCompanySerializer.Meta.fields,
         'address',
         'registered_address',
     )
     dnb_read_only_fields = (
         *BaseCompanySerializer.Meta.dnb_read_only_fields,
         'address',
         'registered_address',
     )
     validators = (
         *BaseCompanySerializer.Meta.validators,
         RulesBasedValidator(
             ValidationRule(
                 'required',
                 OperatorRule('uk_region', bool),
                 when=EqualsRule(
                     'address_country',
                     Country.united_kingdom.value.id,
                 ),
             ),
             ValidationRule(
                 'uk_establishment_not_in_uk',
                 EqualsRule('address_country',
                            Country.united_kingdom.value.id),
                 when=EqualsRule(
                     'business_type',
                     BusinessTypeConstant.uk_establishment.value.id,
                 ),
             ),
         ),
     )
    class Meta:
        list_serializer_class = OrderAssigneeListSerializer
        model = OrderAssignee
        fields = [
            'adviser',
            'estimated_time',
            'actual_time',
            'is_lead',
        ]
        validators = (
            RulesBasedValidator(
                # can't be changed when in draft, quote_awaiting_acceptance or quote_accepted
                ValidationRule(
                    'readonly',
                    OperatorRule('actual_time', is_blank),
                    when=OrderInStatusRule([
                        OrderStatus.draft,
                        OrderStatus.quote_awaiting_acceptance,
                        OrderStatus.quote_accepted,
                    ], ),
                ),

                # can't be changed when in quote_awaiting_acceptance, quote_accepted or paid
                ValidationRule(
                    'readonly',
                    OperatorRule('estimated_time', is_blank),
                    when=OrderInStatusRule([
                        OrderStatus.quote_awaiting_acceptance,
                        OrderStatus.quote_accepted,
                        OrderStatus.paid,
                    ], ),
                ),
                # can't be changed when in quote_awaiting_acceptance, quote_accepted or paid
                ValidationRule(
                    'readonly',
                    OperatorRule('is_lead', is_blank),
                    when=OrderInStatusRule([
                        OrderStatus.quote_awaiting_acceptance,
                        OrderStatus.quote_accepted,
                        OrderStatus.paid,
                    ], ),
                ),
            ), )
 class Meta(CompanySerializer.Meta):
     read_only_fields = []
     dnb_read_only_fields = []
     validators = (
         RulesBasedValidator(
             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,
                 ),
             ),
         ),
         RulesBasedValidator(
             ValidationRule(
                 'uk_establishment_not_in_uk',
                 EqualsRule('address_country',
                            Country.united_kingdom.value.id),
                 when=EqualsRule(
                     'business_type',
                     BusinessTypeConstant.uk_establishment.value.id,
                 ),
             ), ),
     )
Example #6
0
 class Meta:
     validators = [
         RulesBasedValidator(
             ValidationRule(
                 'invalid_required_checks_conducted_on',
                 IsFieldBeingUpdatedAndIsNotBlankRule(
                     'required_checks_conducted_on', ),
                 when=AndRule(
                     IsFieldBeingUpdatedRule('required_checks_conducted', ),
                     InRule(
                         'required_checks_conducted',
                         REQUIRED_CHECKS_THAT_NEED_ADDITIONAL_INFORMATION,
                     ),
                 ),
             ),
             ValidationRule(
                 'invalid_required_checks_conducted_by',
                 IsFieldBeingUpdatedAndIsNotBlankRule(
                     'required_checks_conducted_by', ),
                 when=AndRule(
                     IsFieldBeingUpdatedRule('required_checks_conducted', ),
                     InRule(
                         'required_checks_conducted',
                         REQUIRED_CHECKS_THAT_NEED_ADDITIONAL_INFORMATION,
                     ),
                 ),
             ),
             ValidationRule(
                 'required_checks_conducted_value',
                 OperatorRule(
                     'required_checks_conducted',
                     is_not_blank,
                 ),
                 when=AnyIsNotBlankRule(
                     'required_checks_conducted_by',
                     'required_checks_conducted_on',
                 ),
             ),
             ValidationRule(
                 'invalid_required_checks_conducted_on_must_be_within_12_months',
                 IsFieldRule(
                     'required_checks_conducted_on',
                     is_provided_and_is_date_less_than_a_year_ago,
                 ),
                 when=AndRule(
                     IsFieldBeingUpdatedRule(
                         'required_checks_conducted_on', ),
                     InRule(
                         'required_checks_conducted',
                         REQUIRED_CHECKS_THAT_NEED_ADDITIONAL_INFORMATION,
                     ),
                 ),
             ),
         ),
     ]
Example #7
0
 class Meta(ContactSerializer.Meta):
     validators = ContactSerializer.Meta.validators + [
         RulesBasedValidator(
             ValidationRule(
                 'required',
                 OperatorRule('address_area', bool),
                 when=InRule(
                     'address_country',
                     (
                         Country.united_states.value.id,
                         Country.canada.value.id,
                     ),
                 ),
             ),
         ),
     ]
Example #8
0
 def add_area_validator(self, validators):
     """
     Mark area as required for US and Canadian companies.
     """
     validators.append(
         RulesBasedValidator(
             ValidationRule(
                 'required',
                 OperatorRule(f'{self.address_source_prefix}_area', bool),
                 when=InRule(
                     f'{self.address_source_prefix}_country',
                     (
                         CountryEnum.united_states.value.id,
                         CountryEnum.canada.value.id,
                     ),
                 ),
             ), ), )
 class Meta:
     model = Interaction
     extra_kwargs = {
         # Date is a datetime in the model, but only the date component is used
         # (at present). Setting the formats as below effectively makes the field
         # behave like a date field without changing the schema and breaking the
         # v1 API.
         'date': {
             'format': '%Y-%m-%d',
             'input_formats': ['%Y-%m-%d']
         },
         'grant_amount_offered': {
             'min_value': 0
         },
         'net_company_receipt': {
             'min_value': 0
         },
         'status': {
             'default': Interaction.STATUSES.complete,
             'allow_null': False
         },
         'location': {
             'default': ''
         },
         'theme': {
             'allow_blank': False,
             'default': None,
         },
     }
     fields = (
         'id',
         'company',
         'contacts',
         'created_on',
         'created_by',
         'event',
         'is_event',
         'status',
         'kind',
         'modified_by',
         'modified_on',
         'date',
         'dit_adviser',
         'dit_participants',
         'dit_team',
         'communication_channel',
         'grant_amount_offered',
         'investment_project',
         'net_company_receipt',
         'service',
         'service_delivery_status',
         'subject',
         'theme',
         'notes',
         'archived_documents_url_path',
         'policy_areas',
         'policy_feedback_notes',
         'policy_issue_types',
         'was_policy_feedback_provided',
         'location',
         'archived',
         'archived_by',
         'archived_on',
         'archived_reason',
     )
     read_only_fields = (
         'archived_documents_url_path',
         'archived',
         'archived_by',
         'archived_on',
         'archived_reason',
     )
     validators = [
         HasAssociatedInvestmentProjectValidator(),
         ContactsBelongToCompanyValidator(),
         StatusChangeValidator(),
         RulesBasedValidator(
             # If dit_adviser and dit_team are *omitted* (note that they already have
             # allow_null=False) we assume that dit_participants is being used, and return an
             # error if it is empty.
             # TODO: Remove once dit_adviser and dit_team have been removed.
             ValidationRule(
                 'required',
                 OperatorRule('dit_participants', bool),
                 when=AllIsBlankRule('dit_adviser', 'dit_team'),
             ),
             # If dit_adviser has been provided, double-check that dit_team is also set.
             # TODO: Remove once dit_adviser and dit_team have been removed.
             ValidationRule(
                 'required',
                 OperatorRule('dit_adviser', bool),
                 when=AndRule(
                     OperatorRule('dit_team', bool),
                     OperatorRule('dit_participants', not_),
                 ),
             ),
             # If dit_team has been provided, double-check that dit_adviser is also set.
             # TODO: Remove once dit_adviser and dit_team have been removed.
             ValidationRule(
                 'required',
                 OperatorRule('dit_team', bool),
                 when=AndRule(
                     OperatorRule('dit_adviser', bool),
                     OperatorRule('dit_participants', not_),
                 ),
             ),
             ValidationRule(
                 'required',
                 OperatorRule('communication_channel', bool),
                 when=AndRule(
                     EqualsRule('kind', Interaction.KINDS.interaction),
                     EqualsRule('status', Interaction.STATUSES.complete),
                 ),
             ),
             ValidationRule(
                 'required',
                 OperatorRule('service', bool),
                 when=EqualsRule('status', Interaction.STATUSES.complete),
             ),
             ValidationRule(
                 'invalid_for_non_interaction',
                 OperatorRule('investment_project', not_),
                 when=EqualsRule('kind',
                                 Interaction.KINDS.service_delivery),
             ),
             ValidationRule(
                 'invalid_for_service_delivery',
                 OperatorRule('communication_channel', not_),
                 when=EqualsRule('kind',
                                 Interaction.KINDS.service_delivery),
             ),
             ValidationRule(
                 'invalid_for_non_service_delivery',
                 OperatorRule('is_event', is_blank),
                 OperatorRule('event', is_blank),
                 OperatorRule('service_delivery_status', is_blank),
                 OperatorRule('grant_amount_offered', is_blank),
                 OperatorRule('net_company_receipt', is_blank),
                 when=EqualsRule('kind', Interaction.KINDS.interaction),
             ),
             ValidationRule(
                 'invalid_when_no_policy_feedback',
                 OperatorRule('policy_issue_types', not_),
                 OperatorRule('policy_areas', not_),
                 OperatorRule('policy_feedback_notes', not_),
                 when=OperatorRule('was_policy_feedback_provided', not_),
             ),
             ValidationRule(
                 'required',
                 OperatorRule('policy_areas', bool),
                 OperatorRule('policy_issue_types', bool),
                 OperatorRule('policy_feedback_notes', is_not_blank),
                 when=OperatorRule('was_policy_feedback_provided', bool),
             ),
             ValidationRule(
                 'required',
                 OperatorRule('is_event', is_not_blank),
                 when=EqualsRule('kind',
                                 Interaction.KINDS.service_delivery),
             ),
             ValidationRule(
                 'required',
                 OperatorRule('event', bool),
                 when=OperatorRule('is_event', bool),
             ),
             ValidationRule(
                 'too_many_contacts_for_event_service_delivery',
                 OperatorRule('contacts', lambda value: len(value) <= 1),
                 when=OperatorRule('is_event', bool),
             ),
             ValidationRule(
                 'invalid_for_non_event',
                 OperatorRule('event', not_),
                 when=OperatorRule('is_event', not_),
             ),
         ),
     ]
Example #10
0
 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',
     }
Example #11
0
 class Meta:
     model = Interaction
     extra_kwargs = {
         # Date is a datetime in the model, but only the date component is used
         # (at present). Setting the formats as below effectively makes the field
         # behave like a date field without changing the schema and breaking the
         # v1 API.
         'date': {'format': '%Y-%m-%d', 'input_formats': ['%Y-%m-%d']},
         'grant_amount_offered': {'min_value': 0},
         'net_company_receipt': {'min_value': 0},
         'status': {'default': Interaction.Status.COMPLETE},
         'theme': {
             'allow_blank': False,
             'default': None,
         },
     }
     fields = (
         'id',
         'company',
         'contacts',
         'created_on',
         'created_by',
         'event',
         'is_event',
         'status',
         'kind',
         'modified_by',
         'modified_on',
         'date',
         'dit_participants',
         'communication_channel',
         'grant_amount_offered',
         'investment_project',
         'net_company_receipt',
         'service',
         'service_answers',
         'service_delivery_status',
         'subject',
         'theme',
         'notes',
         'archived_documents_url_path',
         'policy_areas',
         'policy_feedback_notes',
         'policy_issue_types',
         'was_policy_feedback_provided',
         'were_countries_discussed',
         'export_countries',
         'archived',
         'archived_by',
         'archived_on',
         'archived_reason',
         'company_referral',
     )
     read_only_fields = (
         'archived_documents_url_path',
         'archived',
         'archived_by',
         'archived_on',
         'archived_reason',
     )
     # Note: These validators are also used by the admin site import interactions tool
     # (see the admin_csv_import sub-package)
     validators = [
         HasAssociatedInvestmentProjectValidator(),
         ContactsBelongToCompanyValidator(),
         StatusChangeValidator(),
         ServiceAnswersValidator(),
         DuplicateExportCountryValidator(),
         RulesBasedValidator(
             ValidationRule(
                 'required',
                 OperatorRule('communication_channel', bool),
                 when=AndRule(
                     EqualsRule('kind', Interaction.Kind.INTERACTION),
                     EqualsRule('status', Interaction.Status.COMPLETE),
                 ),
             ),
             ValidationRule(
                 'required',
                 OperatorRule('service', bool),
                 when=EqualsRule('status', Interaction.Status.COMPLETE),
             ),
             ValidationRule(
                 'invalid_for_investment',
                 EqualsRule('kind', Interaction.Kind.INTERACTION),
                 when=EqualsRule('theme', Interaction.Theme.INVESTMENT),
             ),
             ValidationRule(
                 'invalid_for_non_interaction',
                 OperatorRule('investment_project', not_),
                 when=EqualsRule('kind', Interaction.Kind.SERVICE_DELIVERY),
             ),
             ValidationRule(
                 'invalid_for_service_delivery',
                 OperatorRule('communication_channel', not_),
                 when=EqualsRule('kind', Interaction.Kind.SERVICE_DELIVERY),
             ),
             ValidationRule(
                 'invalid_for_non_service_delivery',
                 OperatorRule('is_event', is_blank),
                 OperatorRule('event', is_blank),
                 OperatorRule('service_delivery_status', is_blank),
                 when=EqualsRule('kind', Interaction.Kind.INTERACTION),
             ),
             ValidationRule(
                 'invalid_when_no_policy_feedback',
                 OperatorRule('policy_issue_types', not_),
                 OperatorRule('policy_areas', not_),
                 OperatorRule('policy_feedback_notes', not_),
                 when=OperatorRule('was_policy_feedback_provided', not_),
             ),
             ValidationRule(
                 'required',
                 OperatorRule('policy_areas', bool),
                 OperatorRule('policy_issue_types', bool),
                 OperatorRule('policy_feedback_notes', is_not_blank),
                 when=OperatorRule('was_policy_feedback_provided', bool),
             ),
             ValidationRule(
                 'required',
                 OperatorRule('is_event', is_not_blank),
                 when=EqualsRule('kind', Interaction.Kind.SERVICE_DELIVERY),
             ),
             ValidationRule(
                 'too_many_contacts_for_event_service_delivery',
                 OperatorRule('contacts', lambda value: len(value) <= 1),
                 when=OperatorRule('is_event', bool),
             ),
             ValidationRule(
                 'invalid_for_investment',
                 OperatorRule('were_countries_discussed', not_),
                 OperatorRule('export_countries', not_),
                 when=EqualsRule('theme', Interaction.Theme.INVESTMENT),
             ),
             ValidationRule(
                 'required',
                 OperatorRule('were_countries_discussed', is_not_blank),
                 when=AndRule(
                     IsObjectBeingCreated(),
                     InRule(
                         'theme',
                         [Interaction.Theme.EXPORT, Interaction.Theme.OTHER],
                     ),
                 ),
             ),
             ValidationRule(
                 'required',
                 OperatorRule('export_countries', is_not_blank),
                 when=AndRule(
                     OperatorRule('were_countries_discussed', bool),
                     InRule(
                         'theme',
                         [Interaction.Theme.EXPORT, Interaction.Theme.OTHER],
                     ),
                 ),
             ),
             ValidationRule(
                 'invalid_when_no_countries_discussed',
                 OperatorRule('export_countries', is_blank),
                 when=AndRule(
                     IsObjectBeingCreated(),
                     OperatorRule('were_countries_discussed', not_),
                     InRule(
                         'theme',
                         [Interaction.Theme.EXPORT, Interaction.Theme.OTHER],
                     ),
                 ),
             ),
             # These two rules are only checked for service deliveries as there's a separate
             # check that event is blank for interactions above which takes precedence (to
             # avoid duplicate or contradictory error messages)
             ValidationRule(
                 'required',
                 OperatorRule('event', bool),
                 when=AndRule(
                     OperatorRule('is_event', bool),
                     EqualsRule('kind', Interaction.Kind.SERVICE_DELIVERY),
                 ),
             ),
             ValidationRule(
                 'invalid_for_non_event',
                 OperatorRule('event', not_),
                 when=AndRule(
                     OperatorRule('is_event', not_),
                     EqualsRule('kind', Interaction.Kind.SERVICE_DELIVERY),
                 ),
             ),
         ),
     ]
def test_operator_rule(data, field, op, res):
    """Tests ValidationCondition for various cases."""
    combiner = Mock(spec_set=DataCombiner, __getitem__=lambda self, field_: data[field_])
    condition = OperatorRule(field, op)
    assert condition(combiner) == res
Example #13
0
 class Meta:
     model = Company
     fields = (
         'id',
         'reference_code',
         'name',
         'trading_names',
         'uk_based',
         'company_number',
         'vat_number',
         'duns_number',
         'created_on',
         'modified_on',
         'archived',
         'archived_documents_url_path',
         'archived_on',
         'archived_reason',
         'archived_by',
         'description',
         'transferred_by',
         'transferred_on',
         'transferred_to',
         'transfer_reason',
         'website',
         'business_type',
         'one_list_group_tier',
         'contacts',
         'employee_range',
         'number_of_employees',
         'is_number_of_employees_estimated',
         'export_to_countries',
         'future_interest_countries',
         'headquarter_type',
         'one_list_group_global_account_manager',
         'global_headquarters',
         'sector',
         'turnover_range',
         'turnover',
         'is_turnover_estimated',
         'uk_region',
         'export_experience_category',
         'address',
         'registered_address',
         'pending_dnb_investigation',
         'export_potential',
         'great_profile_status',
         'is_global_ultimate',
         'global_ultimate_duns_number',
         'dnb_modified_on',
         'export_countries',
     )
     read_only_fields = (
         'archived',
         'archived_documents_url_path',
         'archived_on',
         'archived_reason',
         'reference_code',
         'transfer_reason',
         'duns_number',
         'turnover',
         'is_turnover_estimated',
         'number_of_employees',
         'is_number_of_employees_estimated',
         'pending_dnb_investigation',
         'export_potential',
         'great_profile_status',
         'is_global_ultimate',
         'global_ultimate_duns_number',
         'dnb_modified_on',
         'export_countries',
     )
     dnb_read_only_fields = (
         'name',
         'trading_names',
         'company_number',
         'vat_number',
         'business_type',
         'employee_range',
         'turnover_range',
         'address',
         'registered_address',
     )
     validators = (
         RequiredUnlessAlreadyBlankValidator('sector', 'business_type'),
         RulesBasedValidator(
             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,
                 ),
             ),
         ),
         RulesBasedValidator(
             ValidationRule(
                 'required',
                 OperatorRule('uk_region', bool),
                 when=EqualsRule(
                     'address_country',
                     Country.united_kingdom.value.id,
                 ),
             ),
             ValidationRule(
                 'uk_establishment_not_in_uk',
                 EqualsRule('address_country',
                            Country.united_kingdom.value.id),
                 when=EqualsRule(
                     'business_type',
                     BusinessTypeConstant.uk_establishment.value.id,
                 ),
             ),
         ),
     )
     permissions = {
         f'company.{CompanyPermission.view_company_document}':
         'archived_documents_url_path',
         'company.view_companyexportcountry': 'export_countries',
     }
 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),
     )
Example #15
0
 class Meta:
     model = Interaction
     extra_kwargs = {
         # Date is a datetime in the model, but only the date component is used
         # (at present). Setting the formats as below effectively makes the field
         # behave like a date field without changing the schema and breaking the
         # v1 API.
         'date': {
             'format': '%Y-%m-%d',
             'input_formats': ['%Y-%m-%d']
         },
         'grant_amount_offered': {
             'min_value': 0
         },
         'net_company_receipt': {
             'min_value': 0
         },
     }
     fields = (
         'id',
         'company',
         'contact',
         'created_on',
         'created_by',
         'event',
         'is_event',
         'kind',
         'modified_by',
         'modified_on',
         'date',
         'dit_adviser',
         'dit_team',
         'communication_channel',
         'grant_amount_offered',
         'investment_project',
         'net_company_receipt',
         'service',
         'service_delivery_status',
         'subject',
         'notes',
         'archived_documents_url_path',
         'policy_areas',
         'policy_issue_type',
     )
     read_only_fields = ('archived_documents_url_path', )
     validators = [
         KindPermissionValidator(),
         HasAssociatedInvestmentProjectValidator(),
         RulesBasedValidator(
             ValidationRule(
                 'required',
                 OperatorRule('communication_channel', bool),
                 when=InRule(
                     'kind',
                     [
                         Interaction.KINDS.interaction,
                         Interaction.KINDS.policy_feedback,
                     ],
                 ),
             ),
             ValidationRule(
                 'invalid_for_non_interaction',
                 OperatorRule('investment_project', not_),
                 when=InRule(
                     'kind',
                     [
                         Interaction.KINDS.service_delivery,
                         Interaction.KINDS.policy_feedback,
                     ],
                 ),
             ),
             ValidationRule(
                 'invalid_for_service_delivery',
                 OperatorRule('communication_channel', not_),
                 when=EqualsRule('kind',
                                 Interaction.KINDS.service_delivery),
             ),
             ValidationRule(
                 'invalid_for_non_service_delivery',
                 OperatorRule('is_event', is_blank),
                 OperatorRule('event', is_blank),
                 OperatorRule('service_delivery_status', is_blank),
                 OperatorRule('grant_amount_offered', is_blank),
                 OperatorRule('net_company_receipt', is_blank),
                 when=InRule(
                     'kind',
                     [
                         Interaction.KINDS.interaction,
                         Interaction.KINDS.policy_feedback,
                     ],
                 ),
             ),
             ValidationRule(
                 'invalid_for_non_policy_feedback',
                 OperatorRule('policy_areas', not_),
                 OperatorRule('policy_issue_type', is_blank),
                 when=InRule(
                     'kind',
                     [
                         Interaction.KINDS.interaction,
                         Interaction.KINDS.service_delivery,
                     ],
                 ),
             ),
             ValidationRule(
                 'required',
                 OperatorRule('is_event', is_not_blank),
                 when=EqualsRule('kind',
                                 Interaction.KINDS.service_delivery),
             ),
             ValidationRule(
                 'required',
                 OperatorRule('policy_areas', bool),
                 OperatorRule('policy_issue_type', is_not_blank),
                 when=EqualsRule('kind', Interaction.KINDS.policy_feedback),
             ),
             ValidationRule(
                 'required',
                 OperatorRule('event', bool),
                 when=OperatorRule('is_event', bool),
             ),
             ValidationRule(
                 'invalid_for_non_event',
                 OperatorRule('event', not_),
                 when=OperatorRule('is_event', not_),
             ),
             ValidationRule(
                 'required',
                 OperatorRule('notes', is_not_blank),
                 when=OperatorRule('is_event', not_),
             ),
         ),
     ]