-
Notifications
You must be signed in to change notification settings - Fork 72
/
attribute.py
109 lines (75 loc) · 3.58 KB
/
attribute.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
import re
from marshmallow import Schema, ValidationError, fields, pre_load, validate, validates
class AttributeKeySchema(Schema):
key = fields.Str(required=True, allow_none=False)
@pre_load
def sanitize_key(self, params, **kwargs):
params = dict(params)
if params.get("key"):
params["key"] = params["key"].lower().strip()
return params
@validates("key")
def validate_key(self, value):
if not re.match("^[A-Za-z0-9_-]{1,32}$", value):
raise ValidationError(
"Key should contain max 32 chars and include only letters, "
"digits, underscores and dashes"
)
class AttributeValueSchema(Schema):
value = fields.Raw(required=True, allow_none=False)
@validates("value")
def validate_value(self, value):
# 0 and False are not empty values
if value in ["", {}, [], None]:
raise ValidationError("Value shouldn't be empty")
class AttributeListRequestSchema(Schema):
hidden = fields.Boolean(missing=False)
class AttributeItemRequestSchema(AttributeKeySchema, AttributeValueSchema):
pass
class AttributeDefinitionListRequestSchema(Schema):
access = fields.Str(
validate=validate.OneOf(["read", "set", "manage"]), missing="read"
)
class AttributeDefinitionCreateRequestSchema(AttributeKeySchema):
label = fields.Str(required=True, allow_none=False)
description = fields.Str(required=True, allow_none=False)
url_template = fields.Str(missing="", allow_none=False)
rich_template = fields.Str(missing="", allow_none=False)
example_value = fields.Str(missing="", allow_none=False)
hidden = fields.Boolean(required=True, allow_none=False)
class AttributeDefinitionUpdateRequestSchema(Schema):
label = fields.Str(missing=None)
description = fields.Str(missing=None)
url_template = fields.Str(missing=None)
rich_template = fields.Str(missing=None)
example_value = fields.Str(missing=None)
hidden = fields.Boolean(missing=None)
class AttributePermissionUpdateRequestSchema(Schema):
group_name = fields.Str(required=True, allow_none=False)
can_read = fields.Boolean(required=True, allow_none=False)
can_set = fields.Boolean(required=True, allow_none=False)
class AttributePermissionDeleteRequestSchema(Schema):
group_name = fields.Str(required=True, allow_none=False)
class AttributeItemResponseSchema(AttributeKeySchema, AttributeValueSchema):
id = fields.Integer(required=True, allow_none=False)
class AttributeListResponseSchema(Schema):
attributes = fields.Nested(AttributeItemResponseSchema, many=True)
class AttributeDefinitionItemResponseSchema(AttributeKeySchema):
url_template = fields.Str(required=True, allow_none=False)
rich_template = fields.Str(required=True, allow_none=False)
example_value = fields.Str(required=True, allow_none=False)
label = fields.Str(required=True, allow_none=False)
description = fields.Str(required=True, allow_none=False)
hidden = fields.Boolean(required=True, allow_none=False)
class AttributeDefinitionListResponseSchema(Schema):
attribute_definitions = fields.Nested(
AttributeDefinitionItemResponseSchema, many=True
)
class AttributePermissionItemResponseSchema(Schema):
group_name = fields.Str(required=True, allow_none=False)
can_read = fields.Boolean(required=True, allow_none=False)
can_set = fields.Boolean(required=True, allow_none=False)
class AttributePermissionListResponseSchema(Schema):
attribute_permissions = fields.Nested(
AttributePermissionItemResponseSchema, many=True
)