This repository was archived by the owner on Jun 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathbase.py
More file actions
224 lines (162 loc) · 5.18 KB
/
base.py
File metadata and controls
224 lines (162 loc) · 5.18 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
from hashlib import md5
from faker import Factory
from marshmallow import validate
from marshmallow_jsonapi import Schema, fields
from marshmallow_oneofschema import OneOfSchema
fake = Factory.create()
class Bunch:
def __init__(self, **kwargs):
for key, val in kwargs.items():
setattr(self, key, val)
class Post(Bunch):
pass
class Author(Bunch):
pass
class Comment(Bunch):
pass
class Keyword(Bunch):
pass
class User(Bunch):
pass
class PaymentMethod(Bunch):
"""Base Class for Payment Methods"""
__jsonapi_type__ = "payment-methods"
class PaymentMethodCreditCard(PaymentMethod, Bunch):
__jsonapi_type__ = "payment-methods-cc"
class PaymentMethodPaypal(PaymentMethod, Bunch):
__jsonapi_type__ = "payment-methods-paypal"
class PaymentMethodCreditCardSchema(Schema):
id = fields.Str()
last_4 = fields.Str()
class Meta:
type_ = "payment-methods-cc"
class PaymentMethodPaypalSchema(Schema):
id = fields.Str()
linked_email = fields.Str()
class Meta:
type_ = "payment-methods-paypal"
class PaymentMethodSchema(Schema, OneOfSchema):
"""Using https://github.com/marshmallow-code/marshmallow-oneofschema is one way to have a polymorphic schema"""
id = fields.Str()
type_schemas = {
"payment-methods-cc": PaymentMethodCreditCardSchema,
"payment-methods-paypal": PaymentMethodPaypalSchema,
}
def get_obj_type(self, obj):
if isinstance(obj, PaymentMethod):
return obj.__jsonapi_type__
else:
raise Exception(f"Unknown object type: {obj.__class__.__name__}")
class Meta:
type_ = "payment-methods"
class UserSchema(Schema):
id = fields.Str()
first_name = fields.Str(required=True)
last_name = fields.Str(required=True)
payment_methods = fields.Relationship(
schema=PaymentMethodSchema,
include_resource_linkage=True,
many=True,
type_="payment-methods",
)
class Meta:
type_ = "users"
class AuthorSchema(Schema):
id = fields.Str()
first_name = fields.Str(required=True)
last_name = fields.Str(required=True)
password = fields.Str(load_only=True, validate=validate.Length(6))
twitter = fields.Str()
def get_top_level_links(self, data, many):
if many:
self_link = "/authors/"
else:
self_link = "/authors/{}".format(data["id"])
return {"self": self_link}
class Meta:
type_ = "people"
class KeywordSchema(Schema):
id = fields.Str()
keyword = fields.Str(required=True)
def get_attribute(self, attr, obj, default):
if obj == "id":
return md5(
super(Schema, self)
.get_attribute(attr, "keyword", default)
.encode("utf-8")
).hexdigest()
else:
return super(Schema, self).get_attribute(attr, obj, default)
class Meta:
type_ = "keywords"
strict = True
class CommentSchema(Schema):
id = fields.Str()
body = fields.Str(required=True)
author = fields.Relationship(
"http://test.test/comments/{id}/author/",
related_url_kwargs={"id": "<id>"},
schema=AuthorSchema,
many=False,
)
class Meta:
type_ = "comments"
strict = True
class ArticleSchema(Schema):
id = fields.Integer()
body = fields.String()
author = fields.Relationship(
dump_only=False, include_resource_linkage=True, many=False, type_="people"
)
comments = fields.Relationship(
dump_only=False, include_resource_linkage=True, many=True, type_="comments"
)
class Meta:
type_ = "articles"
strict = True
class PostSchema(Schema):
id = fields.Str()
post_title = fields.Str(attribute="title", dump_to="title", data_key="title")
author = fields.Relationship(
"http://test.test/posts/{id}/author/",
related_url_kwargs={"id": "<id>"},
schema=AuthorSchema,
many=False,
type_="people",
)
post_comments = fields.Relationship(
"http://test.test/posts/{id}/comments/",
related_url_kwargs={"id": "<id>"},
attribute="comments",
load_from="post-comments",
dump_to="post-comments",
data_key="post-comments",
schema="CommentSchema",
many=True,
type_="comments",
)
post_keywords = fields.Relationship(
"http://test.test/posts/{id}/keywords/",
related_url_kwargs={"id": "<id>"},
attribute="keywords",
dump_to="post-keywords",
data_key="post-keywords",
schema="KeywordSchema",
many=True,
type_="keywords",
)
class Meta:
type_ = "posts"
strict = True
class PolygonSchema(Schema):
id = fields.Integer(as_string=True)
sides = fields.Integer()
# This is an attribute that uses the 'meta' key: /data/attributes/meta
meta = fields.String()
# This is the document's top level meta object: /meta
document_meta = fields.DocumentMeta()
# This is the resource object's meta object: /data/meta
resource_meta = fields.ResourceMeta()
class Meta:
type_ = "shapes"
strict = True