def validate_data(self, data: dict) -> dict: valid_export_settings = VALID_EXPORT_SETTINGS + [EXPORT_SETTING_SOURCE] for required in REQUIRED_EXPORT_SETTINGS: if required not in data: raise serializers.ValidationError( { 'data': _( 'Must contain all the following required keys: {}' ).format( format_exception_values( REQUIRED_EXPORT_SETTINGS, 'and' ) ) } ) for key in data: if key not in valid_export_settings: raise serializers.ValidationError( { 'data': _( 'Can contain only the following valid keys: {}' ).format( format_exception_values( valid_export_settings, 'and' ) ) } ) return data
def validate_type(self, data: dict) -> str: export_type = data[EXPORT_SETTING_TYPE] if export_type not in VALID_EXPORT_TYPES: raise serializers.ValidationError({ EXPORT_SETTING_TYPE: t('Must be either {}').format( format_exception_values(VALID_EXPORT_TYPES)) }) return export_type
def validate_multiple_select(self, data: dict) -> str: multiple_select = data[EXPORT_SETTING_MULTIPLE_SELECT] if multiple_select not in VALID_MULTIPLE_SELECTS: raise serializers.ValidationError({ EXPORT_SETTING_MULTIPLE_SELECT: t('Must be either {}').format( format_exception_values(VALID_MULTIPLE_SELECTS)) }) return multiple_select
def validate_lang(self, data: dict) -> str: asset_languages = self._get_asset.summary.get('languages', []) all_valid_languages = [*asset_languages, *VALID_DEFAULT_LANGUAGES] lang = data[EXPORT_SETTING_LANG] if data[EXPORT_SETTING_LANG] not in all_valid_languages: raise serializers.ValidationError({ EXPORT_SETTING_LANG: t('For this asset must be either {}').format( format_exception_values(all_valid_languages)) }) return lang
def validate_export_settings(self, export_settings: dict) -> dict: asset = self.context['view'].asset asset_languages = asset.summary.get('languages', []) all_valid_languages = [*asset_languages, *VALID_DEFAULT_LANGUAGES] for required in REQUIRED_EXPORT_SETTINGS: if required not in export_settings: raise serializers.ValidationError( _( "`export_settings` must contain all the following " "required keys: {}" ).format( format_exception_values(REQUIRED_EXPORT_SETTINGS, 'and') ) ) for key in export_settings: if key not in VALID_EXPORT_SETTINGS: raise serializers.ValidationError( _( "`export_settings` can contain only the following " "valid keys: {}" ).format( format_exception_values(VALID_EXPORT_SETTINGS, 'and') ) ) if ( export_settings[EXPORT_SETTING_MULTIPLE_SELECT] not in VALID_MULTIPLE_SELECTS ): raise serializers.ValidationError( _("`multiple_select` must be either {}").format( format_exception_values(VALID_MULTIPLE_SELECTS) ) ) if export_settings[EXPORT_SETTING_TYPE] not in VALID_EXPORT_TYPES: raise serializers.ValidationError( _("`type` must be either {}").format( format_exception_values(VALID_EXPORT_TYPES) ) ) if ( export_settings[EXPORT_SETTING_HIERARCHY_IN_LABELS] and len(export_settings[EXPORT_SETTING_GROUP_SEP]) == 0 ): raise serializers.ValidationError( _('`group_sep` must be a non-empty value') ) if export_settings[EXPORT_SETTING_LANG] not in all_valid_languages: raise serializers.ValidationError( _("`lang` for this asset must be either {}").format( format_exception_values(all_valid_languages) ) ) if EXPORT_SETTING_FIELDS not in export_settings: return export_settings fields = export_settings[EXPORT_SETTING_FIELDS] if not isinstance(fields, list): raise serializers.ValidationError(_('`fields` must be an array')) if not all((isinstance(field, str) for field in fields)): raise serializers.ValidationError( _('All values in the `fields` array must be strings') ) # `flatten` is used for geoJSON exports only and is ignored otherwise if EXPORT_SETTING_FLATTEN not in export_settings: return export_settings return export_settings