serializers.py 1.54 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


class BuildingSerializer(serializers.ModelSerializer):

14
15
16
    # floors
    # rooms

17
18
19
20
21
22
23
    class Meta:
        model = Building
        fields = ('name', 'neighbourhood', 'campus')


class FloorSerializer(serializers.ModelSerializer):

24
25
26
27
28
29
30
31
32
33
34
    building = serializers.SerializerMethodField('get_building_name')
    #rooms = serializers.SerializerMethodField('get_floor_rooms')

    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

35
36
    class Meta:
        model = Floor
37
        fields = ('number', 'building', )#'rooms')
38
39
40
41


class RoomSerializer(serializers.ModelSerializer):

42
43
44
45
46
47
48
49
50
    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

51
52
    class Meta:
        model = Room
53
54
55
56
57
58
59
60
        fields = ('number', 'floor', 'building')


class MajorSerializer(serializers.ModelSerializer):

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