mybuddy/api/views.py

77 lines
2.3 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from rest_framework import viewsets
from core.models import (Child, DiaperChange, Feeding, Note, Sleep, Timer,
TummyTime)
from core.utils import filter_by_params
2017-08-16 12:49:58 +00:00
from .serializers import (ChildSerializer, DiaperChangeSerializer,
2017-08-13 20:48:16 +00:00
FeedingSerializer, NoteSerializer, SleepSerializer,
TimerSerializer, TummyTimeSerializer,)
2017-08-13 14:48:16 +00:00
2017-08-16 12:49:58 +00:00
class ChildViewSet(viewsets.ModelViewSet):
queryset = Child.objects.all()
serializer_class = ChildSerializer
2017-08-18 12:08:23 +00:00
lookup_field = 'slug'
2017-08-13 14:48:16 +00:00
2017-08-13 20:15:57 +00:00
def get_queryset(self):
2017-08-18 12:08:23 +00:00
params = ['first_name', 'last_name', 'slug']
2017-08-16 12:49:58 +00:00
return filter_by_params(self.request, Child, params)
2017-08-13 20:15:57 +00:00
2017-08-13 14:48:16 +00:00
2017-08-13 19:51:25 +00:00
class DiaperChangeViewSet(viewsets.ModelViewSet):
queryset = DiaperChange.objects.all()
serializer_class = DiaperChangeSerializer
2017-08-13 20:15:57 +00:00
def get_queryset(self):
params = ['child__slug', 'wet', 'solid', 'color']
return filter_by_params(self.request, DiaperChange, params)
2017-08-13 20:15:57 +00:00
2017-08-13 19:51:25 +00:00
2017-08-13 15:59:14 +00:00
class FeedingViewSet(viewsets.ModelViewSet):
queryset = Feeding.objects.all()
serializer_class = FeedingSerializer
2017-08-13 20:15:57 +00:00
def get_queryset(self):
params = ['child__slug', 'type', 'method', 'amount']
return filter_by_params(self.request, Feeding, params)
2017-08-13 20:15:57 +00:00
2017-08-13 15:59:14 +00:00
2017-08-13 20:48:16 +00:00
class NoteViewSet(viewsets.ModelViewSet):
queryset = Note.objects.all()
serializer_class = NoteSerializer
def get_queryset(self):
params = ['child__slug']
2017-08-13 20:48:16 +00:00
return filter_by_params(self.request, Note, params)
2017-08-13 14:48:16 +00:00
class SleepViewSet(viewsets.ModelViewSet):
queryset = Sleep.objects.all()
serializer_class = SleepSerializer
2017-08-13 15:59:14 +00:00
2017-08-13 20:15:57 +00:00
def get_queryset(self):
params = ['child__slug']
return filter_by_params(self.request, Sleep, params)
2017-08-13 20:15:57 +00:00
2017-08-13 15:59:14 +00:00
class TimerViewSet(viewsets.ModelViewSet):
queryset = Timer.objects.all()
serializer_class = TimerSerializer
def get_queryset(self):
params = ['name', 'active', 'user']
return filter_by_params(self.request, Timer, params)
2017-08-13 19:05:44 +00:00
class TummyTimeViewSet(viewsets.ModelViewSet):
queryset = TummyTime.objects.all()
serializer_class = TummyTimeSerializer
2017-08-13 20:15:57 +00:00
def get_queryset(self):
params = ['child__slug']
return filter_by_params(self.request, TummyTime, params)