-
Notifications
You must be signed in to change notification settings - Fork 0
/
schemas.py
61 lines (45 loc) · 1.62 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
from flask_marshmallow import Marshmallow
from flask_marshmallow.fields import fields
from models import ApiItem, Endpoint, Field, Tag
from config import Globals
ma = Marshmallow()
class TagSchema(ma.SQLAlchemyAutoSchema):
class Meta:
model = Tag
tag_schema = TagSchema()
tags_schema = TagSchema(many=True)
class MyBoolean(fields.Field):
def _serialize(self, value, attr, obj, **kwargs):
if value is None:
return ""
if value is True:
return "yes"
return "no"
def _deserialize(self, value, attr, data, **kwargs):
if value not in Globals.ACCEPTED_FIELD_REQUIRED:
raise ValueError('Cannot deserialize')
if value=='yes': return True
return False
class FieldSchema(ma.SQLAlchemyAutoSchema):
class Meta:
model = Field
endpoint_id = ma.auto_field()
required = MyBoolean()
field_schema = FieldSchema()
fields_schema = FieldSchema(many=True)
class ApiSchemaRaw(ma.SQLAlchemyAutoSchema):
class Meta:
model = ApiItem
tags = fields.List(fields.Pluck(TagSchema, "text"), many=True)
class EndpointSchema(ma.SQLAlchemyAutoSchema):
class Meta:
model = Endpoint
tags = fields.List(fields.Pluck(TagSchema, "text"), many=True)
fields_list = fields.List(fields.Nested(FieldSchema, exclude=['endpoint_id']), attribute="fields")
api_item = fields.Nested(ApiSchemaRaw)
endpoint_schema = EndpointSchema()
endpoints_schema = EndpointSchema(many=True)
class ApiSchema(ApiSchemaRaw):
endpoints = fields.List(fields.Nested(EndpointSchema))
api_schema = ApiSchema()
apis_schema = ApiSchema(many=True)