-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Basic API and Improved Error Logging
- Loading branch information
Sam Collins
committed
Oct 9, 2016
1 parent
1490c23
commit 884655b
Showing
7 changed files
with
98 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,7 +61,7 @@ node_modules/ | |
# User-uploaded media | ||
sensor_portal/media/ | ||
|
||
|
||
src/ | ||
|
||
|
||
staticfiles/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from rest_framework import serializers | ||
|
||
from sensor_portal.sensors.models import Sensor, Metric, Reading | ||
|
||
class SensorSerializer(serializers.HyperlinkedModelSerializer): | ||
|
||
class Meta: | ||
model = Sensor | ||
fields = ('name', 'position', 'active','description') | ||
|
||
class MetricSerializer(serializers.HyperlinkedModelSerializer): | ||
|
||
class Meta: | ||
model = Metric | ||
fields = ('name', 'unit', 'eu_limit', 'description') | ||
|
||
class ReadingSerializer(serializers.HyperlinkedModelSerializer): | ||
|
||
class Meta: | ||
model = Reading | ||
fields = ('sensor', 'message', 'metric', 'value', 'recorded') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,59 @@ | ||
from django.shortcuts import render_to_response | ||
from chartit import DataPool, Chart | ||
from rest_framework import viewsets | ||
from rest_framework import filters | ||
|
||
from .models import Reading | ||
import django_filters | ||
from django_filters.widgets import RangeWidget | ||
|
||
from sensor_portal.sensors.models import Sensor, Metric, Reading | ||
from sensor_portal.sensors.serializers import SensorSerializer, MetricSerializer, ReadingSerializer | ||
|
||
def index(request): | ||
chart_data = DataPool(series=[ | ||
{'options': {'source': Reading.objects.all()}, 'terms': ['recorded', 'value']} | ||
]) | ||
cht = Chart(datasource=chart_data, series_options=[ | ||
{'options': {'type': 'line', 'stacking': False}, 'terms': {'recorded': ['value']}}], | ||
chart_options={'title': {'text': 'Readings'}, | ||
'xAxis': {'title': {'text': 'Month number'}}}) | ||
return render_to_response('dashboard/index.html', {'weatherchart': cht}) | ||
|
||
FILTERS = ( | ||
filters.DjangoFilterBackend, | ||
filters.SearchFilter, | ||
filters.OrderingFilter | ||
) | ||
|
||
class SensorFilter(filters.FilterSet): | ||
|
||
class Meta: | ||
model = Sensor | ||
fields = ('name', 'active','description') | ||
|
||
class MetricFilter(filters.FilterSet): | ||
|
||
class Meta: | ||
model = Metric | ||
fields = ('name', 'unit', 'eu_limit', 'description') | ||
|
||
class ReadingFilter(filters.FilterSet): | ||
recorded = django_filters.DateTimeFromToRangeFilter(widget=RangeWidget(attrs={'placeholder': 'dd/mm/yyyy hh:mm'})) | ||
|
||
class Meta: | ||
model = Reading | ||
fields = ('sensor', 'message', 'metric', 'value', 'recorded') | ||
|
||
|
||
class SensorViewSet(viewsets.ModelViewSet): | ||
queryset = Sensor.objects.all() | ||
filter_backends = FILTERS | ||
filter_class = SensorFilter | ||
serializer_class = SensorSerializer | ||
search_fields = ('name', 'position', 'description') | ||
ordering_fields = ('name', 'position', 'active') | ||
|
||
class MetricViewSet(viewsets.ModelViewSet): | ||
queryset = Metric.objects.all() | ||
filter_backends = FILTERS | ||
filter_class = MetricFilter | ||
serializer_class = MetricSerializer | ||
search_fields = ('name', 'unit', 'eu_limit', 'description') | ||
ordering_fields = ('name', 'unit', 'eu_limit') | ||
|
||
class ReadingViewSet(viewsets.ModelViewSet): | ||
queryset = Reading.objects.all() | ||
filter_backends = FILTERS | ||
filter_class = ReadingFilter | ||
serializer_class = ReadingSerializer | ||
search_fields = ('sensor__name', 'message__text', 'value') | ||
ordering_fields = ('sensor', 'message', 'metric', 'value', 'recorded') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters