This repository has been archived by the owner on Apr 30, 2019. It is now read-only.
forked from mwcschool/ecommerce_api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schemas.py
53 lines (39 loc) · 2.09 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
from marshmallow import Schema, fields, validate
def check_empty_str(value):
if str(value).strip() == '':
return False
class ItemSchema(Schema):
uuid = fields.UUID(dump_only=True)
name = fields.Str(required=True, validate=check_empty_str)
price = fields.Decimal(required=True, validate=validate.Range(min=0))
description = fields.Str(required=True, validate=check_empty_str)
category = fields.Str(required=True, validate=check_empty_str)
availability = fields.Int(required=True, validate=validate.Range(min=0))
class UserSchema(Schema):
uuid = fields.UUID(dump_only=True)
first_name = fields.Str(required=True, validate=check_empty_str)
last_name = fields.Str(required=True, validate=check_empty_str)
email = fields.Str(required=True, validate=check_empty_str)
password = fields.Str(required=True, validate=check_empty_str, load_only=True)
status = fields.Str(validate=check_empty_str, dump_only=True)
class AddressSchema(Schema):
uuid = fields.UUID(dump_only=True)
user = fields.UUID(attribute='user.uuid', dump_only=True)
nation = fields.Str(required=True, validate=validate.Length(min=1))
city = fields.Str(required=True, validate=validate.Length(min=1))
postal_code = fields.Str(required=True, validate=validate.Length(min=1))
local_address = fields.Str(required=True, validate=validate.Length(min=1))
phone = fields.Str(required=True, validate=validate.Length(min=1))
class OrderItemSchema(Schema):
item = fields.UUID(required=True, dump_only=True, attribute='item.uuid')
quantity = fields.Integer(as_string=False)
subtotal = fields.Decimal(required=True, dump_only=True)
class OrderSchema(Schema):
uuid = fields.UUID(required=True, dump_only=True)
total_price = fields.Decimal(required=True)
user = fields.Nested(UserSchema, only=["uuid"])
items = fields.Nested(OrderItemSchema, many=True, attribute='order_items')
class FavoritesSchema(Schema):
uuid = fields.UUID(dump_only=True)
user = fields.UUID(required=True, attribute='user.uuid')
item = fields.UUID(required=True, attribute='item.uuid')