class UserSchema(ma.SQLAlchemyAutoSchema): name = String(required=True) email = Email(required=True) posts = Nested("PostSchema", many=True) password = String(load_only=True, required=True) @validates_schema(skip_on_field_errors=True) def hashing_password(self, data, **kwargs): print(data.get("password")) print(data) hashed_pass = password_hash(data.get("password")) data.update(password=hashed_pass) class Meta: model = User load_instance = True
class Algorithm_Catalog_Schema(Base_Schema): """Represents an algorithm in the catalog.""" doc = Algorithm_Catalog_Document id = Str(required=True, example='ddos-prediction', description='Id of the algorithm in the catalog.') parameters = Nested(Algorithm_Catalog_Parameter_Schema, unknown='INCLUDE', many=True, description='Parameter properties.', validate=Unique_List.apply('id'), error_messages=Unique_List.error_messages) encoding_scheme = Str( default='base64', example='base64', description='Encoding scheme used to store the binary data') description = Str(example='Predict DDoS attacks.', description='Short description of the algorithm.')
class LongSquareFeatures(Schema): v_baseline = Float() rheobase_i = Float() fi_fit_slope = Float() sag = Float() vm_for_sag = Float() input_resistance = Float() sweeps = Nested(SweepFeatures, many=True) tau = Float() rheobase_sweep = Nested(SweepFeatures) spiking_sweeps = Nested(SweepFeatures, many=True) hero_sweep = Nested(SweepFeatures) subthreshold_sweeps = Nested(SweepFeatures, many=True) subthreshold_membrane_property_sweeps = Nested(SweepFeatures, many=True)
class DocumentSchema(Schema, DatedSchema): id = String(dump_only=True) collection_id = Integer(dump_only=True, required=True) schema = SchemaName(dump_only=True) schemata = List(SchemaName(), dump_only=True) status = String(dump_only=True) type = String(dump_only=True) foreign_id = String() content_hash = String(dump_only=True) parent = Nested(DocumentReference()) uploader_id = Integer(dump_only=True) error_message = String(dump_only=True) # title = String(validate=Length(min=2, max=5000), missing=None) title = String() summary = String() countries = List(Country(), missing=[]) languages = List(Language(), missing=[]) keywords = List(String(validate=Length(min=1, max=5000)), missing=[]) dates = List(PartialDate(), dump_only=True) file_name = String() file_size = Integer(dump_only=True) author = String() mime_type = String() extension = String(dump_only=True) encoding = String(dump_only=True) source_url = String() pdf_version = String(dump_only=True) text = String(dump_only=True) html = String(dump_only=True) columns = List(String(), dump_only=True) children = Boolean(dump_to='$children', attribute='$children', dump_only=True) @post_dump def transient(self, data): data['$uri'] = url_for('documents_api.view', document_id=data.get('id')) data['$ui'] = document_url(data.get('id')) collection_id = data.get('collection_id') data['$writeable'] = request.authz.can_write(collection_id) return data
class NotificationSchema(BaseSchema): SCHEMATA = { Alert: AlertSchema, Role: RoleSchema, Document: CombinedSchema, Entity: CombinedSchema, Collection: CollectionSchema } actor_id = String() event = Nested(EventSchema(), dump_only=True) params = Raw() def _resolve_alerts(self, cache): alerts = set() for (type_, id_) in cache.keys(): if type_ == Alert: alerts.add(id_) if not len(alerts): return for alert in Alert.all_by_ids(alerts, deleted=True): cache[(Alert, str(alert.id))] = role @pre_dump(pass_many=True) def expand(self, objs, many=False): cache = {} for obj in ensure_list(objs): for name, clazz, value in obj.iterparams(): cache[(clazz, str(value))] = None self._resolve_alerts(cache) self._resolve_roles(cache) self._resolve_index(cache) for obj in ensure_list(objs): params = {} for name, clazz, value in obj.iterparams(): schema = self.SCHEMATA.get(clazz) value = cache.get((clazz, str(value))) if value is not None: params[name], _ = schema().dump(value) obj.params = params
class TriagemSchema(Schema): cpf = Str( required=True, error_messages={'required': MSG_FIELD_REQUIRED} ) doctor = Str( required=True, error_messages={'required': MSG_FIELD_REQUIRED} ) main_complaint = Str( required=True, error_messages={'required': MSG_FIELD_REQUIRED} ) attendance_date = Str( required=True, error_messages={'required': MSG_FIELD_REQUIRED} ) crm = Int( required=True, error_messages={'required': MSG_FIELD_REQUIRED} ) manchester_classification = Int( required=True, error_messages={'required': MSG_FIELD_REQUIRED} ) sympton = List(Nested(SymptonSchema))
class FieldCal(AfterglowSchema): """ Field calibration prescription """ id: int = Integer() name: str = String() catalog_sources: TList[CatalogSource] = List(Nested(CatalogSource)) catalogs: TList[str] = List(String()) custom_filter_lookup: TDict[str, TDict[str, str]] = Dict(keys=String, values=Dict(keys=String, values=String)) source_inclusion_percent: float = Float() min_snr: float = Float() max_snr: float = Float() source_match_tol: float = Float() variable_check_tol: float = Float() max_star_rms: float = Float() max_stars: int = Integer()
class UserPersonalDataSchema(mm.SQLAlchemyAutoSchema): title = NoneValueEnumField(UserTitle, none_value=UserTitle.none, attribute='_title') email = String(dump_only=True) synced_fields = List(String(validate=validate.OneOf(syncable_fields))) affiliation_data = Nested(_AffiliationDataSchema, attribute='_affiliation') class Meta: model = User # XXX: this schema is also used for updating a user's personal data, so the fields here must # under no circumstances include sensitive fields that should not be modifiable by a user! fields = ('title', 'first_name', 'last_name', 'email', 'affiliation', 'affiliation_data', 'address', 'phone', 'synced_fields') @pre_load def wrap_plain_affiliation(self, data, **kwargs): if (affiliation := data.pop('affiliation', None)) is not None: data['affiliation_data'] = {'id': None, 'text': affiliation} return data
class BillSummarySchema(ModelSchema): place = String(required=True, validate=not_blank) billed_at = Date(required=True) wallet_uid = UUID(required=True, allow_none=False) items = Nested( BillItemSchema, many=True, only=("uid", "name", "quantity", "value", "group_uid"), ) total = Decimal(as_string=True, required=False, places=2, allow_none=True) @pre_dump def make_dict(self, obj): return dict( uid=obj.uid, place=obj.place, billed_at=_to_date(obj.billed_at), wallet_uid=obj.wallet_uid, total=obj.total, )
class AbstractReviewSchema(mm.ModelSchema): track = Nested(track_schema_basic) user = Nested(UserSchema) proposed_related_abstract = Nested('AbstractSchema', only=_basic_abstract_fields) proposed_contrib_type = Nested(contribution_type_schema_basic, attribute='proposed_contribution_type') proposed_tracks = Nested(track_schema_basic, many=True) ratings = Nested(AbstractReviewRatingSchema, many=True) class Meta: model = AbstractReview fields = ('id', 'track', 'user', 'comment', 'created_dt', 'modified_dt', 'proposed_action', 'proposed_contrib_type', 'proposed_related_abstract', 'proposed_tracks', 'ratings')
class NotificationSchema(BaseSchema): SCHEMATA = { Alert: AlertSchema, Role: RoleSchema, Document: CombinedSchema, Entity: CombinedSchema, Collection: CollectionSchema } actor_id = String() event = Nested(EventSchema(), dump_only=True) params = Raw() @pre_dump(pass_many=True) def expand(self, objs, many=False): cache = {} for obj in ensure_list(objs): for name, clazz, value in obj.iterparams(): cache[(clazz, str(value))] = None self._resolve_alerts(cache) self._resolve_roles(cache) self._resolve_entities(cache) self._resolve_collections(cache) results = [] for obj in ensure_list(objs): params = {} for name, clazz, value in obj.iterparams(): schema = self.SCHEMATA.get(clazz) value = cache.get((clazz, str(value))) if value is not None: params[name], _ = schema().dump(value) results.append({ 'id': obj.id, 'created_at': obj.created_at, 'actor_id': obj.actor_id, 'event': obj.event, 'params': params }) return results
class UserCreateSchema(ModelSchema): email = Email() roles = Nested(UserRoleSchema, attribute='role_assoc', many=True, exclude=('user_id', )) class Meta: model = User fields = ( 'username', 'first_name', 'last_name', 'email', 'password', 'acted_id', 'language_id', 'roles', 'date_joined', 'is_active', )
class LicenseSwitchSchemeGet(LicenseSwitchScheme): package = Nested('PackageSwitchSchemeGet', many=False) class Meta: model = model.LicenseSwitch fields = ('user_email', 'type', 'ip', 'switch_port', 'minute_count', 'amount', 'license_switch_uuid', 'package_switch_uuid', 'user_uuid', 'ordered_amount', 'package', 'enabled', 'start_time', 'end_time', 'duration', 'switch_uuid') search_fields = ('user_email', 'type', 'ip', 'switch_port', 'minute_count', 'amount', 'license_switch_uuid', 'package_switch_uuid', 'user_uuid', 'ordered_amount', 'package', 'enabled', 'duration', 'switch_uuid') query_fields = ( 'start_time_gt', 'start_time_lt', 'end_time_gt', 'end_time_lt', 'cost_gt', 'cost_lt', )
class DatasetSchema(Schema): author = Str() author_email = Str() creator_user_id = UUID() extras = Nested(ExtraSchema, many=True) groups = Nested(CategorySchema, many=True) license_id = Str() license_title = Str() license_url = URL() maintainer = Str() maintainer_email = Str() created = DateTime(data_key='metadata_created') modified = DateTime(data_key='metadata_modified', allow_none=True) slug = Str(data_key='name') notes = Str() num_resources = Int() num_tags = Int() ext_ident = Str(data_key='id', validate=validate.Length(max=36)) isopen = Bool() organization = Nested(OrganizationSchema, many=False) owner_org = UUID() private = Bool() relationships_as_object = Nested(RelationshipObjectSchema, many=True) relationships_as_subject = Nested(RelationshipSubjectSchema, many=True) resources = Nested(ResourceSchema, many=True) revision_id = UUID() status = Str(data_key='state') tags = Nested(TagSchema, many=True) title = Str() type = Str() url = Str() version = Str() class Meta: exclude = [ 'author', 'author_email', 'creator_user_id', 'extras', 'groups', 'license_title', 'license_url', 'maintainer', 'maintainer_email', 'num_resources', 'num_tags', 'isopen', 'owner_org', 'private', 'relationships_as_object', 'relationships_as_subject', 'revision_id', 'type', 'status', 'url', 'version' ] ordered = True unknown = EXCLUDE
class AccessSchema(Schema): """Access schema.""" metadata = SanitizedUnicode(required=True) files = SanitizedUnicode(required=True) embargo = NestedAttribute(EmbargoSchema) status = SanitizedUnicode(dump_only=False) owned_by = List(Nested(Agent)) def validate_protection_value(self, value, field_name): """Check that the protection value is valid.""" if value not in AccessStatusEnum.list(): raise ValidationError( _("'{}' must be either '{}', '{}' or '{}'").format( field_name, *AccessStatusEnum.list(), ), "record", ) @validates("metadata") def validate_record_protection(self, value): """Validate the record protection value.""" self.validate_protection_value(value, "metadata") @validates_schema def validate_embargo(self, data, **kwargs): """Validate that the properties are consistent with each other.""" metadata = data.get("metadata", "") embargo = data.get("embargo", "") if AccessStatusEnum.EMBARGOED.value == metadata and not embargo: raise ValidationError( _("Embargo must be set if metadata is Embargoed"), field_name="embargo", ) @validates("files") def validate_files_protection(self, value): """Validate the files protection value.""" self.validate_protection_value(value, "files")
class DeclarativeInputArgumentItemSchema(Schema): id = Integer(required=True, allow_none=False, dump_only=True) # type: int argument_index = Integer(required=True, allow_none=False, dump_to='argumentIndex', load_from='argumentIndex') # type: int input_type = Nested(ArgumentTypeSchema, required=True, allow_none=False, load_from='inputType', dump_to='inputType') # type: ArgumentType input_value = String(required=True, allow_none=False, dump_to='inputValue', load_from='inputValue') # type: str @post_load() def create_class(self, value): return DeclarativeInputArgumentItem(**value)
class CollectionSchema(BaseSchema): EXPAND = [ ('creator', Role, 'creator', RoleReferenceSchema, False), ] label = String(validate=Length(min=2, max=500), required=True) foreign_id = String() summary = String(allow_none=True) countries = List(Country()) languages = List(Language()) managed = Boolean(missing=False) secret = Boolean(dump_only=True) category = Category(missing=Collection.DEFAULT) creator_id = String(allow_none=True) creator = Nested(RoleReferenceSchema(), dump_only=True) count = Integer(dump_only=True) schemata = Dict(dump_only=True, default={}) @pre_load() def flatten_collection(self, data): flatten_id(data, 'creator_id', 'creator') @pre_dump() def visibility(self, data): if not is_mapping(data): return roles = data.get('roles', []) public = Role.public_roles() data['secret'] = len(public.intersection(roles)) == 0 @post_dump def transient(self, data): pk = str(data.get('id')) data['links'] = { 'self': url_for('collections_api.view', id=pk), 'ui': collection_url(pk) } data['writeable'] = request.authz.can_write(pk) return data
class CVESchema(Schema): id = String(required=True) published = ParsedDateTime(allow_none=True) description = String(allow_none=True) ubuntu_description = String(allow_none=True) notes = List(Nested(Note)) priority = String(allow_none=True) status = String(allow_none=True) cvss3 = Float(allow_none=True) mitigation = String(allow_none=True) references = List(String()) bugs = List(String()) patches = Dict( keys=String(), values=List(String(), required=False), allow_none=True, ) tags = Dict( keys=String(), values=List(String(), required=False), allow_none=True, )
def __new__(mcs, name, bases, attrs): model_attrs = { attr_name: attr for attr_name, attr in attrs.items() if is_model_attribute(attr_name, attr) } model_class = super(ModelMeta, mcs).__new__(mcs, name, bases, model_attrs) schema_base = getattr(model_class, '_schema_class', Schema) # simple attrs schema_attrs = { attr_name: attr for attr_name, attr in attrs.items() if is_schema_attribute(attr_name, attr) } # hooks schema_hooks = { attr_name: attr for attr_name, attr in attrs.items() if is_schema_decorator(attr_name, attr) } schema_attrs.update(schema_hooks) for attr_name, attr in attrs.items(): try: if issubclass(attr, ModelABC): schema_attrs[attr_name] = Nested(attr._schema_class) except TypeError: pass _schema_class = SchemaMeta('%sSchema' % name, (schema_base, ), schema_attrs) model_class._schema_class = _schema_class return model_class
class PackageLrnScheme(BaseModelScheme): package_lrn_uuid = Str(validate=[validate.Length(max=36)]) package_name = Str(validate=[validate.Length(max=64)]) cps = Int() type = Choice() lrn_port = Int() dip_count = Int() amount = Int() enabled = Bool() create_on = DateTime() licenses = Nested('LicenseLrnScheme', many=True) class Meta: model = model.PackageLrn fields = ( 'package_name', 'cps', 'type', 'lrn_port', 'dip_count', 'amount', 'enabled', )
class Algorithm_Instance_Schema(Base_Schema): """Represents an algorithm instance.""" doc = Algorithm_Instance_Document id = Str(required=True, example='ddos-predictor-1', description='Id of the algorithm instance.') algorithm_catalog_id = Str( required=True, readonly=True, example='ddos-predictor', description='Id of the algorithm in the catalog.', validate=In.apply(Algorithm_Catalog_Document.get_ids), error_messages=In.error_messages) operations = Nested(Algorithm_Instance_Operation_Schema, many=True, unknown='INCLUDE', description='List of algorithm instance operations.') description = Str( example='Collect system metrics from execution environments.', description= 'Short description of the algorithm installed in the execution environment.' )
class TestTemplateDtoSchema(Schema): id = fields.Integer(required=False, allow_none=True) name = fields.String() time_limit = Integer(load_from='timeLimit', dump_to='timeLimit', attribute='time_limit', allow_none=True) questions = Nested(TestQuestionTemplateSchema, load_from='questionTemplates', dump_to='questionTemplates', attribute='questions', many=True) is_deleted = Boolean( required=False, allow_none=True, load_from='isDeleted', dump_to='isDeleted', ) @post_load() def create_class(self, value): template = TestTemplate(**value) return template
class Base_Response_Schema(Schema): """Response for the item creation.""" status = Str(required=True, enum=RESPONSE_STATUS, example=RESPONSE_STATUS[0], description='HTTP Status Code phrase.', validate=validate.OneOf(RESPONSE_STATUS)) error = Bool(default=False, example=False, description='Indicate the presence of an error') message = Str( required=True, example='Request not valid: two ids provided.', description= 'Human readable message that describes the status of the operation.') exception = Nested(Exception_Response_Schema, description='Message of the occurred exception.') code = Integer(required=True, enum=RESPONSE_CODES, example=RESPONSE_CODES[0], description='HTTP Status Code.', validate=validate.OneOf(RESPONSE_CODES))
class PolymorphicOrderSelectionSchema(Schema): selected = Nested(FolderOrder, many=True) pc = List(Integer) pl = Boolean() # LOAD @pre_load def pre_load_adapt_polymorphic(self, data): data['pc'] = as_list(data.get('pc', ())) try: data['selected'] = json.loads(data['selected']) except (ValueError, TypeError): data['selected'] = [] return data @post_load def post_load_adapt_polymorphic(self, data): entities = [] pm = orm.class_mapper(Content).polymorphic_map # Polymorphic loading if data['pl']: # Polymorphic children entities = data['pc'] if entities: entities = [pm[i].class_ for i in entities] else: entities = [pm[i].class_ for i in pm] data['entities'] = entities return data
class BlockingSchema(mm.ModelSchema): blocked_rooms = Nested(BlockedRoomSchema, many=True) allowed = PrincipalList() permissions = Method('_get_permissions') created_by = Pluck(UserSchema, 'full_name', attribute='created_by_user') class Meta: model = Blocking fields = ('id', 'start_date', 'end_date', 'reason', 'blocked_rooms', 'allowed', 'created_by', 'permissions') def _get_permissions(self, blocking): methods = ('can_delete', 'can_edit') admin_permissions = None user_permissions = { x: getattr(blocking, x)(session.user, allow_admin=False) for x in methods } if rb_is_admin(session.user): admin_permissions = { x: getattr(blocking, x)(session.user) for x in methods } return {'user': user_permissions, 'admin': admin_permissions}
class Session(AfterglowSchema): """ JSON-serializable Afterglow session class A session is a collection of user's data files. When creating or importing a data file, it is associated with a certain session (by default, if no session ID provided, with the anonymous session that always exists). Sessions are created by the user via the /sessions endpoint. Their main purpose is to provide independent Afterglow UI workspaces; in addition, they may serve as a means to group data files by the client API scripts. Fields: id: unique integer session ID; assigned automatically when creating the session name: unique session name data: arbitrary user data associated with the session data_files: list of data file objects associated with the session """ id: int = Integer(default=None) name: str = String(default=None) data: str = String() data_files: TList[DataFile] = List(Nested(DataFile), default=[], dump_only=True)
class ActionDetailGeneralSchema(Schema): """ General schema for action detail """ class Meta: unknown = EXCLUDE id = Integer(data_key="id") name = Str(data_key="name") place = Str(data_key="place") deadlines = List(DateTime(data_key="deadlines")) organizer = Nested(OrganizerSchema) days = List(Str(data_key="exhibitionDays")) web = Str(data_key="web") type = Integer(data_key="type") @pre_load def load_organizer(self, in_data, **kwargs): in_data["organizer"] = in_data return in_data @post_load def make_action(self, data, **kwargs): return DeClass(_name="ActionDetail", _resp=("id", "name"), **data)
class Agent_Catalog_Parameter_Schema(Schema): """Agent parameter.""" id = Str(required=True, example='log-period', description='Parameter id.') type = Str(required=True, enum=PARAMETER_TYPES, example=PARAMETER_TYPES[0], description='Parameter type.', validate=validate.OneOf(PARAMETER_TYPES)) config = Nested(Agent_Catalog_Parameter_Config_Schema, unknown='INCLUDE', required=True, description='Parameter configuration.') list = Bool( default=False, example=True, description='Indicate if the parameter can have multiple values.') values = List_or_One( Str, example='mysql', description='Possible values if the parameter type is choice.') description = Str(example='Enable the agent.', description='Short description of the parameter.') example = Raw(example='10s', description='Example of parameter value.')
class Agent_Instance_Schema(Base_Schema): """Represents an agent instance installed in an execution environment.""" doc = Agent_Instance_Document id = Str(required=True, example='filebeat@apache', description= 'Id of the agent instance installed in an execution environment.') agent_catalog_id = Str(required=True, readonly=True, example='filebeat', description='Id of the agent in the catalog.', validate=In.apply(Agent_Catalog_Document.get_ids), error_messages=In.error_messages) exec_env_id = Str( required=True, readonly=True, example='apache', description= 'Id of the execution environment where the agent instance is installed.', validate=In.apply(Exec_Env_Document.get_ids), error_messages=In.error_messages) status = Str(enum=AGENT_STATUS, required=True, readonly=True, example=AGENT_STATUS[0], description='Status of the agent.') operations = Nested(Agent_Instance_Operation_Schema, many=True, unknown='INCLUDE', description='List of agent instance operations.') description = Str( example='Collect system metrics from execution environments.', description= 'Short description of the agent installed in the execution environment.' )
class PermissionSchema(BaseSchema): write = Boolean(required=True) read = Boolean(required=True) collection_id = String(dump_only=True) role = Nested(RoleReferenceSchema)