Commit 8e04a4ab authored by Daniel W Bond's avatar Daniel W Bond

added clickable links between objects on API through additional serializers

parent e9ddd189
# standard library imports
from __future__ import absolute_import, print_function
# core django imports
from django.core.serializers import serialize
# third party imports
from rest_framework import serializers
from rest_framework.reverse import reverse
# imports from your apps
from housing.models import Building, Floor, Room
from accounts.models import Major
# we're not using DRF's default hyperlinks because we need to look up multiple fields
class FloorURLSerializer(serializers.HyperlinkedIdentityField):
def get_url(self, obj, view_name, request, format):
url = reverse(view_name,
kwargs={'building__building_name': obj.building.building_name,
'floor_num': obj.number},
request=request,
format=format)
return url
class RoomURLSerializer(serializers.HyperlinkedIdentityField):
def get_url(self, obj, view_name, request, format):
url = reverse(view_name,
kwargs={'floor__building__building_name': obj.floor.building.building_name,
'floor__floor_num': obj.floor.number,
'room_num': obj.number},
request=request,
format=format)
return url
# these serializers are both used as fields for other serialzers
class FloorNumSerializer(serializers.ModelSerializer):
url = FloorURLSerializer(view_name='api_detail_floor')
class Meta:
model = Floor
fields = ('url', 'number')
class RoomNumSerializer(serializers.ModelSerializer):
url = RoomURLSerializer(view_name='api_detail_room')
class Meta:
model = Room
fields = ('url', 'number')
# the main serializers
class BuildingSerializer(serializers.ModelSerializer):
# using django's built-in serialization makes for many escape characters
# and lots of unnecessary fields... there has to be a way to do this with DRF
#floors = serializers.SerializerMethodField('get_building_floors')
class Meta:
model = Building
fields = ('url', 'name', 'neighbourhood', 'campus',)
extra_kwargs = {
'url': {'view_name': 'api_detail_building', 'lookup_field': 'building_name'}
}
def get_building_floors(self, building):
floors = serialize('json', Floor.objects.filter(building=building),
fields=('number'))
return floors
class BuildingFloorListSerializer(serializers.ModelSerializer):
floors = FloorNumSerializer(source='floor_set', many=True)
class Meta:
model = Building
fields = ('name', 'neighbourhood', 'campus')
fields = ('name', 'neighbourhood', 'campus', 'floors')
class FloorSerializer(serializers.ModelSerializer):
building = serializers.SerializerMethodField('get_building_name')
#rooms = serializers.SerializerMethodField('get_floor_room')
rooms = RoomNumSerializer(source='room_set', many=True)
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
class Meta:
model = Floor
fields = ('number', 'building')
fields = ('number', 'building', 'rooms')
class RoomSerializer(serializers.ModelSerializer):
......
......@@ -11,7 +11,8 @@ from rest_framework.response import Response
# imports from your apps
from housing.models import Building, Floor, Room
from accounts.models import Major
from .serializers import (BuildingSerializer, FloorSerializer, RoomSerializer,
from .serializers import (BuildingSerializer, BuildingFloorListSerializer,
FloorSerializer, RoomSerializer,
MajorSerializer)
......@@ -45,7 +46,7 @@ class BuildingList(ListAPIView):
class BuildingRetrieve(RetrieveAPIView):
model = Building
queryset = Building.objects.all()
serializer_class = BuildingSerializer
serializer_class = BuildingFloorListSerializer
lookup_field = 'building_name'
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment