serializers.py 3.18 KB
Newer Older
1
"""
2
3
api/serializers.py

David Haynes's avatar
David Haynes committed
4
5
6
7
Serializers allow complex data to be converted to native Python datatypes that
can then be easily rendered into JSON, XML or other content types.

http://www.django-rest-framework.org/api-guide/serializers
8
"""
9
10
11
12
13
# Future Imports
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

# App Imports
David Haynes's avatar
David Haynes committed
14
from .models import Category, Facility, Schedule, OpenTime, Location, Alert
15
16

# Other Imports
17
from rest_framework import serializers
18
19
from taggit_serializer.serializers import (TagListSerializerField,
                                           TaggitSerializer)
20

David Haynes's avatar
David Haynes committed
21
22
23
24
25
26
27
class AlertSerializer(serializers.ModelSerializer):
    """
    """
    class Meta:
        model = Alert
        fields = '__all__'

David Haynes's avatar
David Haynes committed
28
class CategorySerializer(serializers.ModelSerializer):
29
30
    """
    """
31
    class Meta:
David Haynes's avatar
David Haynes committed
32
33
34
        # Choose the model to be serialized
        model = Category
        # Serialize all of the fields
35
        fields = '__all__'
36

David Haynes's avatar
David Haynes committed
37
class LocationSerializer(serializers.ModelSerializer):
38
    """
David Haynes's avatar
David Haynes committed
39
    Serializer for the Location model.
40
41
    """
    class Meta:
David Haynes's avatar
David Haynes committed
42
43
44
        # Choose the model to be serialized
        model = Location
        # Serialize all of the fields
45
46
        fields = '__all__'

David Haynes's avatar
David Haynes committed
47
class OpenTimeSerializer(serializers.ModelSerializer):
48
    """
David Haynes's avatar
David Haynes committed
49
    Serializer for the OpenTime model.
50
    """
51
    class Meta:
David Haynes's avatar
David Haynes committed
52
53
54
        # Choose the model to be serialized
        model = OpenTime
        # Serialize all of the fields
55
        fields = '__all__'
56
57

class ScheduleSerializer(serializers.ModelSerializer):
58
    """
David Haynes's avatar
David Haynes committed
59
    Serializer for the Schedule model.
60
    """
David Haynes's avatar
David Haynes committed
61
    # Append a serialized OpenTime object
Nicholas Anderson's avatar
Nicholas Anderson committed
62
    open_times = OpenTimeSerializer(many=True, read_only=True)
63

64
    class Meta:
David Haynes's avatar
David Haynes committed
65
        # Choose the model to be serialized
66
        model = Schedule
David Haynes's avatar
David Haynes committed
67
        # List the fields that we are serializing
David Haynes's avatar
David Haynes committed
68
69
        fields = ('id', 'open_times', 'modified', 'name', 'valid_start',
                  'valid_end')
70

71
class FacilitySerializer(serializers.HyperlinkedModelSerializer):
72
    """
David Haynes's avatar
David Haynes committed
73
    Serializer for the Facility model.
74
75
76
77
78
79

    From the docs:
        The HyperlinkedModelSerializer class is similar to the ModelSerializer
        class except that it uses hyperlinks to represent relationships, rather
        than primary keys.
        http://www.django-rest-framework.org/api-guide/serializers/#hyperlinkedmodelserializer
80
    """
David Haynes's avatar
David Haynes committed
81
    # Append a serialized Category object
82
    facility_category = CategorySerializer(many=False, read_only=True)
David Haynes's avatar
David Haynes committed
83
    # Append a serialized Location object
84
    facility_location = LocationSerializer(many=False, read_only=True)
David Haynes's avatar
David Haynes committed
85
    # Append a serialized Schedule object to represent main_schedule
86
    main_schedule = ScheduleSerializer(many=False, read_only=True)
David Haynes's avatar
David Haynes committed
87
    # Append a serialized Schedule object to represent special_schedules
88
    special_schedules = ScheduleSerializer(many=True, read_only=True)
89

90
91
    tags = TagListSerializerField()

92
    class Meta:
David Haynes's avatar
David Haynes committed
93
        # Choose the model to be serialized
94
        model = Facility
David Haynes's avatar
David Haynes committed
95
        # List the fields that we are serializing
96
97
98
        fields = ('id', 'facility_category', 'facility_location', 'tags',
                  'tapingo_url', 'main_schedule', 'special_schedules',
                  'modified', 'name')