From 25c9a51d1bbf03b3b6836c8476b67f40a1500aa1 Mon Sep 17 00:00:00 2001 From: Christian Kreuzberger Date: Sat, 4 Jan 2020 12:51:41 +0100 Subject: [PATCH] Added information about the smarthome used to the latest data --- app/tempBerry/smarthome/rest/serializers.py | 11 ++++++++++- app/tempBerry/temperatures/rest/serializers.py | 5 ++++- app/tempBerry/temperatures/rest/viewsets.py | 2 +- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/app/tempBerry/smarthome/rest/serializers.py b/app/tempBerry/smarthome/rest/serializers.py index 2b1084a..388dbdf 100644 --- a/app/tempBerry/smarthome/rest/serializers.py +++ b/app/tempBerry/smarthome/rest/serializers.py @@ -1,6 +1,6 @@ from rest_framework import serializers -from tempBerry.smarthome.models import Room +from tempBerry.smarthome.models import Room, SmartHome class RoomSerializer(serializers.ModelSerializer): @@ -12,3 +12,12 @@ class Meta: fields = ('id', 'name', 'comment', 'created_at', 'public', 'has_temperature', 'has_humidity', 'has_air_pressure') read_only_fields = ('created_at', ) + + +class MinimalisticSmartHomeSerializer(serializers.Serializer): + """ + Minimalistic Serializer for SmartHome + """ + id = serializers.IntegerField(read_only=True) + name = serializers.CharField(read_only=True) + description = serializers.CharField(read_only=True) diff --git a/app/tempBerry/temperatures/rest/serializers.py b/app/tempBerry/temperatures/rest/serializers.py index 958fa41..fd570f6 100644 --- a/app/tempBerry/temperatures/rest/serializers.py +++ b/app/tempBerry/temperatures/rest/serializers.py @@ -4,6 +4,7 @@ from rest_framework import serializers from tempBerry.smarthome.models import Room +from tempBerry.smarthome.rest.serializers import MinimalisticSmartHomeSerializer from tempBerry.temperatures.models import TemperatureDataEntry @@ -37,9 +38,11 @@ class RoomLiveDataSerializer(serializers.ModelSerializer): average_last_hour = serializers.SerializerMethodField() + smarthome = MinimalisticSmartHomeSerializer(many=False) + class Meta: model = Room - fields = ('id', 'name', 'comment', 'created_at', 'public', 'live_data', + fields = ('id', 'name', 'comment', 'created_at', 'public', 'live_data', 'smarthome', 'has_temperature', 'has_humidity', 'has_air_pressure', 'average_last_hour') read_only_fields = ('created_at', 'live_data') diff --git a/app/tempBerry/temperatures/rest/viewsets.py b/app/tempBerry/temperatures/rest/viewsets.py index 3f566b6..58a2000 100644 --- a/app/tempBerry/temperatures/rest/viewsets.py +++ b/app/tempBerry/temperatures/rest/viewsets.py @@ -67,7 +67,7 @@ def latest(self, request): cached_data = cache.get('last_temperature_data') # get queryset with public rooms only - rooms = self.get_queryset().filter(public=True) + rooms = self.get_queryset().filter(public=True).prefetch_related('smarthome') if not cached_data: # no cached_data available yet, pre-fill it