class KeepOrder(Schema): class Meta: ordered = True name = fields.String(allow_none=True) email = fields.Email(allow_none=True) age = fields.Integer() created = fields.DateTime() id = fields.Integer(allow_none=True) homepage = fields.Url() birthdate = fields.Date()
class UserMetaSchema(Schema): """The equivalent of the UserSchema, using the ``fields`` option.""" uppername = Uppercased(attribute="name", dump_only=True) balance = fields.Decimal() is_old = fields.Method("get_is_old") lowername = fields.Function(get_lowername) updated_local = fields.LocalDateTime(attribute="updated", dump_only=True) species = fields.String(attribute="SPECIES") homepage = fields.Url() email = fields.Email() various_data = fields.Dict() def get_is_old(self, obj): if obj is None: return missing if isinstance(obj, dict): age = obj.get("age") else: age = obj.age try: return age > 80 except TypeError as te: raise ValidationError(str(te)) class Meta: fields = ( "name", "age", "created", "updated", "id", "homepage", "uppername", "email", "balance", "is_old", "lowername", "updated_local", "species", "registered", "hair_colors", "sex_choices", "finger_count", "uid", "time_registered", "birthdate", "since_created", "various_data", )
class UserSchema(Schema): name = fields.String() age = fields.Float() created = fields.DateTime() created_formatted = fields.DateTime(format="%Y-%m-%d", attribute="created", dump_only=True) created_iso = fields.DateTime(format="iso", attribute="created", dump_only=True) updated = fields.DateTime() updated_local = fields.LocalDateTime(attribute="updated", dump_only=True) species = fields.String(attribute="SPECIES") id = fields.String(default="no-id") uppername = Uppercased(attribute="name", dump_only=True) homepage = fields.Url() email = fields.Email() balance = fields.Decimal() is_old = fields.Method("get_is_old") lowername = fields.Function(get_lowername) registered = fields.Boolean() hair_colors = fields.List(fields.Raw) sex_choices = fields.List(fields.Raw) finger_count = fields.Integer() uid = fields.UUID() time_registered = fields.Time() birthdate = fields.Date() activation_date = fields.Date() since_created = fields.TimeDelta() sex = fields.Str(validate=validate.OneOf(["male", "female"])) various_data = fields.Dict() class Meta: render_module = simplejson def get_is_old(self, obj): if obj is None: return missing if isinstance(obj, dict): age = obj.get("age") else: age = obj.age try: return age > 80 except TypeError as te: raise ValidationError(str(te)) @post_load def make_user(self, data): return User(**data)
def test_url_field_serialize_none(self, user): user.homepage = None field = fields.Url() assert field.serialize("homepage", user) is None
class UserRelativeUrlSchema(UserSchema): homepage = fields.Url(relative=True)