-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschemas.py
More file actions
66 lines (45 loc) · 2.48 KB
/
schemas.py
File metadata and controls
66 lines (45 loc) · 2.48 KB
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
64
65
66
# The schema.py contains the marshmallow structure to validate the request and response messages passed to/by each API endpoint/HTTP method
from marshmallow import Schema, fields
class PlainItemSchema(Schema):
# dump_only=True is used when the field must be part of the response message
id = fields.Int(dump_only=True)
# required=True is used when the field must be part of the request message
name = fields.Str(required=True)
# required=True is used when the field must be part of the request message
price = fields.Float(required=True)
class PlainStoreSchema(Schema):
# dump_only=True is used when the field must be part of the response message. Used to send data back to the client
id = fields.Int(dump_only=True)
# required=True is used when the field must be part of the request message
name = fields.Str(required=True)
class PlainTagSchema(Schema):
# dump_only=True is used when the field must be part of the response message. Used to send data back to the client
id = fields.Int(dump_only=True)
# required=True is used when the field must be part of the request message
name = fields.Str()
class ItemUpdateSchema(PlainItemSchema):
name = fields.Str() # optional field. That's why there is no parameter in parenthesis
price = fields.Float() # optional field. That's why there is no parameter in parenthesis
# optional field. That's why there is no parameter in parenthesis
store_id = fields.Int()
class ItemSchema(PlainItemSchema):
# load_only=True this enable us to pass in the store_id when receiving data from the client
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 StoreSchema(PlainStoreSchema):
items = fields.List(fields.Nested(PlainItemSchema()))
tags = fields.List(fields.Nested(PlainTagSchema()))
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)
# load_only=True is important so the password is never returned
password = fields.Str(required=True, load_only=True)