class Output(Schema): val = fields.Integer() created = fields.DateTime() modified1 = fields.DateTime() modified2 = fields.DateTime() user = Neo4jSchema( User, fields=( "uuid", "email", "name", "surname", "is_active", "last_password_change", ), ) group1 = Neo4jSchema(Group, fields="*") group2 = Neo4jSchema(Group, fields=("*", )) group3 = Neo4jSchema(Group, fields=["*"]) group4 = Neo4jSchema(Group, fields=[]) group5 = Neo4jSchema(Group, fields=["fullname", "shortname"]) group6 = Neo4jSchema(Group, fields="") group7 = Neo4jSchema(Group, fields=None) choices1 = Neo4jChoice(CHOICES_tuple) choices2 = Neo4jChoice(CHOICES_dict)
def build_schema(self, model: Type[Any]) -> None: # Get the full list of parent classes from model to object classes = inspect.getmro(model) starting_point = False # Iterate in reversed order to start from object for c in reversed(classes): # Skip all parentes up to StructuredNode and StructuredRel (included) if not starting_point: # Found the starting point, next class will be descended up to model if c == StructuredNode or c == StructuredRel: starting_point = True # skip all parent up to StructuredNode and StructuredRel INCLUDED continue # Iterate all class attributes to find neomodel properties for attribute in c.__dict__: prop = getattr(c, attribute) if not isinstance(prop, properties.Property): continue # self.fields can be None when the special value * is given in input if self.fields and attribute not in self.fields: continue # log.info("Including property {}.{}", model.__name__, attribute) if isinstance(prop, properties.StringProperty): if prop.choices is None: self.declared_fields[attribute] = fields.Str() else: self.declared_fields[attribute] = fields.Neo4jChoice( prop.choices) elif isinstance(prop, properties.BooleanProperty): self.declared_fields[attribute] = fields.Boolean() elif isinstance(prop, properties.IntegerProperty): self.declared_fields[attribute] = fields.Integer() elif isinstance(prop, properties.FloatProperty): self.declared_fields[attribute] = fields.Float() elif isinstance(prop, properties.EmailProperty): # This is because Nested is not typed on marshmallow self.declared_fields[attribute] = fields.Email( ) # type: ignore elif isinstance(prop, properties.DateTimeProperty): self.declared_fields[attribute] = fields.AwareDateTime() elif isinstance(prop, properties.DateProperty): self.declared_fields[attribute] = fields.Date() elif isinstance(prop, properties.UniqueIdProperty): self.declared_fields[attribute] = fields.Str() else: # pragma: no cover log.error( "Unsupport neomodel property: {}, fallback to StringProperty", prop.__class__.__name__, ) self.declared_fields[attribute] = fields.Str()
class PrivateStatsOutput(Schema): num_users = fields.Integer(required=False) num_studies = fields.Integer(required=False) num_datasets = fields.Integer(required=False) num_datasets_per_group = fields.Dict(required=False, keys=fields.Str(), values=fields.Integer()) num_datasets_with_vcf = fields.Integer(required=False) num_datasets_with_vcf_per_group = fields.Dict(required=False, keys=fields.Str(), values=fields.Integer()) num_datasets_with_gvcf = fields.Integer(required=False) num_datasets_with_gvcf_per_group = fields.Dict(required=False, keys=fields.Str(), values=fields.Integer()) num_files = fields.Integer(required=False)
class PhenotypeOutputSchema(Schema): uuid = fields.Str(required=True) name = fields.Str(required=True) age = fields.Integer() sex = fields.Str(required=True, validate=validate.OneOf(SEX)) hpo = fields.List(fields.Nested(Hpo), required=False) birth_place = fields.Nested(GeoData, required=False) relationships = fields.Nested( Relationships, metadata={"description": "family relationships between phenotypes"}, )
def getInputSchema(request: FlaskRequest, is_post: bool) -> Type[Schema]: graph = neo4j.get_instance() # as defined in Marshmallow.schema.from_dict attributes: Dict[str, Union[fields.Field, type]] = {} attributes["name"] = fields.Str(required=True) attributes["age"] = fields.Integer(allow_none=True, validate=validate.Range(min=0)) attributes["sex"] = fields.Str( required=True, validate=validate.OneOf(SEX), metadata={"description": ""} ) attributes["hpo"] = fields.List( fields.Str(), metadata={ "label": "HPO", "autocomplete_endpoint": "/api/hpo", "autocomplete_show_id": True, "autocomplete_id_bind": "hpo_id", "autocomplete_label_bind": "label", }, ) geodata_keys = [] geodata_labels = [] for g in graph.GeoData.nodes.all(): geodata_keys.append(g.uuid) geodata_labels.append(g.province) if len(geodata_keys) == 1: default_geodata = geodata_keys[0] else: default_geodata = None attributes["birth_place"] = fields.Str( required=False, allow_none=True, metadata={ "label": "Birth Place", "description": "", }, dump_default=default_geodata, validate=validate.OneOf(choices=geodata_keys, labels=geodata_labels), ) return Schema.from_dict(attributes, name="PhenotypeDefinition")
class FileOutput(Schema): uuid = fields.Str(required=True) name = fields.Str(required=True) type = fields.Str(required=False) size = fields.Integer(required=False) status = fields.Str(required=False)
def test_responses(self, faker: Faker) -> None: class MySchema(Schema): name = fields.Str() f = "myfield" assert ( ResponseMaker.get_schema_type(f, fields.Str(metadata={"password": True})) == "password" ) assert ResponseMaker.get_schema_type(f, fields.Bool()) == "boolean" assert ResponseMaker.get_schema_type(f, fields.Boolean()) == "boolean" assert ResponseMaker.get_schema_type(f, fields.Date()) == "date" assert ResponseMaker.get_schema_type(f, fields.DateTime()) == "datetime" assert ResponseMaker.get_schema_type(f, fields.AwareDateTime()) == "datetime" assert ResponseMaker.get_schema_type(f, fields.NaiveDateTime()) == "datetime" assert ResponseMaker.get_schema_type(f, fields.Decimal()) == "number" # This is because Email is not typed on marshmallow assert ResponseMaker.get_schema_type(f, fields.Email()) == "email" # type: ignore assert ResponseMaker.get_schema_type(f, fields.Float()) == "number" assert ResponseMaker.get_schema_type(f, fields.Int()) == "int" assert ResponseMaker.get_schema_type(f, fields.Integer()) == "int" assert ResponseMaker.get_schema_type(f, fields.Number()) == "number" assert ResponseMaker.get_schema_type(f, fields.Str()) == "string" assert ResponseMaker.get_schema_type(f, fields.String()) == "string" assert ResponseMaker.get_schema_type(f, fields.Dict()) == "dictionary" assert ResponseMaker.get_schema_type(f, fields.List(fields.Str())) == "string[]" assert ResponseMaker.get_schema_type(f, fields.Nested(MySchema())) == "nested" # Unsupported types, fallback to string assert ResponseMaker.get_schema_type(f, fields.URL()) == "string" assert ResponseMaker.get_schema_type(f, fields.Url()) == "string" assert ResponseMaker.get_schema_type(f, fields.UUID()) == "string" # assert ResponseMaker.get_schema_type(f, fields.Constant("x")) == "string" assert ResponseMaker.get_schema_type(f, fields.Field()) == "string" # assert ResponseMaker.get_schema_type(f, fields.Function()) == "string" # assert ResponseMaker.get_schema_type(f, fields.Mapping()) == "string" # assert ResponseMaker.get_schema_type(f, fields.Method()) == "string" # assert ResponseMaker.get_schema_type(f, fields.Raw()) == "string" # assert ResponseMaker.get_schema_type(f, fields.TimeDelta()) == "string" assert not ResponseMaker.is_binary(None) assert not ResponseMaker.is_binary("") assert not ResponseMaker.is_binary("application/json") assert ResponseMaker.is_binary("application/octet-stream") assert ResponseMaker.is_binary("application/x-bzip") assert ResponseMaker.is_binary("application/x-bzip2") assert ResponseMaker.is_binary("application/pdf") assert ResponseMaker.is_binary("application/msword") assert ResponseMaker.is_binary("application/rtf") assert ResponseMaker.is_binary("application/x-tar") assert ResponseMaker.is_binary("application/gzip") assert ResponseMaker.is_binary("application/zip") assert ResponseMaker.is_binary("application/x-7z-compressed") assert not ResponseMaker.is_binary("text/plain") assert not ResponseMaker.is_binary("text/css") assert not ResponseMaker.is_binary("text/csv") assert not ResponseMaker.is_binary("text/html") assert not ResponseMaker.is_binary("text/javascript") assert not ResponseMaker.is_binary("text/xml") assert ResponseMaker.is_binary("image/gif") assert ResponseMaker.is_binary("image/jpeg") assert ResponseMaker.is_binary("image/png") assert ResponseMaker.is_binary("image/svg+xml") assert ResponseMaker.is_binary("image/tiff") assert ResponseMaker.is_binary("image/webp") assert ResponseMaker.is_binary("image/bmp") assert ResponseMaker.is_binary("image/aac") assert ResponseMaker.is_binary("audio/midi") assert ResponseMaker.is_binary("audio/mpeg") assert ResponseMaker.is_binary("audio/wav") assert ResponseMaker.is_binary("audio/anyother") assert ResponseMaker.is_binary("video/mpeg") assert ResponseMaker.is_binary("video/ogg") assert ResponseMaker.is_binary("video/webm") assert ResponseMaker.is_binary("video/anyother") assert ResponseMaker.is_binary("video/anyother") assert not ResponseMaker.is_binary(faker.pystr()) response = EndpointResource.response("", code=200) assert response[1] == 200 # type: ignore response = EndpointResource.response(None, code=200) assert response[1] == 204 # type: ignore response = EndpointResource.response(None, code=200, head_method=True) assert response[1] == 200 # type: ignore
class PublicStatsOutput(Schema): num_users = fields.Integer(required=False) num_studies = fields.Integer(required=False) num_datasets = fields.Integer(required=False) num_datasets_with_vcf = fields.Integer(required=False) num_files = fields.Integer(required=False)