-
Notifications
You must be signed in to change notification settings - Fork 0
/
schemas.py
96 lines (67 loc) · 2.88 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
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
import re
from marshmallow import Schema, fields, validate, validates, ValidationError
from flask_smorest.fields import Upload
ALLOWED_EXTENSIONS = "image/jpeg", "image/png"
class PlainUserSchema(Schema):
id = fields.Int(dump_only=True)
username = fields.Str(required=True, validate=validate.Length(min=3, max=18))
class UserLoginSchema(PlainUserSchema):
password = fields.Str(required=True, load_only=True)
@validates("password")
def validate_password(self, value):
if len(value) < 8:
raise ValidationError("Invalid password. Must be at least 8 characters.")
elif not re.search(r"^[a-zA-Z0-9]*$", value):
raise ValidationError(
"Invalid password. Must consist of only letters and digits."
)
elif not re.search(r"[a-zA-Z]\d|\d[a-zA-Z]", value):
raise ValidationError("Invalid password. Must contain letters and digits.")
class UserRegisterSchema(UserLoginSchema):
confirm_password = fields.Str(required=True, load_only=True)
class PostSchema(Schema):
id = fields.Int(dump_only=True)
user_id = fields.Int(dump_only=True)
author = fields.Str(required=True)
title = fields.Str(required=True)
quote = fields.Str(required=True)
longitude = fields.Float(validate=validate.Range(min=-180, max=180))
latitude = fields.Float(validate=validate.Range(min=-90, max=90))
address = fields.Str(
metadata={
"description": "PicturesqueAPI first tries to locate the address, then the coordinates (if given)"
}
)
filename = fields.Str(dump_only=True)
thumbnail_url = fields.Str(dump_only=True)
photo_url = fields.Str(dump_only=True)
in_collection = fields.List(fields.Nested(PlainUserSchema()), dump_only=True)
class UserSchema(PlainUserSchema):
collection = fields.List(
fields.Nested(PostSchema(only=("id", "author", "title"))), dump_only=True
)
class PostUploadSchema(Schema):
photo = Upload(required=True, load_only=True)
class PostUpdateSchema(PostSchema):
author = fields.Str(required=False)
title = fields.Str(required=False)
quote = fields.Str(required=False)
class PostUpdateUploadSchema(Schema):
photo = Upload(required=False, load_only=True)
class PostSearchSchema(Schema):
q = fields.Str()
class PostResultSchema(Schema):
found_in = fields.Str()
post = fields.Nested(PostSchema(exclude=["in_collection", "photo_url"]))
class PlainAuthorSchema(Schema):
id = fields.Int(dump_only=True)
name = fields.Str(dump_only=True)
class TitleSchema(Schema):
id = fields.Int(dump_only=True)
title = fields.Str(dump_only=True)
author = fields.Nested(PlainAuthorSchema(), dump_only=True)
posts = fields.List(fields.String(dump_only=True))
class AuthorSchema(PlainAuthorSchema):
titles = fields.List(
fields.Nested(TitleSchema(), exclude=["author"]), dump_only=True
)