def __init__(self, required=False, allow_none=True, validate=None, **kwargs): if validate is not None: raise ValueError( "The SubscriberSubset field provides its own validation " "and thus does not accept a the 'validate' argument.") super().__init__(required=required, allow_none=allow_none, validate=OneOf([None]), **kwargs)
class UpdateFileSchema(ma.Schema): file = fields.Field(location='files', type='file', missing=None) comment = fields.Str(validate=Length(max=1024), missing=None) status = fields.Str(validate=OneOf([r[0] for r in STATUSES]), missing=None) @validates_schema def check_schema(self, data, **_): # noqa if not any([v for v in data.values()]): raise ValidationError('All fields are empty') status = data['status'] if status and not current_user.is_admin: if status not in [STATUS_CREATED, STATUS_MODERATION]: raise ValidationError(f'Status {status} not allowed', field_name='status')
class CitizenSchema(Schema): citizen_id = fields.Integer(strict=True, required=True, validate=isPositiveNumber) town = fields.Str(required=True, validate=validateStringFileds) street = fields.Str(required=True, validate=validateStringFileds) building = fields.Str(required=True, validate=validateStringFileds) apartment = fields.Integer(strict=True, required=True, validate=isPositiveNumber) name = fields.Str(required=True, validate=validateName) birth_date = fields.DateTime(strict=True, required=True, format='%d.%m.%Y', validate=isPastDate) gender = fields.Str(required=True, validate=OneOf(['male','female'])) relatives = fields.List(fields.Integer(), required=True) class Meta: strict = True unknown = RAISE
class ConditionalExpressionSchema(ExecutionElementBaseSchema): """Schema for conditional expressions """ conditions = fields.Nested(ConditionSchema, many=True) child_expressions = fields.Nested('self', many=True) operator = field_for(ConditionalExpression, 'operator', default='and', validates=OneOf(*valid_operators)) is_negated = field_for(ConditionalExpression, 'is_negated', default=False) class Meta: model = ConditionalExpression excludes = ('parent', )
class HostUpdateSchema(Schema): description = fields.Str(validate=Length(max=1000)) owners = fields.List(fields.Str(validate=Length(max=200))) principal = fields.List( fields.Str(required=False, validate=Length(max=100))) duration = fields.Int(required=False, validate=Range(min=1, max=500)) durationUnit = fields.Str(required=False, validate=OneOf(['Hours', 'Days', 'Weeks'])) sshPublicCerts = fields.List(fields.Nested(sshPublicCertSchema), required=False) class Meta: fields = ("owners", "description", "sshPublicCerts", "principal", "duration", "durationUnit")
def _load_ghe_options(env: Env): global GHE_HOST, GHE_PROTO, GHE_API_URL, GHE_API_SPEC with env.prefixed("GHE_"): GHE_API_SPEC = env.str( "API_SPEC", "api.github.com", validate=OneOf(specifications.keys(), error="GHE_API_SPEC must be one of: {choices}"), ) if GHE_API_SPEC == "api.github.com": GHE_PROTO = "https" GHE_HOST = "github.com" GHE_API_URL = urlparse("https://api.github.com") else: GHE_PROTO = env.str( "PROTO", "https", validate=OneOf(["http", "https"], error="GHE_PROTO must be one of: {choices}"), ) GHE_HOST = env.str("HOST") GHE_API_URL = env.url("GHE_API_URL", f"{GHE_PROTO}://{GHE_HOST}/api/v3")
class StatusSchema(marshmallow.Schema): slug = marshmallow.fields.String( example='open', description='the slug represents the type of status. ' 'Statuses are open, closed-validated, closed-invalidated, closed-deprecated' # nopep8 ) global_status = marshmallow.fields.String( example='open', description='global_status: open, closed', validate=OneOf([status.value for status in GlobalStatus]), ) label = marshmallow.fields.String(example='Open') fa_icon = marshmallow.fields.String(example='fa-check') hexcolor = marshmallow.fields.String(example='#0000FF')
class UserSchema(BaseSchema): username = EmqString(required=True) email = EmqEmail(required=True) password = EmqString(required=True, len_min=6, len_max=100, load_only=True) enable = EmqInteger(allow_none=True) nickname = EmqString(allow_none=True) phone = EmqString(allow_none=True, len_max=15) department = EmqString(allow_none=True) lastRequestTime = EmqDateTime(allow_none=True) loginTime = EmqDateTime(allow_none=True) expiresAt = EmqDateTime(allow_none=True) roleIntID = EmqInteger(allow_none=True) userAuthType = EmqInteger(allow_none=True, validate=OneOf([1, 2])) groups = EmqList(allow_none=True, list_type=str, load_only=True) tenantID = EmqString(dump_only=True) @validates('username') def validate_username(self, value): if value in current_app.config.get('RESERVED'): raise FormInvalid(field='username') @validates('email') def email_is_exist(self, value): try: split_email = value.split('@')[0] if split_email in current_app.config.get('RESERVED'): raise FormInvalid(field='email') except Exception: raise FormInvalid(field='email') if db.session.query(User.email).filter_by(email=value).first(): raise DataExisted(field='email') @post_load def handle_user_auth_type(self, data): if 'user_id' not in g: data['userAuthType'] = 1 groups_uid = self.get_request_data(key='groups') auth_type = data.get('userAuthType') if auth_type not in (1, 2): raise FormInvalid(field='userAuthType') if auth_type == 2 and groups_uid: groups = Group.query \ .filter_tenant(tenant_uid=g.tenant_uid) \ .filter(Group.groupID.in_(set(groups_uid))).all() if len(groups_uid) != len(groups): raise DataNotFound(field='groups') data['groups'] = groups else: data.pop('groups', None) return data
class PulseQobjConfigSchema(QobjConfigSchema): """Schema for PulseQobjConfig of device backend.""" # Required properties. meas_level = Integer(required=True, validate=Range(min=0, max=2)) meas_return = String(required=True, validate=OneOf(choices=(MeasReturnType.AVERAGE, MeasReturnType.SINGLE))) pulse_library = Nested(PulseLibraryItemSchema, required=True, many=True) qubit_lo_freq = List(Number(validate=Range(min=0)), required=True) meas_lo_freq = List(Number(validate=Range(min=0)), required=True) # Optional properties. memory_slot_size = Integer(validate=Range(min=1)) rep_time = Integer(validate=Range(min=0))
class ProtocolDescriptorSchema(Schema): """Schema for an entry in the protocols list.""" pid = fields.Str(required=True) roles = fields.List( fields.Str( description="Role: requester or responder", example="requester", validate=OneOf(["requester", "responder"]), ), required=False, allow_none=True, description="List of roles", )
class SearchImageForm(Schema): """Define the schema to search for images on Remote Providers.""" satsen = fields.String(required=True, allow_none=False) start = fields.Date(required=True, allow_none=False) end = fields.Date(required=True, allow_none=False) tags = fields.List(fields.String, required=True, allow_none=False) cloud = fields.Float(default=100, allow_nan=False) action = fields.String(required=True, validate=OneOf(['preview', 'start'])) w = fields.Float(allow_none=False, allow_nan=False) s = fields.Float(allow_none=False, allow_nan=False) e = fields.Float(allow_none=False, allow_nan=False) n = fields.Float(allow_none=False, allow_nan=False) scenes = fields.List(fields.String(), allow_none=False) @post_load def pre_load_dates(self, data, **kwargs) -> dict: """Format the parsed data and serialize the 'start' and 'end' as 'Y-m-d' string.""" if 'start' in data: data['start'] = data['start'].strftime('%Y-%m-%d') if 'end' in data: data['end'] = data['end'].strftime('%Y-%m-%d') return data @validates_schema def validate_scenes(self, data, **kwargs): """Validate the search image form. Ensure that bounding box given (w, s, e, n) and scenes is not set in the same context. Raises: ValidationError When both scenes and bounding box given. It also raise error when bbox is inconsistent. """ bbox_given = data.keys() >= {'w', 's', 'e', 'n'} if 'scenes' in data and bbox_given: raise ValidationError( '"scenes" and bbox ("w", "s", "e", "n") given. Please refer one of those.' ) if bbox_given: w, s, e, n = data['w'], data['s'], data['e'], data['n'] if w > e: raise ValidationError('Xmin is greater than XMax') if s > n: raise ValidationError('Ymin is greater than YMax')
class IAXSchema(BaseSchema): id = fields.Integer(dump_only=True) tenant_uuid = fields.String(dump_only=True) name = fields.String(validate=Regexp(NAME_REGEX)) type = fields.String(validate=OneOf(['friend', 'peer', 'user'])) host = fields.String(validate=Length(max=255)) options = fields.List( fields.List(fields.String(), validate=Length(equal=2))) links = ListLink(Link('endpoint_iax')) trunk = Nested('TrunkSchema', only=['id', 'links'], dump_only=True, attribute='trunk_rel')
class BundleSchema(Schema): """ All Bundles should be shaped like this :) """ device_uuid = fields.UUID(required=True) sensor_type = fields.Str( validate=OneOf(['temperature', 'humidity']), required=True, ) sensor_value = fields.Decimal( validate=Range(min=0.0, max=100.0), required=True, ) sensor_reading_time = fields.Integer(required=True)
class DummyQuerySchema(Schema): """ Dummy query useful for testing. """ # query_kind parameter is required here for claims validation query_kind = fields.String(validate=OneOf(["dummy_query"])) dummy_param = fields.String(required=True) aggregation_unit = AggregationUnit() dummy_delay = fields.Integer(missing=0, required=False) @post_load def make_query_object(self, params, **kwargs): return DummyQueryExposed(**params)
class WatchdogSchema(Schema): """ The schema for deserializing 'watchdog' endpoint (POST - JSON). """ watchdog = fields.Str(required=True, error_messages={'required': 'Watchdog field is required.'}, validate=OneOf(choices=['start', 'stop'], error='Not valid data. Use start or stop.')) @pre_load def validate_type(self, data, **kwargs): if not data or not isinstance(data, dict): raise ValidationError('Invalid input type.', 'services') return data
class JoinedSpatialAggregateSchema(Schema): # query_kind parameter is required here for claims validation query_kind = fields.String(validate=OneOf(["joined_spatial_aggregate"])) locations = fields.Nested(InputToSpatialAggregate, required=True) metric = fields.Nested(JoinableMetrics, required=True) method = fields.String( validate=OneOf(JoinedSpatialAggregate.allowed_methods)) @pre_load def validate_method(self, data, **kwargs): continuous_metrics = [ "radius_of_gyration", "unique_location_counts", "topup_balance", "subscriber_degree", "topup_amount", "event_count", "nocturnal_events", "pareto_interactions", "displacement", ] categorical_metrics = ["handset"] if data["metric"]["query_kind"] in continuous_metrics: validate = OneOf( ["avg", "max", "min", "median", "mode", "stddev", "variance"]) elif data["metric"]["query_kind"] in categorical_metrics: validate = OneOf(["distr"]) else: raise ValidationError( f"{data['metric']['query_kind']} does not have a valid metric type." ) validate(data["method"]) return data @post_load def make_query_object(self, params, **kwargs): return JoinedSpatialAggregateExposed(**params)
class AttributePrivacyItemResponseSchema(BaseSchema): """ JSON serialization schema """ id = fields.Integer(required=True) attribute_name = fields.String(required=True) data_type = fields.String(required=False, allow_none=True, validate=[OneOf(list(DataType.__dict__.keys()))]) privacy_type = fields.String( required=True, validate=[OneOf(list(PrivacyType.__dict__.keys()))]) category_technique = fields.String(required=False, allow_none=True) anonymization_technique = fields.String( required=True, validate=[OneOf(list(AnonymizationTechnique.__dict__.keys()))]) hierarchical_structure_type = fields.String(required=False, allow_none=True) privacy_model_technique = fields.String(required=False, allow_none=True) hierarchy = fields.String(required=False, allow_none=True) category_model = fields.String(required=False, allow_none=True) privacy_model = fields.String(required=False, allow_none=True) privacy_model_parameters = fields.String(required=False, allow_none=True) unlock_privacy_key = fields.String(required=False, allow_none=True) is_global_law = fields.Boolean(required=False, allow_none=True, missing=False, default=False) attribute_privacy_group_id = fields.Integer(required=False, allow_none=True) # noinspection PyUnresolvedReferences @post_load def make_object(self, data, **kwargs): """ Deserialize data into an instance of AttributePrivacy""" return AttributePrivacy(**data) class Meta: ordered = True unknown = EXCLUDE
class AlarmRequestSchema(SOSAccessSchema): """ Schema for dumping and loading a AlarmRequest """ __envelope__ = 'alarmrequest' __model__ = AlarmRequest reference = marshmallow.fields.String(allow_none=True, validate=[Length(min=1, max=50)]) authentication = marshmallow.fields.String(required=True, validate=[Length(equal=15)]) receiver = marshmallow.fields.String(required=True, validate=[Length(min=1, max=20)]) transmitter_time = marshmallow.fields.DateTime(allow_none=True, data_key='transmittertime') alarm_type = marshmallow.fields.String(allow_none=True, validate=[OneOf(ALARM_TYPES)], data_key='alarmtype') transmitter_type = marshmallow.fields.String(required=True, validate=[Length(equal=5)], data_key='transmittertype') transmitter_code = marshmallow.fields.String(required=True, validate=[ Length(min=1, max=15)], data_key='transmittercode') transmitter_area = marshmallow.fields.String(allow_none=True, validate=[ Length(min=1, max=5)], data_key='transmitterarea') event_code = marshmallow.fields.String(required=True, validate=[Length(min=1, max=25)], data_key='eventcode') section = marshmallow.fields.String(allow_none=True, validate=[Length(min=1, max=5)], data_key='section') section_text = marshmallow.fields.String(allow_none=True, validate=[Length(min=1, max=40)], data_key='sectiontext') detector = marshmallow.fields.String(allow_none=True, validate=[Length(min=1, max=5)]) detector_text = marshmallow.fields.String(allow_none=True, validate=[Length(min=1, max=40)], data_key='detectortext') # Lines in additionalinfo is separated via CR+LF or LF. CR = 0x0a LF = 0x0d additional_info = marshmallow.fields.String(allow_none=True, validate=[ Length(min=1, max=2000)], data_key='additionalinfo', load_only=True) additional_info_text = marshmallow.fields.String(allow_none=True, validate=[ Length(min=1, max=2000)], data_key='additionalinfo', dump_only=True) position = marshmallow.fields.Nested(PositionSchema, allow_none=True) class Meta: ordered = True
class JobCreateRequestSchema(BaseSchema): """ JSON serialization schema """ name = fields.String(required=False, allow_none=True) type = fields.String(required=False, allow_none=True, missing=JobType.NORMAL, default=JobType.NORMAL, validate=[OneOf(list(JobType.__dict__.keys()))]) exception_stack = fields.String(required=False, allow_none=True) job_key = fields.String(required=True) workflow = fields.Nested( 'stand.schema.WorkflowDefinitionCreateRequestSchema', required=True) cluster = fields.Nested('stand.schema.ClusterIdCreateRequestSchema', required=True) user = fields.Nested('stand.schema.UserCreateRequestSchema', required=True) # noinspection PyUnresolvedReferences @post_load def make_object(self, data, **kwargs): """ Deserialize data into an instance of Job""" data['cluster_id'] = data['cluster']['id'] data['user_id'] = data['user']['id'] data['user_name'] = data['user']['name'] data['user_login'] = data['user']['login'] data['workflow_id'] = data['workflow']['id'] data['workflow_name'] = data['workflow']['name'] data['workflow_definition'] = json.dumps(data['workflow']) now = datetime.datetime.now() data['steps'] = [ JobStep(date=now, status=StatusExecution.PENDING, task_id=t['id'], task_name=t.get('name'), operation_id=t['operation']['id'], operation_name="EMPTY") for t in data['workflow'].get('tasks', []) ] data.pop('cluster') data.pop('workflow') data.pop('user') return Job(**data) class Meta: ordered = True unknown = EXCLUDE
class BaseDestinationSchema(Schema): type = fields.String( validate=OneOf( [ 'agent', 'bsfilter', 'conference', 'custom', 'forward', 'group', 'groupmember', 'onlinerec', 'paging', 'park_position', 'parking', 'queue', 'service', 'transfer', 'user', ] ), required=True, ) href = fields.String(default=None, allow_none=True, dump_only=True) @validates('type') def exclude_destination(self, data): if data in self.context.get('exclude_destination', []): raise ValidationError('The "{}" funckey are excluded'.format(data), 'type') def get_parameters(self): parameters = [] exclude_fields = ['href', 'type'] for field_name, field_obj in self.declared_fields.items(): if ( field_name is None or field_name in exclude_fields or field_obj.dump_only ): continue parameter = {'name': field_name} if isinstance(field_obj.validate, OneOf): parameter['values'] = field_obj.validate.choices if getattr(self, 'endpoint_list', False): parameter['collection'] = url_for(self.endpoint_list, _external=True) parameters.append(parameter) return parameters
class HASchema(BaseSchema): node_type = fields.String(validate=OneOf(['disabled', 'master', 'slave']), required=True) remote_address = fields.IP() @validates_schema def check_remote_if_enabled(self, data, **kwargs): if 'node_type' not in data: return if data['node_type'] == 'disabled': return if not data.get('remote_address'): raise ValidationError('remote_address cannot be empty')
class DigitalOceanProviderReq(BaseProviderReq): # Digital Ocean access token digitalocean_access_token = ma.Str(required=True) # enable backups for droplet digitalocean_backups = ma.Bool(default=False) # Digital Ocean Image digitalocean_image = ma.Str(missing="ubuntu-14-04-x64") # enable ipv6 for droplet digitalocean_ipv6 = ma.Bool(missing=False) # enable private networking for droplet digitalocean_private_networking = ma.Bool(default=False) # Digital Ocean region digitalocean_region = ma.Str( required=True, validate=OneOf(DO_REGION_CHOICES), ) # Digital Ocean size digitalocean_size = ma.Str(validate=OneOf(DO_SIZE_CHOICES), default="4gb") @post_load def finalize_data(self, data): data["driver_attrs"] = { "digitalocean_access_token": data.pop("digitalocean_access_token"), "digitalocean_backups": data.pop("digitalocean_backups"), "digitalocean_private_networking": data.pop("digitalocean_private_networking"), "digitalocean_region": data.pop("digitalocean_region"), "digitalocean_size": data.pop("digitalocean_size"), "digitalocean_image": data.pop("digitalocean_image"), "digitalocean_ipv6": data.pop("digitalocean_ipv6"), } return data
class ExperimentCreateRequestSchema(Schema): """ JSON serialization schema """ date = fields.DateTime(required=True) type = fields.String(required=True, validate=[OneOf(ResultType.__dict__.keys())]) city = fields.Nested('CityCreateRequestSchema', required=True) # noinspection PyUnresolvedReferences @post_load def make_object(self, data): """ Deserialize data into an instance of Experiment""" return Experiment(**data) class Meta: ordered = True
class AwsProviderReq(BaseProviderReq): amazonec2_access_key = ma.Str(required=True) amazonec2_secret_key = ma.Str(required=True) amazonec2_region = ma.Str(required=True, validate=OneOf(AWS_REGION_CHOICES)) amazonec2_instance_type = ma.Str(validate=OneOf(AWS_INSTANCE_TYPE_CHOICES), default="m4.large") amazonec2_private_address_only = ma.Bool(default=False) @post_load def finalize_data(self, data): data["driver_attrs"] = { "amazonec2_access_key": data.pop("amazonec2_access_key"), "amazonec2_secret_key": data.pop("amazonec2_secret_key"), "amazonec2_region": data.pop("amazonec2_region"), "amazonec2_instance_type": data.pop("amazonec2_instance_type"), "amazonec2_private_address_only": data.pop("amazonec2_private_address_only"), } return data
class CDRListRequestSchema(Schema): from_ = fields.DateTime(load_from='from', attribute='start', missing=None) until = fields.DateTime(attribute='end', missing=None) direction = fields.String(validate=OneOf(['asc', 'desc']), missing='desc') order = fields.String( validate=OneOf(set(CDRSchema().fields) - {'end', 'tags'}), missing='start') limit = fields.Integer(validate=Range(min=0), missing=None) offset = fields.Integer(validate=Range(min=0), 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') class Meta(object): strict = True @pre_load def convert_tags_and_user_uuid_to_list(self, data): 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 @post_load def map_order_field(self, in_data): mapped_order = CDRSchema().fields[in_data['order']].attribute if mapped_order: in_data['order'] = mapped_order
class ActionRequest(Schema): """ Marshmallow schema for incoming ZMQ requests. Deserialises a ZeroMQ JSON message into a namedtuple """ action = fields.String(required=True, validate=OneOf(ACTION_HANDLERS.keys())) request_id = fields.String(required=True) params = fields.Dict(required=False, missing={}) @post_load def make_action(self, data, **kwargs): return namedtuple("Action", self.fields.keys())(**data)
def __init__(self, **kwargs): days_of_week = [ "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday", ] super().__init__( keys=fields.String(validate=OneOf(days_of_week)), values=fields.Float(validate=Range(min=-1.0, max=1.0)), **kwargs, )
def validate_method(self, data, **kwargs): continuous_metrics = [ "radius_of_gyration", "unique_location_counts", "topup_balance", "subscriber_degree", "topup_amount", "event_count", "nocturnal_events", "pareto_interactions", "displacement", ] categorical_metrics = ["handset"] if data["metric"]["query_kind"] in continuous_metrics: validate = OneOf( ["avg", "max", "min", "median", "mode", "stddev", "variance"]) elif data["metric"]["query_kind"] in categorical_metrics: validate = OneOf(["distr"]) else: raise ValidationError( f"{data['metric']['query_kind']} does not have a valid metric type." ) validate(data["method"]) return data
class SetUserProfileSchema(marshmallow.Schema): """ Schema used for setting user profile. This schema is for write access only """ profile = marshmallow.fields.String( attribute='profile', validate=OneOf(Profile._NAME), example='trusted-users', description=FIELD_PROFILE_DESC, ) @post_load def create_user_profile(self, data: typing.Dict[str, typing.Any]) -> object: return UserProfile(**data)
class DeploymentLogCreateRequestSchema(Schema): """ JSON serialization schema """ date = fields.DateTime(required=True, missing=datetime.datetime.utcnow) status = fields.String(required=True, validate=[OneOf(list(DeploymentStatus.__dict__.keys()))]) log = fields.String(required=True) # noinspection PyUnresolvedReferences @post_load def make_object(self, data): """ Deserialize data into an instance of DeploymentLog""" return DeploymentLog(**data) class Meta: ordered = True