Blame view

src/core/pyros_django/api/serializers.py 4.19 KB
eefbbbd2   Etienne Pallier   Model splitting g...
1
# General imports
98621b46   Alexis Koralewski   add DRF, pyros ap...
2
from rest_framework import serializers
0d6aa42f   Alexis Koralewski   Add vuejs in agen...
3
4
import datetime
from datetime import timezone
33c8da07   Alexis Koralewski   Adding SP_Period ...
5

eefbbbd2   Etienne Pallier   Model splitting g...
6
7
8
9
10
11
# Project imports
from user_manager.models import Institute, PyrosUser, Period, SP_Period, ScienceTheme, ScientificProgram
#from scientific_program.models import Period, SP_Period, ScienceTheme, ScientificProgram
from common.models import AgentCmd, AgentSurvey
from routine_manager.models import Sequence, Plan, Album

33c8da07   Alexis Koralewski   Adding SP_Period ...
12
13
14
15
16
17
class InstituteSerializer(serializers.ModelSerializer):
    class Meta:
        model = Institute
        fields = ["name"]


98621b46   Alexis Koralewski   add DRF, pyros ap...
18
class UserSerializer(serializers.ModelSerializer):
995a2ae6   Alexis Koralewski   adding scientific...
19
    user_level = serializers.StringRelatedField(many=True)
33c8da07   Alexis Koralewski   Adding SP_Period ...
20
    institute = InstituteSerializer(read_only=True)
98621b46   Alexis Koralewski   add DRF, pyros ap...
21
22
    class Meta:
        model = PyrosUser
995a2ae6   Alexis Koralewski   adding scientific...
23
24
        fields = ['url', 'username', 'email',  'institute',
                  "user_level"]
98621b46   Alexis Koralewski   add DRF, pyros ap...
25
26
27
        depth = 1


33c8da07   Alexis Koralewski   Adding SP_Period ...
28
29
class PeriodSerializer(serializers.ModelSerializer):
    
995a2ae6   Alexis Koralewski   adding scientific...
30
    class Meta:
33c8da07   Alexis Koralewski   Adding SP_Period ...
31
32
        model = Period
        fields = "__all__"
995a2ae6   Alexis Koralewski   adding scientific...
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

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__"

33c8da07   Alexis Koralewski   Adding SP_Period ...
51
52
53
54
55
56
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"]
98621b46   Alexis Koralewski   add DRF, pyros ap...
57
58
59

class SequenceSerializer(serializers.ModelSerializer):
    pyros_user = UserSerializer(read_only=True)
995a2ae6   Alexis Koralewski   adding scientific...
60

98621b46   Alexis Koralewski   add DRF, pyros ap...
61
62
    class Meta:
        model = Sequence
c2589779   Alexis Koralewski   Adding API views ...
63
64
65
66
67
        fields = "__all__"


class AlbumSerializer(serializers.ModelSerializer):
    sequence = SequenceSerializer(read_only=True)
995a2ae6   Alexis Koralewski   adding scientific...
68

c2589779   Alexis Koralewski   Adding API views ...
69
70
71
72
73
74
75
    class Meta:
        model = Album
        fields = "__all__"


class PlanSerializer(serializers.ModelSerializer):
    album = AlbumSerializer(read_only=True)
995a2ae6   Alexis Koralewski   adding scientific...
76

c2589779   Alexis Koralewski   Adding API views ...
77
78
79
80
    class Meta:
        model = Plan
        fields = "__all__"

995a2ae6   Alexis Koralewski   adding scientific...
81

c2589779   Alexis Koralewski   Adding API views ...
82
class SimplePlanSerializer(serializers.ModelSerializer):
995a2ae6   Alexis Koralewski   adding scientific...
83

c2589779   Alexis Koralewski   Adding API views ...
84
85
86
87
    class Meta:
        model = Plan
        exclude = ["album"]

995a2ae6   Alexis Koralewski   adding scientific...
88

c2589779   Alexis Koralewski   Adding API views ...
89
class SimpleAlbumSerializer(serializers.ModelSerializer):
995a2ae6   Alexis Koralewski   adding scientific...
90
91
    plans = SimplePlanSerializer(read_only=True, many=True)

c2589779   Alexis Koralewski   Adding API views ...
92
93
    class Meta:
        model = Album
995a2ae6   Alexis Koralewski   adding scientific...
94
95
96
        fields = ["name", "desc", "plans"]


c2589779   Alexis Koralewski   Adding API views ...
97
98
99
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
995a2ae6   Alexis Koralewski   adding scientific...
100
    albums = SimpleAlbumSerializer(read_only=True, many=True)
c2589779   Alexis Koralewski   Adding API views ...
101
102
103
104

    class Meta:
        model = Sequence
        fields = "__all__"
995a2ae6   Alexis Koralewski   adding scientific...
105
        extra_fields = ["pyros_user", "albums"]
0d6aa42f   Alexis Koralewski   Add vuejs in agen...
106
107


e49ef271   Alexis Koralewski   Add vue to render...
108
109
110
111
class AgentCmdSerializer(serializers.ModelSerializer):
    class Meta:
        model = AgentCmd
        fields = "__all__"
12ede5d0   Alexis Koralewski   Add new field in ...
112
113
114
    exec_time = serializers.SerializerMethodField()
    def get_exec_time(self,obj):
        try:
4bc760b9   Alexis Koralewski   UI changes on age...
115
            return round((obj.r_end_time - obj.r_start_time).total_seconds(),2)
12ede5d0   Alexis Koralewski   Add new field in ...
116
        except:
4bc760b9   Alexis Koralewski   UI changes on age...
117
            return ""
0d6aa42f   Alexis Koralewski   Add vuejs in agen...
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141

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"