Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/eco counter serialize data from year field #307

Merged
merged 2 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 2 additions & 0 deletions eco_counter/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest
from rest_framework.reverse import reverse

from .conftest import TEST_TIMESTAMP
from .constants import TEST_EC_STATION_NAME


Expand Down Expand Up @@ -271,6 +272,7 @@ def test__station(api_client, stations, year_datas):
assert response.status_code == 200
assert response.json()["results"][0]["name"] == TEST_EC_STATION_NAME
assert response.json()["results"][0]["sensor_types"] == ["at"]
assert response.json()["results"][0]["data_from_year"] == TEST_TIMESTAMP.year
# Test retrieving station by data type
url = reverse("eco_counter:stations-list") + "?data_type=a"
response = api_client.get(url)
Expand Down
Loading