serializers.py 1.44 KB
Newer Older
1
2
# standard library imports
from __future__ import absolute_import, print_function
3
4
# core django imports
from django.core.serializers import serialize
5
6
7
8
# third party imports
from rest_framework import serializers
# imports from your apps
from housing.models import Building, Floor, Room
9
from accounts.models import Major
10
11
12
13
14
15
16
17
18
19
20


class BuildingSerializer(serializers.ModelSerializer):

    class Meta:
        model = Building
        fields = ('name', 'neighbourhood', 'campus')


class FloorSerializer(serializers.ModelSerializer):

21
22
23
24
25
26
27
28
29
30
    building = serializers.SerializerMethodField('get_building_name')

    def get_building_name(self, floor):
        return floor.building.name

    def get_floor_rooms(self, floor):
        rooms = serialize('json', Room.objects.filter(floor=floor),
                          fields=('number'))
        return rooms

31
32
    class Meta:
        model = Floor
33
        fields = ('number', 'building')
34
35
36
37


class RoomSerializer(serializers.ModelSerializer):

38
39
40
41
42
43
44
45
46
    building = serializers.SerializerMethodField('get_building_name')
    floor = serializers.SerializerMethodField('get_floor_number')

    def get_building_name(self, room):
        return room.floor.building.name

    def get_floor_number(self, room):
        return room.floor.number

47
48
    class Meta:
        model = Room
49
50
51
52
53
54
55
56
        fields = ('number', 'floor', 'building')


class MajorSerializer(serializers.ModelSerializer):

    class Meta:
        model = Major
        fields = ('name', )