vo.py 1.1 KB
Newer Older
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

from rest_framework import serializers
from rest_polymorphic.serializers import PolymorphicSerializer

from ..vo import VO, Group, Entitlement, EntitlementNameSpace


class EntitlementNameSpaceSerializer(serializers.ModelSerializer):
    class Meta:
        model = EntitlementNameSpace
        fields = ('name', )


# polymorphic serializer
VO_FIELDS = (
    'id',
    'name',
    'pretty_name',
    'description',
)


class AbstractVOSerializer(serializers.ModelSerializer):
    class Meta:
        model = VO
        fields = VO_FIELDS


class GroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = Group
        fields = VO_FIELDS


class EntitlementSerializer(serializers.ModelSerializer):
    name_space = EntitlementNameSpaceSerializer()

    class Meta:
        model = Entitlement
        fields = VO_FIELDS + (
            'name_space',
            'group_authority',
            'role',
        )


class VOSerializer(PolymorphicSerializer):
    model_serializer_mapping = {
        VO: AbstractVOSerializer,
        Group: GroupSerializer,
        Entitlement: EntitlementSerializer,
    }