serializers.py 2.77 KB
from common.models import Institute, Period, PyrosUser, SP_Period, ScienceTheme, ScientificProgram, Sequence, Plan, Album
from rest_framework import serializers



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