class LDAPConfig(BaseSchema): tenant_uuid = fields.String(dump_only=True, default=None) host = fields.String(validate=Length(max=512), required=True, default=None) port = fields.Integer(required=True, default=None) protocol_version = fields.Integer(validate=Range(min=2, max=3), missing=3, default=None) protocol_security = fields.String( validate=OneOf(['ldaps', 'tls']), allow_none=True, default=None, ) bind_dn = fields.String(validate=Length(max=256), allow_none=True, default=None) user_base_dn = fields.String(validate=Length(max=256), required=True, default=None) user_login_attribute = fields.String(validate=Length(max=64), required=True, default=None) user_email_attribute = fields.String(validate=Length(max=64), required=True, default=None) search_filters = fields.String(allow_none=True, default=None)
class MarketListRequestSchema(Schema): direction = fields.String(validate=OneOf(['asc', 'desc']), missing='asc') order = fields.String(validate=Length(min=1), missing='name') limit = fields.Integer(validate=Range(min=0), missing=None) offset = fields.Integer(validate=Range(min=0), missing=0) search = fields.String(missing=None) installed = fields.Boolean()
class CDRListRequestSchema(CDRListingBase): direction = fields.String(validate=OneOf(['asc', 'desc']), missing='desc') order = fields.String( validate=OneOf(set(CDRSchema().fields) - {'end', 'tags', 'recordings'}), missing='start', ) limit = fields.Integer(validate=Range(min=0), missing=1000) offset = fields.Integer(validate=Range(min=0), missing=None) distinct = fields.String(validate=OneOf(['peer_exten']), missing=None) recorded = fields.Boolean(missing=None) format = fields.String(validate=OneOf(['csv', 'json']), missing=None) @post_load def map_order_field(self, in_data, **kwargs): mapped_order = CDRSchema().fields[in_data['order']].attribute if mapped_order: in_data['order'] = mapped_order return in_data
class TokenRequestSchema(BaseSchema): backend = fields.String(missing='wazo_user') expiration = fields.Integer(validate=Range(min=1)) access_type = fields.String(validate=OneOf(['online', 'offline'])) client_id = fields.String(validate=Length(min=1, max=1024)) refresh_token = fields.String() tenant_id = fields.String() domain_name = fields.String() @validates_schema def check_access_type_usage(self, data, **kwargs): access_type = data.get('access_type') if access_type != 'offline': return refresh_token = data.get('refresh_token') if refresh_token: raise ValidationError( 'cannot use the "access_type" "offline" with a refresh token' ) client_id = data.get('client_id') if not client_id: raise ValidationError( '"client_id" must be specified when using "access_type" is "offline"' ) @validates_schema def check_backend_type_for_tenant_id_and_domain_name(self, data, **kwargs): backend = data.get('backend') if not backend == 'ldap_user': return tenant_id = data.get('tenant_id') domain_name = data.get('domain_name') if tenant_id and domain_name: raise ValidationError( '"tenant_id" and "domain_name" must be mutually exclusive' ) if not tenant_id and not domain_name: raise ValidationError( '"tenant_id" or "domain_name" must be specified when using the "ldap_user" backend' ) @validates_schema def check_refresh_token_usage(self, data, **kwargs): refresh_token = data.get('refresh_token') if not refresh_token: return client_id = data.get('client_id') if not client_id: raise ValidationError( '"client_id" must be specified when using a "refresh_token"' )
class UserPresenceSchema(Schema): uuid = fields.UUID(dump_only=True) tenant_uuid = fields.UUID(dump_only=True) state = fields.String( required=True, validate=OneOf(['available', 'unavailable', 'invisible', 'away'])) status = fields.String(allow_none=True) last_activity = fields.DateTime(dump_only=True) line_state = fields.String(dump_only=True) mobile = fields.Boolean(dump_only=True) do_not_disturb = fields.Boolean(dump_only=True) connected = fields.Boolean(dump_only=True) lines = fields.Nested('LinePresenceSchema', many=True, dump_only=True) @post_dump def _set_line_state(self, user, **kwargs): line_states = [line['state'] for line in user['lines']] if 'ringing' in line_states: merged_state = 'ringing' elif 'progressing' in line_states: merged_state = 'progressing' elif 'holding' in line_states: merged_state = 'holding' elif 'talking' in line_states: merged_state = 'talking' elif 'available' in line_states: merged_state = 'available' else: merged_state = 'unavailable' user['line_state'] = merged_state return user @post_dump(pass_original=True) def _set_mobile(self, user, raw_user, **kwargs): for token in raw_user.refresh_tokens: if token.mobile is True: user['mobile'] = True return user for session in raw_user.sessions: if session.mobile is True: user['mobile'] = True return user user['mobile'] = False return user @post_dump(pass_original=True) def _set_connected(self, user, raw_user, **kwargs): user['connected'] = True if raw_user.sessions else False return user
class ApplicationSnoopPutSchema(BaseSchema): whisper_mode = fields.String(validate=OneOf(['in', 'out', 'both']), missing=None) @post_load def load_whisper_mode(self, data): whisper_mode = data.get('whisper_mode') if whisper_mode is None: data['whisper_mode'] = 'none' return data @pre_dump def dump_whisper_mode(self, data): if data.whisper_mode == 'none': data.whisper_mode = None return data
class SourceSchema(BaseSourceSchema): ldap_uri = fields.String(validate=Length(min=1, max=256), required=True) ldap_base_dn = fields.String(validate=Length(min=1, max=1024), required=True) ldap_username = fields.String(validate=Length(min=1), missing=None) ldap_password = fields.String(validate=Length(min=1), missing=None) ldap_custom_filter = fields.String(validate=Length(min=1, max=1024), missing=None) ldap_network_timeout = fields.Float(validate=Range(min=0), default=0.3) ldap_timeout = fields.Float(validate=Range(min=0), default=1.0) unique_column = fields.String(validate=Length(min=1, max=128), allow_none=True, missing=None) unique_column_format = fields.String(validate=OneOf( ['string', 'binary_uuid']), missing='string')
class QueueStatisticsQoSRequestSchema(_StatisticsListRequestSchema): interval = fields.String(validate=OneOf(['hour', 'day', 'month'])) qos_thresholds = fields.List(fields.Integer(validate=Range(min=0)), missing=[]) @pre_load def convert_qos_thresholds_to_list(self, data, **kwargs): result = data.copy() if not isinstance(result, dict): result = result.to_dict() if data.get('qos_thresholds'): result['qos_thresholds'] = list( set(data['qos_thresholds'].split(','))) return result @post_load def sort_qos_thresholds(self, data, **kwargs): result = data.copy() if data.get('qos_thresholds'): result['qos_thresholds'] = sorted(data['qos_thresholds']) return result
class CDRListingBase(Schema): from_ = fields.DateTime(data_key='from', attribute='start', missing=None) until = fields.DateTime(attribute='end', missing=None) search = fields.String(missing=None) call_direction = fields.String( validate=OneOf(['internal', 'inbound', 'outbound']), missing=None ) number = fields.String(validate=Regexp(NUMBER_REGEX), missing=None) tags = fields.List(fields.String(), missing=[]) user_uuid = fields.List(fields.String(), missing=[], attribute='user_uuids') from_id = fields.Integer(validate=Range(min=0), attribute='start_id', missing=None) recurse = fields.Boolean(missing=False) @pre_load def convert_tags_and_user_uuid_to_list(self, data, **kwargs): result = data.to_dict() if data.get('tags'): result['tags'] = data['tags'].split(',') if data.get('user_uuid'): result['user_uuid'] = data['user_uuid'].split(',') return result
class VoicemailGreetingCopySchema(Schema): dest_greeting = fields.String(validate=OneOf(VALID_GREETINGS))
class PluginInstallSchema(Schema): method = fields.String(validate=OneOf(['git', 'market']), required=True) options = OptionField(missing=dict, required=True)
class ApplicationSnoopPutSchema(BaseSchema): whisper_mode = fields.String(validate=OneOf(['in', 'out', 'both', 'none']), missing='none')
class _StatisticsListRequestSchema(Schema): from_ = fields.DateTime(data_key='from', missing=None) until = fields.DateTime(missing=None) day_start_time = fields.String(attribute='start_time', validate=Regexp(HOUR_REGEX)) day_end_time = fields.String(attribute='end_time', validate=Regexp(HOUR_REGEX)) week_days = fields.List( fields.Integer(), missing=[1, 2, 3, 4, 5, 6, 7], validate=ContainsOnly([1, 2, 3, 4, 5, 6, 7]), ) timezone = fields.String(validate=OneOf(pytz.all_timezones), missing='UTC') def _normalize_datetime(self, dt, timezone): if not dt.tzinfo: return timezone.normalize(timezone.localize(dt)) else: utc_dt = pytz.utc.normalize(dt) return timezone.normalize(utc_dt) @pre_load def convert_week_days_to_list(self, data, **kwargs): result = data.copy() if not isinstance(data, dict): result = data.to_dict() if data.get('week_days'): result['week_days'] = list(set(data['week_days'].split(','))) return result @post_load def convert_time_to_hour(self, data, **kwargs): if data.get('start_time'): start_time = time.fromisoformat(data['start_time']) data['start_time'] = start_time.hour if data.get('end_time'): end_time = time.fromisoformat(data['end_time']) data['end_time'] = end_time.hour return data @post_load def default_timezone_on_datetime(self, data, **kwargs): timezone = pytz.timezone(data.get('timezone')) from_ = data.get('from_') until = data.get('until') if from_: data['from_'] = self._normalize_datetime(from_, timezone) if until: data['until'] = self._normalize_datetime(until, timezone) return data @validates_schema def validate_dates(self, data, **kwargs): from_ = data.get('from_', None) until = data.get('until', None) timezone = pytz.timezone(data.get('timezone')) if from_: from_ = self._normalize_datetime(from_, timezone) if until: until = self._normalize_datetime(until, timezone) if from_ and until and until <= from_: raise ValidationError({'until': 'Field must be greater than from'}) @validates_schema def validate_start_end_times(self, data, **kwargs): if data.get('start_time') and data.get('end_time'): if data['start_time'] >= data['end_time']: raise ValidationError({ 'day_start_time': 'Field must be lower than day_end_time' })
class QueueStatisticsRequestSchema(QueueStatisticsListRequestSchema): interval = fields.String(validate=OneOf(['hour', 'day', 'month']))
def validate_method(self, data): OneOf(VALID_METHODS)(data.lower())