serializers.py 3.21 KB
Newer Older
David Haynes's avatar
David Haynes committed
1
2
#!/usr/bin/env python
# -*- coding: utf-8 -*-
3
"""
4
5
api/serializers.py

David Haynes's avatar
David Haynes committed
6
7
8
9
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
10
"""
11
# App Imports
David Haynes's avatar
David Haynes committed
12
from .models import Category, Facility, Schedule, OpenTime, Location, Alert
13
14

# Other Imports
15
from rest_framework import serializers
David Haynes's avatar
David Haynes committed
16
from taggit_serializer.serializers import TagListSerializerField
17

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

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

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

David Haynes's avatar
David Haynes committed
44
class OpenTimeSerializer(serializers.ModelSerializer):
45
    """
David Haynes's avatar
David Haynes committed
46
    Serializer for the OpenTime model.
47
    """
48
    class Meta:
David Haynes's avatar
David Haynes committed
49
50
51
        # Choose the model to be serialized
        model = OpenTime
        # Serialize all of the fields
52
53
        fields = ('schedule', 'modified',
                  'start_day', 'end_day', 'start_time', 'end_time')
54
55

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

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

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

    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
78
    """
David Haynes's avatar
David Haynes committed
79
    # Append serialized objects
80
81
    facility_category = CategorySerializer(many=False, read_only=True)
    facility_location = LocationSerializer(many=False, read_only=True)
82
83
    main_schedule = ScheduleSerializer(many=False, read_only=True)
    special_schedules = ScheduleSerializer(many=True, read_only=True)
84
    facility_product_tags = TagListSerializerField()
David Haynes's avatar
David Haynes committed
85
86
    facility_labels = TagListSerializerField()
    facility_classifier = TagListSerializerField()
87

88
    class Meta:
David Haynes's avatar
David Haynes committed
89
        # Choose the model to be serialized
90
        model = Facility
David Haynes's avatar
David Haynes committed
91
        # List the fields that we are serializing
David Haynes's avatar
David Haynes committed
92
93
94
95
        fields = ('slug', 'facility_name', 'logo', 'facility_location',
                  'facility_category', 'facility_product_tags',
                  'facility_labels', 'facility_classifier', 'tapingo_url',
                  'note', 'main_schedule', 'special_schedules', 'modified', )