Skip to content

Commit

Permalink
Merge branch 'master' of github.com:CodeForAfricaLabs/sensors.AFRICA-api
Browse files Browse the repository at this point in the history
  • Loading branch information
karimkawambwa committed Feb 21, 2019
2 parents 59719a6 + a821340 commit a06be7e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 22 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ build:
up:
$(COMPOSE) up -d

logs:
$(COMPOSE) logs -f
log:
$(COMPOSE) logs -f

compilescss:
$(COMPOSE) exec api python manage.py compilescss
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Docker compose make commands:

- `make build`
- `make up` - run docker and detach
- `make logs` - tail logs
- `make log` - tail logs
- `make test` - run test
- `make migrate` - migrate database
- `make createsuperuser` - create a super user for admin
Expand Down
3 changes: 3 additions & 0 deletions sensorsafrica/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class City(TimeStampedModel):
latitude = models.DecimalField(max_digits=14, decimal_places=11, null=True, blank=True)
longitude = models.DecimalField(max_digits=14, decimal_places=11, null=True, blank=True)

class Meta:
verbose_name_plural = "Cities"

def save(self, *args, **kwargs):
self.slug = slugify(self.name)
return super(City, self).save(*args, **kwargs)
Expand Down
22 changes: 10 additions & 12 deletions sensorsafrica/api/v2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,12 @@ def get_paginated_response(self, data_stats):
}
)

count = len(results.keys())
values = list(results.values())
return Response(
{
"next": self.get_next_link(),
"previous": self.get_previous_link(),
"count": count,
"results": values[0] if count == 1 else values,
"count": len(results.keys()),
"results": list(results.values()),
}
)

Expand All @@ -97,7 +95,7 @@ class SensorDataStatView(mixins.ListModelMixin, viewsets.GenericViewSet):
def get_queryset(self):
sensor_type = self.kwargs["sensor_type"]

city_slug = self.request.query_params.get("city", None)
city_slugs = self.request.query_params.get("city", None)
from_date = self.request.query_params.get("from", None)
to_date = self.request.query_params.get("to", None)

Expand All @@ -117,12 +115,12 @@ def get_queryset(self):
)

if not from_date and not to_date:
return self._retrieve_past_24hrs(city_slug, filter_value_types)
return self._retrieve_past_24hrs(city_slugs, filter_value_types)

return self._retrieve_range(from_date, to_date, city_slug, filter_value_types)
return self._retrieve_range(from_date, to_date, city_slugs, filter_value_types)

@staticmethod
def _retrieve_past_24hrs(city_slug, filter_value_types):
def _retrieve_past_24hrs(city_slugs, filter_value_types):
to_date = timezone.now().replace(minute=0, second=0, microsecond=0)
from_date = to_date - datetime.timedelta(hours=24)

Expand All @@ -132,8 +130,8 @@ def _retrieve_past_24hrs(city_slug, filter_value_types):
timestamp__lte=to_date,
)

if city_slug:
queryset = queryset.filter(city_slug=city_slug)
if city_slugs:
queryset = queryset.filter(city_slug__in=city_slugs.split(','))

return (
queryset.order_by()
Expand All @@ -152,7 +150,7 @@ def _retrieve_past_24hrs(city_slug, filter_value_types):
)

@staticmethod
def _retrieve_range(from_date, to_date, city_slug, filter_value_types):
def _retrieve_range(from_date, to_date, city_slugs, filter_value_types):
if not to_date:
from_date = beginning_of_day(from_date)
to_date = end_of_today()
Expand All @@ -162,7 +160,7 @@ def _retrieve_range(from_date, to_date, city_slug, filter_value_types):

return (
SensorDataStat.objects.filter(
city_slug=city_slug,
city_slug__in=city_slugs.split(','),
value_type__in=filter_value_types,
timestamp__gte=from_date,
timestamp__lt=to_date,
Expand Down
33 changes: 26 additions & 7 deletions sensorsafrica/tests/test_sensordatastats_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_getting_air_data_now(self, client, sensorsdatastats):

assert data["count"] == 1

result = data["results"]
result = data["results"][0]

assert "P1" in result
assert result["P1"]["average"] == 0.0
Expand Down Expand Up @@ -48,6 +48,22 @@ def test_getting_air_data_now_all_cities(self, client, sensorsdatastats):
assert "P2" in results[1]
assert results[2]["city_slug"] == "nairobi"

def test_getting_air_data_now_filter_cities(self, client, sensorsdatastats):
response = client.get("/v2/data/air/?city=dar-es-salaam,bagamoyo", format="json")
assert response.status_code == 200

data = response.json()

assert data["count"] == 2

results = data["results"]

assert results[0]["city_slug"] == "bagamoyo"
assert results[1]["city_slug"] == "dar-es-salaam"
assert "P1" in results[1]
assert results[1]["city_slug"] == "dar-es-salaam"
assert "P2" in results[1]

def test_getting_air_data_value_type(self, client, sensorsdatastats):
response = client.get(
"/v2/data/air/?city=dar-es-salaam&value_type=P2", format="json"
Expand All @@ -57,7 +73,10 @@ def test_getting_air_data_value_type(self, client, sensorsdatastats):
data = response.json()

assert data["count"] == 1
assert "P2" in data["results"]
assert "P2" in data["results"][0]
assert "P1" not in data["results"][0]
assert "temperature" not in data["results"][0]
assert "humidity" not in data["results"][0]

def test_getting_air_data_from_date(self, client, sensorsdatastats):
response = client.get(
Expand All @@ -69,8 +88,8 @@ def test_getting_air_data_from_date(self, client, sensorsdatastats):

data = response.json()

assert type(data["results"]["P1"]) == list
assert type(data["results"]["P2"]) == list
assert type(data["results"][0]["P1"]) == list
assert type(data["results"][0]["P2"]) == list

def test_getting_air_data_from_date_to_date(self, client, sensorsdatastats):
now = timezone.now()
Expand All @@ -83,8 +102,8 @@ def test_getting_air_data_from_date_to_date(self, client, sensorsdatastats):
data = response.json()

assert data["count"] == 1
assert type(data["results"]["P1"]) == list
assert type(data["results"]["P2"]) == list
assert type(data["results"][0]["P1"]) == list
assert type(data["results"][0]["P2"]) == list

def test_getting_air_data_with_invalid_request(self, client, sensorsdatastats):
response = client.get(
Expand Down Expand Up @@ -123,7 +142,7 @@ def test_getting_air_data_now_with_additional_values(

assert data["count"] == 1

result = data["results"]
result = data["results"][0]

assert "P1" in result
assert result["P1"]["average"] == 0.0
Expand Down

0 comments on commit a06be7e

Please sign in to comment.