Blame view

src/core/pyros_django/api/serializers.py 2.77 KB
33c8da07   Alexis Koralewski   Adding SP_Period ...
1
from common.models import Institute, Period, PyrosUser, SP_Period, ScienceTheme, ScientificProgram, Sequence, Plan, Album
98621b46   Alexis Koralewski   add DRF, pyros ap...
2
3
from rest_framework import serializers

995a2ae6   Alexis Koralewski   adding scientific...
4

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