forked from openwisp/openwisp-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserializers.py
More file actions
104 lines (89 loc) · 3.18 KB
/
serializers.py
File metadata and controls
104 lines (89 loc) · 3.18 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
from rest_framework import serializers
from swapper import load_model
from openwisp_users.api.mixins import FilterSerializerByOrgManaged
from openwisp_utils.api.serializers import ValidatedModelSerializer
from ...serializers import BaseSerializer
Command = load_model('connection', 'Command')
DeviceConnection = load_model('connection', 'DeviceConnection')
Credentials = load_model('connection', 'Credentials')
Device = load_model('config', 'Device')
class ValidatedDeviceFieldSerializer(ValidatedModelSerializer):
def validate(self, data):
# Add "device_id" to the data for validation
data['device_id'] = self.context['device_id']
return super().validate(data)
class CommandSerializer(ValidatedDeviceFieldSerializer):
input = serializers.JSONField(allow_null=True)
device = serializers.PrimaryKeyRelatedField(
read_only=True, pk_field=serializers.UUIDField(format='hex_verbose')
)
connection = serializers.PrimaryKeyRelatedField(
allow_null=True,
queryset=DeviceConnection.objects.all(),
required=False,
pk_field=serializers.UUIDField(format='hex_verbose'),
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# show only connections and command types available for the device
if device_id := self.context.get('device_id'):
self.fields['connection'].queryset = self.fields[
'connection'
].queryset.filter(device_id=device_id)
device = Device.objects.only('organization_id', 'id').get(pk=device_id)
# filter command types based on the device's organization
self.fields['type'].choices = Command.get_org_allowed_commands(
device.organization_id
)
def to_representation(self, instance):
repr = super().to_representation(instance)
repr['type'] = instance.get_type_display()
repr['input'] = instance.input_data
return repr
class Meta:
model = Command
fields = '__all__'
read_only_fields = [
'device',
'output',
'status',
'created',
'modified',
]
class CredentialSerializer(BaseSerializer):
params = serializers.JSONField()
class Meta:
model = Credentials
fields = (
'id',
'connector',
'name',
'organization',
'auto_add',
'params',
'created',
'modified',
)
read_only_fields = ('created', 'modified')
class DeviceConnectionSerializer(
FilterSerializerByOrgManaged, ValidatedDeviceFieldSerializer
):
class Meta:
model = DeviceConnection
fields = (
'id',
'credentials',
'update_strategy',
'enabled',
'is_working',
'failure_reason',
'last_attempt',
'created',
'modified',
)
extra_kwargs = {
'last_attempt': {'read_only': True},
'enabled': {'initial': True},
'is_working': {'read_only': True},
}
read_only_fields = ('created', 'modified')