Add Temperature data to API.

This commit is contained in:
Christopher C. Wells 2019-05-16 22:13:14 -07:00
parent 88dfe809d3
commit 243b9ef1c1
5 changed files with 58 additions and 0 deletions

View File

@ -58,6 +58,12 @@ class SleepSerializer(CoreModelSerializer):
fields = ('id', 'child', 'start', 'end', 'duration') fields = ('id', 'child', 'start', 'end', 'duration')
class TemperatureSerializer(CoreModelSerializer):
class Meta:
model = models.Temperature
fields = ('id', 'child', 'temperature', 'time')
class TimerSerializer(CoreModelSerializer): class TimerSerializer(CoreModelSerializer):
user = serializers.PrimaryKeyRelatedField(queryset=User.objects.all()) user = serializers.PrimaryKeyRelatedField(queryset=User.objects.all())

View File

@ -195,6 +195,41 @@ class SleepAPITestCase(APITestCase):
self.assertEqual(str(obj.duration), '3:30:00') self.assertEqual(str(obj.duration), '3:30:00')
@override_settings(TIME_ZONE='US/Eastern')
class TemperatureAPITestCase(APITestCase):
fixtures = ['tests.json']
endpoint = reverse('api:temperature-list')
def setUp(self):
self.client.login(username='admin', password='admin')
def test_get(self):
response = self.client.get(self.endpoint)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['results'][0], {
'id': 1,
'child': 1,
'temperature': 98.6,
'time': '2017-11-17T12:52:00-05:00'
})
def test_options(self):
response = self.client.options(self.endpoint)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['name'], 'Temperature List')
def test_post(self):
data = {
'child': 1,
'temperature': '100.1',
'time': '2017-11-20T22:52:00-05:00'
}
response = self.client.post(self.endpoint, data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
obj = models.Temperature.objects.get(pk=response.data['id'])
self.assertEqual(str(obj.temperature), data['temperature'])
@override_settings(TIME_ZONE='US/Eastern') @override_settings(TIME_ZONE='US/Eastern')
class TimerAPITestCase(APITestCase): class TimerAPITestCase(APITestCase):
fixtures = ['tests.json'] fixtures = ['tests.json']

View File

@ -10,6 +10,7 @@ router.register(r'changes', views.DiaperChangeViewSet)
router.register(r'feedings', views.FeedingViewSet) router.register(r'feedings', views.FeedingViewSet)
router.register(r'notes', views.NoteViewSet) router.register(r'notes', views.NoteViewSet)
router.register(r'sleep', views.SleepViewSet) router.register(r'sleep', views.SleepViewSet)
router.register(r'temperature', views.TemperatureViewSet)
router.register(r'timers', views.TimerViewSet) router.register(r'timers', views.TimerViewSet)
router.register(r'tummy-times', views.TummyTimeViewSet) router.register(r'tummy-times', views.TummyTimeViewSet)
router.register(r'weight', views.WeightViewSet) router.register(r'weight', views.WeightViewSet)

View File

@ -37,6 +37,12 @@ class SleepViewSet(viewsets.ModelViewSet):
filterset_fields = ('child',) filterset_fields = ('child',)
class TemperatureViewSet(viewsets.ModelViewSet):
queryset = models.Temperature.objects.all()
serializer_class = serializers.TemperatureSerializer
filterset_fields = ('child',)
class TimerViewSet(viewsets.ModelViewSet): class TimerViewSet(viewsets.ModelViewSet):
queryset = models.Timer.objects.all() queryset = models.Timer.objects.all()
serializer_class = serializers.TimerSerializer serializer_class = serializers.TimerSerializer

View File

@ -286,6 +286,16 @@
"end": "2017-11-19T04:00:00Z", "end": "2017-11-19T04:00:00Z",
"duration": "04:00:00" "duration": "04:00:00"
} }
},
{
"model": "core.temperature",
"pk": 1,
"fields":
{
"child": 1,
"temperature": 98.6,
"time": "2017-11-17T17:52:00Z"
}
}, },
{ {
"model": "core.timer", "model": "core.timer",