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 68
/
Copy pathbase.py
153 lines (116 loc) · 3.57 KB
/
base.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
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
from hashlib import md5
from faker import Factory
from marshmallow import validate
from marshmallow_jsonapi import Schema, fields
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 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