-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserializers.py
25 lines (21 loc) · 915 Bytes
/
serializers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from marshmallow import Schema, fields, validate
class BaseSchema(Schema):
id = fields.Int()
email = fields.Email()
phone = fields.Str()
address = fields.Str()
city = fields.Str()
region = fields.Str()
postal_code = fields.Str()
country = fields.Str()
deleted = fields.Boolean()
class ContactSchema(BaseSchema):
first_name = fields.Str(validate=validate.Length(min=1))
last_name = fields.Str(validate=validate.Length(min=1))
organization = fields.Nested(lambda: OrganizationSchema())
organization_id = fields.Function(lambda o: o.organization.id)
name = fields.Function(lambda o: f"{o.first_name} {o.last_name}")
class OrganizationSchema(BaseSchema):
name = fields.Str()
contacts = fields.Function(lambda o:
ContactSchema(many=True, exclude=("organization",)).dump(o.contacts.all()))