Blame view

src/core/pyros_django/api/serializers.py 4 KB
0d6aa42f   Alexis Koralewski   Add vuejs in agen...
1
from common.models import AgentSurvey, Institute, Period, PyrosUser, SP_Period, ScienceTheme, ScientificProgram, Sequence, Plan, Album, AgentCmd
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
6
7
8
9
10
11

class InstituteSerializer(serializers.ModelSerializer):
    class Meta:
        model = Institute
        fields = ["name"]


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


33c8da07   Alexis Koralewski   Adding SP_Period ...
22
23
class PeriodSerializer(serializers.ModelSerializer):
    
995a2ae6   Alexis Koralewski   adding scientific...
24
    class Meta:
33c8da07   Alexis Koralewski   Adding SP_Period ...
25
26
        model = Period
        fields = "__all__"
995a2ae6   Alexis Koralewski   adding scientific...
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

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 ...
45
46
47
48
49
50
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...
51
52
53

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

98621b46   Alexis Koralewski   add DRF, pyros ap...
55
56
    class Meta:
        model = Sequence
c2589779   Alexis Koralewski   Adding API views ...
57
58
59
60
61
        fields = "__all__"


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

c2589779   Alexis Koralewski   Adding API views ...
63
64
65
66
67
68
69
    class Meta:
        model = Album
        fields = "__all__"


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

c2589779   Alexis Koralewski   Adding API views ...
71
72
73
74
    class Meta:
        model = Plan
        fields = "__all__"

995a2ae6   Alexis Koralewski   adding scientific...
75

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

c2589779   Alexis Koralewski   Adding API views ...
78
79
80
81
    class Meta:
        model = Plan
        exclude = ["album"]

995a2ae6   Alexis Koralewski   adding scientific...
82

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

c2589779   Alexis Koralewski   Adding API views ...
86
87
    class Meta:
        model = Album
995a2ae6   Alexis Koralewski   adding scientific...
88
89
90
        fields = ["name", "desc", "plans"]


c2589779   Alexis Koralewski   Adding API views ...
91
92
93
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...
94
    albums = SimpleAlbumSerializer(read_only=True, many=True)
c2589779   Alexis Koralewski   Adding API views ...
95
96
97
98

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


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

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"