Exemple #1
0
 def test_string_field(self):
     field = fields.String()
     user = User(name=b"foo")
     assert field.serialize("name", user) == "foo"
     field = fields.String(allow_none=True)
     user.name = None
     assert field.serialize("name", user) is None
Exemple #2
0
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)
Exemple #3
0
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()
Exemple #4
0
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 BadSchema(Schema):
            foo = fields.String(data_key="foo-name")

            @validates("foo")
            def validate_string(self, data):
                raise ValidationError("nope")
        class S1(Schema):
            s = fields.String(attribute="string_name")

            @validates("s")
            def validate_string(self, data):
                raise ValidationError("nope")
        class VSchema(Schema):
            s = fields.String()

            @validates("s")
            def validate_string(self, data):
                raise ValidationError("nope")
Exemple #8
0
 class AliasingUserSerializer(Schema):
     name = fields.String()
     birthdate = fields.DateTime(
         default=lambda: dt.datetime(2017, 9, 29))
Exemple #9
0
 def test_string_field_default_to_empty_string(self, user):
     field = fields.String(default="")
     assert field.serialize("notfound", {}) == ""
Exemple #10
0
 class ConfusedDumpToAndAttributeSerializer(Schema):
     name = fields.String(data_key="FullName")
     username = fields.String(attribute="uname", data_key="UserName")
     years = fields.Integer(attribute="le_wild_age", data_key="Years")
Exemple #11
0
 class DumpToSchema(Schema):
     name = fields.String(data_key="NamE")
     years = fields.Integer(data_key="YearS")
Exemple #12
0
class BlogOnlySchema(Schema):
    title = fields.String()
    user = fields.Nested(UserSchema)
    collaborators = fields.Nested(UserSchema, only=("id", ), many=True)
Exemple #13
0
class BlogSchema(Schema):
    title = fields.String()
    user = fields.Nested(UserSchema)
    collaborators = fields.Nested(UserSchema, many=True)
    categories = fields.List(fields.String)
    id = fields.String()