serializers.py
4.5 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
# General imports
from rest_framework import serializers
import datetime
from datetime import timezone
# Project imports
from user_mgmt.models import Institute, PyrosUser, Period, SP_Period, ScienceTheme, ScientificProgram
#scp_mgmt.models import Period, SP_Period, ScienceTheme, ScientificProgram
from majordome.models import AgentCmd, AgentSurvey
from env_monitor.models import Env_data, Sensors_data_last_value
from seq_submit.models import Sequence, Plan, Album
class InstituteSerializer(serializers.ModelSerializer):
class Meta:
model = Institute
fields = ["name"]
class UserSerializer(serializers.ModelSerializer):
user_level = serializers.StringRelatedField(many=True)
institute = InstituteSerializer(read_only=True)
class Meta:
model = PyrosUser
fields = ['url', 'username', 'email', 'institute',
"user_level"]
depth = 1
class PeriodSerializer(serializers.ModelSerializer):
class Meta:
model = Period
fields = "__all__"
class ScienceThemeSerializer(serializers.ModelSerializer):
class Meta:
model = ScienceTheme
fields = ["name"]
class ScientificProgramSerializer(serializers.ModelSerializer):
sp_pi = UserSerializer(read_only=True)
institute = InstituteSerializer(read_only=True)
science_theme = ScienceThemeSerializer(read_only=True)
class Meta:
model = ScientificProgram
fields = ["name", "institute", "sp_pi", "description_short",
"description_long", "science_theme"]
#fields = "__all__"
class SPPeriodSerializer(serializers.ModelSerializer):
period = PeriodSerializer(read_only=True)
scientific_program = ScientificProgramSerializer(read_only=True)
class Meta:
model = SP_Period
fields = ["id", "scientific_program","period", "quota_remaining", "over_quota_duration_remaining", "token_remaining"]
class SequenceSerializer(serializers.ModelSerializer):
pyros_user = UserSerializer(read_only=True)
class Meta:
model = Sequence
fields = "__all__"
class AlbumSerializer(serializers.ModelSerializer):
sequence = SequenceSerializer(read_only=True)
class Meta:
model = Album
fields = "__all__"
class PlanSerializer(serializers.ModelSerializer):
album = AlbumSerializer(read_only=True)
class Meta:
model = Plan
fields = "__all__"
class SimplePlanSerializer(serializers.ModelSerializer):
class Meta:
model = Plan
exclude = ["album"]
class SimpleAlbumSerializer(serializers.ModelSerializer):
plans = SimplePlanSerializer(read_only=True, many=True)
class Meta:
model = Album
fields = ["name", "desc", "plans"]
class FullSequenceSerializer(serializers.ModelSerializer):
pyros_user = UserSerializer(read_only=True)
# here albums is the relationmodel with many to many realtion so we have to set many to True
albums = SimpleAlbumSerializer(read_only=True, many=True)
class Meta:
model = Sequence
fields = "__all__"
extra_fields = ["pyros_user", "albums"]
class AgentCmdSerializer(serializers.ModelSerializer):
class Meta:
model = AgentCmd
fields = "__all__"
exec_time = serializers.SerializerMethodField()
def get_exec_time(self,obj):
try:
return round((obj.r_end_time - obj.r_start_time).total_seconds(),2)
except:
return ""
class AgentSurveySerializer(serializers.ModelSerializer):
class Meta:
model = AgentSurvey
fields = "__all__"
is_out_of_date = serializers.SerializerMethodField()
running_cmd = serializers.SerializerMethodField()
def get_is_out_of_date(self, obj):
datetime_now = datetime.datetime.utcnow()
datetime_now_minus_thirty_sec = datetime_now - datetime.timedelta(seconds=30)
datetime_now_minus_thirty_sec = datetime_now_minus_thirty_sec.replace(tzinfo=timezone.utc)
if datetime_now_minus_thirty_sec >= obj.updated:
return True
else:
return False
def get_running_cmd(self,obj):
try:
return AgentCmd.objects.get(state="CMD_RUNNING",sender=obj.name).full_name
except AgentCmd.DoesNotExist:
return "None"
class EnvDataSerializer(serializers.ModelSerializer):
class Meta:
model = Env_data
fields = "__all__"
class SensorDataLastSerializer(serializers.ModelSerializer):
class Meta:
model = Sensors_data_last_value
fields = "__all__"