serializers.py 3.4 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
David Haynes's avatar
David Haynes committed
18
from taggit_serializer.serializers import TagListSerializerField
19

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

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

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

David Haynes's avatar
David Haynes committed
46
class OpenTimeSerializer(serializers.ModelSerializer):
47
    """
David Haynes's avatar
David Haynes committed
48
    Serializer for the OpenTime model.
49
    """
50
    class Meta:
David Haynes's avatar
David Haynes committed
51
52
53
        # Choose the model to be serialized
        model = OpenTime
        # Serialize all of the fields
54
55
        fields = ('schedule', 'modified',
                  'start_day', 'end_day', 'start_time', 'end_time')
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
        fields = ('id', 'open_times', 'modified', 'name', 'valid_start',
69
                  'valid_end', 'twenty_four_hours', 'schedule_for_removal')
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
    # Append a serialized TagList object that represents the product tags for a
    # Facility
    facility_product_tags = TagListSerializerField()
92

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