def admin_user_output(many: bool = True) -> Schema: attributes: MarshmallowSchema = {} attributes["uuid"] = fields.UUID() # This is because Email is not typed on marshmallow attributes["email"] = fields.Email() # type: ignore attributes["name"] = fields.Str() attributes["surname"] = fields.Str() attributes["first_login"] = fields.DateTime(allow_none=True, format=ISO8601UTC) attributes["last_login"] = fields.DateTime(allow_none=True, format=ISO8601UTC) attributes["last_password_change"] = fields.DateTime(allow_none=True, format=ISO8601UTC) attributes["is_active"] = fields.Boolean() attributes["privacy_accepted"] = fields.Boolean() attributes["roles"] = fields.List(fields.Nested(Role)) attributes["expiration"] = fields.DateTime(allow_none=True, format=ISO8601UTC) if Connector.authentication_service == "neo4j": attributes["belongs_to"] = fields.Neo4jRelationshipToSingle( Group, data_key="group") else: attributes["belongs_to"] = fields.Nested(Group, data_key="group") if custom_fields := mem.customizer.get_custom_output_fields(None): attributes.update(custom_fields)
class LoginsSchema(Schema): # This is because Email is not typed on marshmallow username = fields.Email() # type: ignore date = fields.DateTime(format=ISO8601UTC) IP = fields.Str() location = fields.Str() failed = fields.Boolean() flushed = fields.Boolean()
class Pagination(PartialSchema): get_total = fields.Boolean( required=False, description="Request the total number of elements" ) page = fields.Int( required=False, description="Current page number", validate=validate.Range(min=1), ) size = fields.Int( required=False, description="Number of elements to retrieve", validate=validate.Range(min=1, max=100), ) sort_order = fields.Str( validate=validate.OneOf(["asc", "desc"]), required=False, missing="asc" ) sort_by = fields.Str(required=False, missing=None) input_filter = fields.Str(required=False, missing=None) @post_load def verify_parameters(self, data, **kwargs): if "get_total" in data: data["page"] = None data["size"] = None else: data.setdefault("get_total", False) data.setdefault("page", 1) data.setdefault("size", 20) return data
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()
def profile_patch_input() -> Schema: attributes: MarshmallowSchema = {} attributes["name"] = fields.Str(metadata={"label": "First Name"}) attributes["surname"] = fields.Str(metadata={"label": "Last Name"}) attributes["privacy_accepted"] = fields.Boolean() if custom_fields := mem.customizer.get_custom_input_fields( request=None, scope=mem.customizer.PROFILE): attributes.update(custom_fields)
def patchUserProfile(): # as defined in Marshmallow.schema.from_dict attributes: Dict[str, Union[fields.Field, type]] = {} attributes["name"] = fields.Str() attributes["surname"] = fields.Str() attributes["privacy_accepted"] = fields.Boolean() if custom_fields := mem.customizer.get_custom_input_fields( request=None, scope=mem.customizer.PROFILE ): attributes.update(custom_fields)
def profile_output() -> Schema: attributes: MarshmallowSchema = {} attributes["uuid"] = fields.UUID(required=True) # This is because Email is not typed on marshmallow attributes["email"] = fields.Email(required=True) # type: ignore attributes["name"] = fields.Str(required=True) attributes["surname"] = fields.Str(required=True) attributes["isAdmin"] = fields.Boolean(required=True) attributes["isStaff"] = fields.Boolean(required=True) attributes["isCoordinator"] = fields.Boolean(required=True) attributes["privacy_accepted"] = fields.Boolean(required=True) attributes["is_active"] = fields.Boolean(required=True) attributes["expiration"] = fields.DateTime(allow_none=True, format=ISO8601UTC) attributes["roles"] = fields.Dict(required=True) attributes["last_password_change"] = fields.DateTime(required=True, format=ISO8601UTC) attributes["first_login"] = fields.DateTime(required=True, format=ISO8601UTC) attributes["last_login"] = fields.DateTime(required=True, format=ISO8601UTC) if Connector.authentication_service == "neo4j": attributes["belongs_to"] = fields.Neo4jRelationshipToSingle( Group, data_key="group") else: attributes["belongs_to"] = fields.Nested(Group, data_key="group") attributes["two_factor_enabled"] = fields.Boolean(required=True) if custom_fields := mem.customizer.get_custom_output_fields(None): attributes.update(custom_fields)
def getProfileData(): # as defined in Marshmallow.schema.from_dict attributes: Dict[str, Union[fields.Field, type]] = {} attributes["uuid"] = fields.UUID(required=True) attributes["email"] = fields.Email(required=True) attributes["name"] = fields.Str(required=True) attributes["surname"] = fields.Str(required=True) attributes["isAdmin"] = fields.Boolean(required=True) attributes["isStaff"] = fields.Boolean(required=True) attributes["isCoordinator"] = fields.Boolean(required=True) attributes["privacy_accepted"] = fields.Boolean(required=True) attributes["is_active"] = fields.Boolean(required=True) attributes["expiration"] = fields.DateTime(allow_none=True, format=ISO8601UTC) attributes["roles"] = fields.Dict(required=True) attributes["last_password_change"] = fields.DateTime( required=True, format=ISO8601UTC ) attributes["first_login"] = fields.DateTime(required=True, format=ISO8601UTC) attributes["last_login"] = fields.DateTime(required=True, format=ISO8601UTC) attributes["group"] = fields.Nested(Group) attributes["two_factor_enabled"] = fields.Boolean(required=True) if custom_fields := mem.customizer.get_custom_output_fields(None): attributes.update(custom_fields)
def get_output_schema(): # as defined in Marshmallow.schema.from_dict attributes: Dict[str, Union[fields.Field, type]] = {} attributes["uuid"] = fields.UUID() attributes["email"] = fields.Email() attributes["name"] = fields.Str() attributes["surname"] = fields.Str() attributes["first_login"] = fields.DateTime(allow_none=True, format=ISO8601UTC) attributes["last_login"] = fields.DateTime(allow_none=True, format=ISO8601UTC) attributes["last_password_change"] = fields.DateTime(allow_none=True, format=ISO8601UTC) attributes["is_active"] = fields.Boolean() attributes["privacy_accepted"] = fields.Boolean() attributes["roles"] = fields.List(fields.Nested(Roles)) attributes["expiration"] = fields.DateTime(allow_none=True, format=ISO8601UTC) attributes["belongs_to"] = fields.Nested(Group, data_key="group") if custom_fields := mem.customizer.get_custom_output_fields(None): attributes.update(custom_fields)
class MailInput(Schema): subject = fields.Str(required=True, metadata={"description": "Subject of your email"}) body = fields.Str( required=True, validate=validate.Length(max=9999), metadata={ "description": "Body of your email. You can use html code here." }, ) # This is because Email is not typed on marshmallow to = fields.Email( # type: ignore required=True, metadata={"label": "Destination email address"}) cc = fields.DelimitedList( # This is because Email is not typed on marshmallow fields.Email(), # type: ignore metadata={ "label": "CC - Carbon Copy", "description": "CC email addresses (comma-delimited list)", }, ) bcc = fields.DelimitedList( # This is because Email is not typed on marshmallow fields.Email(), # type: ignore metadata={ "label": "BCC - Blind Carbon Copy", "description": "BCC email addresses (comma-delimited list)", }, ) dry_run = fields.Boolean( required=True, metadata={ "label": "Dry run execution", "description": "Only simulate the email, do not send it", }, )
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