-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserializers.py
91 lines (74 loc) · 2.88 KB
/
serializers.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
"""
Serializers for flowspec models
"""
from rest_framework import serializers
from flowspec.models import (
Route, MatchPort, ThenAction, FragmentType, MatchProtocol, MatchDscp)
from flowspec.validators import (
clean_source, clean_destination, clean_expires, clean_status)
class RouteSerializer(serializers.HyperlinkedModelSerializer):
"""
A serializer for `Route` objects
"""
applier = serializers.CharField(source='applier_username', read_only=True)
def validate_source(self, attrs, source):
user = self.context.get('request').user
source_ip = attrs.get('source')
res = clean_source(user, source_ip)
if res != source_ip:
raise serializers.ValidationError(res)
return attrs
def validate_destination(self, attrs, source):
user = self.context.get('request').user
destination = attrs.get('destination')
res = clean_destination(user, destination)
if res != destination:
raise serializers.ValidationError(res)
return attrs
def validate_expires(self, attrs, source):
expires = attrs.get('expires')
res = clean_expires(expires)
if res != expires:
raise serializers.ValidationError(res)
return attrs
def validate_status(self, attrs, source):
status = attrs.get('status')
res = clean_status(status)
if res != status:
raise serializers.ValidationError(res)
return attrs
class Meta:
model = Route
fields = (
'name', 'id', 'comments', 'applier', 'source', 'sourceport',
'destination', 'destinationport', 'port', 'dscp', 'fragmenttype',
'icmpcode', 'packetlength', 'protocol', 'tcpflag', 'then', 'filed',
'last_updated', 'status', 'expires', 'response', 'comments',
'requesters_address')
read_only_fields = (
'requesters_address', 'response', 'last_updated', 'id', 'filed')
class PortSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = MatchPort
fields = ('id', 'port', )
read_only_fields = ('id', )
class ThenActionSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = ThenAction
fields = ('id', 'action', 'action_value')
read_only_fields = ('id', )
class FragmentTypeSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = FragmentType
fields = ('id', 'fragmenttype', )
read_only_fields = ('id', )
class MatchProtocolSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = MatchProtocol
fields = ('id', 'protocol', )
read_only_fields = ('id', )
class MatchDscpSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = MatchDscp
fields = ('id', 'dscp', )
read_only_fields = ('id', )