-
Notifications
You must be signed in to change notification settings - Fork 0
/
schemas.py
63 lines (46 loc) · 1.98 KB
/
schemas.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Marshmellow is a tool that enables data validation using Schemas
from marshmallow import Schema, fields
# Config an PlainItemSchema class that will dictate how an item should look like
# in its simplest form
class PlainItemSchema(Schema):
# dump_only means this field will only be returned
# it not required as a parameter in request
id = fields.Int(dump_only=True)
# required field in the request
name = fields.Str(required=True)
price = fields.Float(required=True)
class PlainStoreSchema(Schema):
id = fields.Int(dump_only=True)
name = fields.Str(required=True)
class PlainTagSchema(Schema):
id = fields.Int(dump_only=True)
name = fields.Str()
# This schema will inherit from PlainItemSchema
# and add additional fields related to store of an item
# Store metadata will exists in a nested field that will be populated by PlainStoreSchema
class ItemSchema(PlainItemSchema):
store_id = fields.Int(required=True, load_only=True)
store = fields.Nested(PlainStoreSchema(), dump_only=True)
tags = fields.List(fields.Nested(PlainTagSchema()), dump_only=True)
class ItemUpdateSchema(Schema):
# These fields are optional
name = fields.Str()
price = fields.Float()
store_id = fields.Int()
class StoreSchema(PlainStoreSchema):
items = fields.List(fields.Nested(PlainItemSchema()), dump_only=True)
tags = fields.List(fields.Nested(PlainTagSchema()), dump_only=True)
class TagSchema(PlainTagSchema):
store_id = fields.Int(load_only=True)
store = fields.Nested(PlainStoreSchema(), dump_only=True)
items = fields.List(fields.Nested(PlainItemSchema()), dump_only=True)
class TagAndItemSchema(Schema):
message = fields.Str()
item = fields.Nested(ItemSchema)
tag = fields.Nested(TagSchema)
class UserSchema(Schema):
id = fields.Int(dump_only=True)
username = fields.Str(required=True)
password = fields.Str(required=True)
class UserRegisterSchema(UserSchema):
email = fields.Str(required=True)