"""Settings Whitelisted Resources Schema.""" from marshmallow import fields, pre_load from marshmallow.validate import Length, OneOf from permissions.constants import constants from permissions.validations import ma class SettingsResourcesSchema(ma.Schema): """Schema for allowed resources in settings app. Properties: - resource_type (str): Type of resource. currently 'LabelParticipant' only. - uuid (str): identifier for that resource. - roles (list): List of role. e.g: ['marketing', 'analytics'] """ resource_type = fields.Str( validate=OneOf(constants.SETTINGS_SUPPORT_MAPPING['resourceTypes']), required=True ) uuid = fields.Str(required=True) roles = fields.List( fields.String(validate=OneOf(constants.PROFILE_ROLE_TYPES)), required=True, validate=Length(min=1), ) @pre_load def slugify_roles(self, in_data, **kwargs): """Convert roles values to required format.""" if in_data.get('roles'): in_data['roles'] = [x.lower().strip().replace(' ', '_') for x in in_data['roles']] return in_data