Skip to content

Commit

Permalink
Serialize data_from_year field
Browse files Browse the repository at this point in the history
  • Loading branch information
juuso-j committed Sep 13, 2023
1 parent 7cbc2b4 commit 24fb14b
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions eco_counter/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.db.models import Q
from rest_framework import serializers

from ..models import (
Expand Down Expand Up @@ -35,6 +36,7 @@ class StationSerializer(serializers.ModelSerializer):
lon = serializers.SerializerMethodField()
lat = serializers.SerializerMethodField()
sensor_types = serializers.SerializerMethodField()
data_from_year = serializers.SerializerMethodField()

class Meta:
model = Station
Expand All @@ -52,6 +54,7 @@ class Meta:
"lon",
"lat",
"sensor_types",
"data_from_year",
]

def get_y(self, obj):
Expand Down Expand Up @@ -79,6 +82,19 @@ def get_sensor_types(self, obj):
result.append(type)
return result

def get_data_from_year(self, obj):
q_exp = (
Q(value_at__gt=0)
| Q(value_pt__gt=0)
| Q(value_jt__gt=0)
| Q(value_bt__gt=0)
)
qs = YearData.objects.filter(q_exp, station=obj).order_by("year__year_number")
if qs.count() > 0:
return qs[0].year.year_number
else:
return None


class YearSerializer(serializers.ModelSerializer):
station_name = serializers.PrimaryKeyRelatedField(
Expand Down

0 comments on commit 24fb14b

Please sign in to comment.