-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathserializers.py
More file actions
322 lines (239 loc) · 11 KB
/
serializers.py
File metadata and controls
322 lines (239 loc) · 11 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
"""
Classes to serialize the RESTful representation of Deis API models.
"""
import json
import re
from django.contrib.auth.models import User
from django.utils import timezone
from rest_framework import serializers
from api import models
PROCTYPE_MATCH = re.compile(r'^(?P<type>[a-z]+)')
MEMLIMIT_MATCH = re.compile(r'^(?P<mem>[0-9]+(MB|KB|GB|[BKMG]))$', re.IGNORECASE)
CPUSHARE_MATCH = re.compile(r'^(?P<cpu>[0-9.]+[m]{0,1})$')
TAGKEY_MATCH = re.compile(r'^[a-z]+$')
TAGVAL_MATCH = re.compile(r'^\w+$')
CONFIGKEY_MATCH = re.compile(r'^[a-z_]+[a-z0-9_]*$', re.IGNORECASE)
class JSONFieldSerializer(serializers.JSONField):
def __init__(self, *args, **kwargs):
super(JSONFieldSerializer, self).__init__(*args, **kwargs)
def to_internal_value(self, data):
"""Deserialize the field's JSON data, for write operations."""
try:
val = json.loads(data)
except TypeError:
val = data
return val
def to_representation(self, obj):
"""Serialize the field's JSON data, for read operations."""
for k, v in obj.items():
if v is None: # NoneType is used to unset a value
continue
try:
obj[k] = str(v)
except ValueError:
obj[k] = v
# Do nothing, the validator will catch this later
return obj
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ['email', 'username', 'password', 'first_name', 'last_name', 'is_superuser',
'is_staff', 'groups', 'user_permissions', 'last_login', 'date_joined',
'is_active']
read_only_fields = ['is_superuser', 'is_staff', 'groups',
'user_permissions', 'last_login', 'date_joined', 'is_active']
extra_kwargs = {'password': {'write_only': True}}
def create(self, validated_data):
now = timezone.now()
user = User(
email=validated_data.get('email'),
username=validated_data.get('username'),
last_login=now,
date_joined=now,
is_active=True
)
if validated_data.get('first_name'):
user.first_name = validated_data['first_name']
if validated_data.get('last_name'):
user.last_name = validated_data['last_name']
user.set_password(validated_data['password'])
# Make the first signup an admin / superuser
if not User.objects.filter(is_superuser=True).exists():
user.is_superuser = user.is_staff = True
user.save()
return user
class AdminUserSerializer(serializers.ModelSerializer):
"""Serialize admin status for a User model."""
class Meta:
model = User
fields = ['username', 'is_superuser']
read_only_fields = ['username']
class AppSerializer(serializers.ModelSerializer):
"""Serialize a :class:`~api.models.App` model."""
owner = serializers.ReadOnlyField(source='owner.username')
structure = serializers.JSONField(required=False)
class Meta:
"""Metadata options for a :class:`AppSerializer`."""
model = models.App
fields = ['uuid', 'id', 'owner', 'structure', 'created', 'updated']
class BuildSerializer(serializers.ModelSerializer):
"""Serialize a :class:`~api.models.Build` model."""
app = serializers.SlugRelatedField(slug_field='id', queryset=models.App.objects.all())
owner = serializers.ReadOnlyField(source='owner.username')
procfile = serializers.JSONField(required=False)
class Meta:
"""Metadata options for a :class:`BuildSerializer`."""
model = models.Build
fields = ['owner', 'app', 'image', 'sha', 'procfile', 'dockerfile', 'created',
'updated', 'uuid']
class ConfigSerializer(serializers.ModelSerializer):
"""Serialize a :class:`~api.models.Config` model."""
app = serializers.SlugRelatedField(slug_field='id', queryset=models.App.objects.all())
owner = serializers.ReadOnlyField(source='owner.username')
values = JSONFieldSerializer(required=False, binary=True)
memory = JSONFieldSerializer(required=False, binary=True)
cpu = JSONFieldSerializer(required=False, binary=True)
tags = JSONFieldSerializer(required=False, binary=True)
class Meta:
"""Metadata options for a :class:`ConfigSerializer`."""
model = models.Config
def validate_values(self, data):
for key, value in data.items():
if not re.match(CONFIGKEY_MATCH, key):
raise serializers.ValidationError(
"Config keys must start with a letter or underscore and "
"only contain [A-z0-9_]")
return data
def validate_memory(self, data):
for key, value in data.items():
if value is None: # use NoneType to unset an item
continue
if not re.match(PROCTYPE_MATCH, key):
raise serializers.ValidationError("Process types can only contain [a-z]")
if not re.match(MEMLIMIT_MATCH, str(value)):
raise serializers.ValidationError(
"Limit format: <number><unit>, where unit = B, K, M or G")
return data
def validate_cpu(self, data):
for key, value in data.items():
if value is None: # use NoneType to unset an item
continue
if not re.match(PROCTYPE_MATCH, key):
raise serializers.ValidationError("Process types can only contain [a-z]")
shares = re.match(CPUSHARE_MATCH, str(value))
if not shares:
raise serializers.ValidationError("CPU shares must be a numeric value")
for share in shares.groupdict().values():
try:
if share[-1] == "m":
float(share[:-1])
else:
float(share)
except ValueError:
raise serializers.ValidationError("CPU units must be a numeric value")
return data
def validate_tags(self, data):
for key, value in data.items():
if value is None: # use NoneType to unset an item
continue
if not re.match(TAGKEY_MATCH, key):
raise serializers.ValidationError("Tag keys can only contain [a-z]")
if not re.match(TAGVAL_MATCH, str(value)):
raise serializers.ValidationError("Invalid tag data")
return data
class ReleaseSerializer(serializers.ModelSerializer):
"""Serialize a :class:`~api.models.Release` model."""
app = serializers.SlugRelatedField(slug_field='id', queryset=models.App.objects.all())
owner = serializers.ReadOnlyField(source='owner.username')
class Meta:
"""Metadata options for a :class:`ReleaseSerializer`."""
model = models.Release
class ContainerSerializer(serializers.ModelSerializer):
"""Serialize a :class:`~api.models.Container` model."""
app = serializers.SlugRelatedField(slug_field='id', queryset=models.App.objects.all())
owner = serializers.ReadOnlyField(source='owner.username')
release = serializers.SerializerMethodField()
class Meta:
"""Metadata options for a :class:`ContainerSerializer`."""
model = models.Container
fields = ['owner', 'app', 'release', 'type', 'num', 'state', 'created', 'updated', 'uuid']
def get_release(self, obj):
return "v{}".format(obj.release.version)
class KeySerializer(serializers.ModelSerializer):
"""Serialize a :class:`~api.models.Key` model."""
owner = serializers.ReadOnlyField(source='owner.username')
class Meta:
"""Metadata options for a KeySerializer."""
model = models.Key
class DomainSerializer(serializers.ModelSerializer):
"""Serialize a :class:`~api.models.Domain` model."""
app = serializers.SlugRelatedField(slug_field='id', queryset=models.App.objects.all())
owner = serializers.ReadOnlyField(source='owner.username')
class Meta:
"""Metadata options for a :class:`DomainSerializer`."""
model = models.Domain
fields = ['owner', 'created', 'updated', 'app', 'domain']
read_only_fields = ['uuid']
def validate_domain(self, value):
"""
Check that the hostname is valid
"""
if len(value) > 255:
raise serializers.ValidationError('Hostname must be 255 characters or less.')
if value[-1:] == ".":
value = value[:-1] # strip exactly one dot from the right, if present
labels = value.split('.')
if 'xip.io' in value:
return value
# Let wildcards through by not trying to validate it
if labels[0] == '*':
labels.pop(0)
if len(labels) == 0:
raise serializers.ValidationError("Hostname can't only be a wildcard")
# TODO this doesn't support IDN domains
allowed = re.compile("^(?!-)[a-z0-9-]{1,63}(?<!-)$", re.IGNORECASE)
for label in labels:
match = allowed.match(label)
if not match or '--' in label or label.isdigit() or \
len(labels) == 1 and any(char.isdigit() for char in label):
raise serializers.ValidationError('Hostname does not look valid.')
if models.Domain.objects.filter(domain=value).exists():
raise serializers.ValidationError(
"The domain {} is already in use by another app".format(value))
return value
class CertificateSerializer(serializers.ModelSerializer):
"""Serialize a :class:`~api.models.Cert` model."""
owner = serializers.ReadOnlyField(source='owner.username')
san = serializers.ListField(
child=serializers.CharField(allow_blank=True, allow_null=True, required=False),
required=False
)
domains = serializers.ListField(
child=serializers.CharField(allow_blank=True, allow_null=True, required=False),
required=False, default=[]
)
class Meta:
"""Metadata options for CertificateSerializer."""
model = models.Certificate
extra_kwargs = {
'certificate': {'write_only': True},
'key': {'write_only': True}
}
read_only_fields = ['common_name', 'fingerprint', 'san', 'domains', 'subject', 'issuer']
class PushSerializer(serializers.ModelSerializer):
"""Serialize a :class:`~api.models.Push` model."""
app = serializers.SlugRelatedField(slug_field='id', queryset=models.App.objects.all())
owner = serializers.ReadOnlyField(source='owner.username')
class Meta:
"""Metadata options for a :class:`PushSerializer`."""
model = models.Push
fields = ['owner', 'app', 'sha', 'fingerprint', 'receive_user', 'receive_repo',
'ssh_connection', 'ssh_original_command', 'created', 'updated']
class PodSerializer(serializers.BaseSerializer):
name = serializers.CharField()
state = serializers.CharField()
type = serializers.CharField()
release = serializers.CharField()
started = serializers.DateTimeField()
def to_representation(self, obj):
return obj