Skip to content

Commit

Permalink
Merge pull request #286 from City-of-Turku/feature/eco-counter-filter…
Browse files Browse the repository at this point in the history
…-station-by-data-type

Feature/eco counter filter station by data type
  • Loading branch information
juuso-j authored Jul 17, 2023
2 parents 50a7df0 + 6ab422f commit a070d5e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
15 changes: 15 additions & 0 deletions eco_counter/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ def list(self, request):
raise ParseError(
"Valid 'counter_type' choices are: 'EC', 'TC', 'TR' or 'LC'."
)
if "data_type" in filters:
data_type = filters["data_type"].lower()
data_types = ["a", "j", "b", "p"]
if data_type not in data_types:
raise ParseError(
f"Valid 'data_type' choices are: {', '.join(data_types)}"
)
ids = []
data_type = data_type + "t"
for station in Station.objects.all():
filter = {"station": station, f"value_{data_type}__gt": 0}
if YearData.objects.filter(**filter).count() > 0:
ids.append(station.id)
queryset = Station.objects.filter(id__in=ids)

page = self.paginate_queryset(queryset)
serializer = StationSerializer(page, many=True)
return self.get_paginated_response(serializer.data)
Expand Down
4 changes: 4 additions & 0 deletions eco_counter/specification.swagger2.0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ paths:
description: "The type of the counter EC(Eco Counter), TC(Traffic Counter), LC(LAM Counter), TR(Telraam Counter)"
name: counter_type
type: string
- in: query
description: "The data type of the counter: A(car), B(bus), J(pedestrian) or P(bicycle). Returns stations containing data of the specified type."
name: data_type
type: string
responses:
200:
description: "List of stations."
Expand Down
7 changes: 7 additions & 0 deletions eco_counter/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,10 @@ 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"]
# Test retrieving station by data type
url = reverse("eco_counter:stations-list") + "?data_type=a"
response = api_client.get(url)
assert response.json()["count"] == 1
url = reverse("eco_counter:stations-list") + "?data_type=p"
response = api_client.get(url)
assert response.json()["count"] == 0

0 comments on commit a070d5e

Please sign in to comment.