From 884655bedac6feb227f5b865d6fafade20006486 Mon Sep 17 00:00:00 2001 From: Sam Collins Date: Sun, 9 Oct 2016 01:03:54 +0100 Subject: [PATCH] Added Basic API and Improved Error Logging --- .gitignore | 2 +- config/settings/common.py | 5 +- config/urls.py | 10 +++ sensor_portal/sensors/serializers.py | 21 ++++++ sensor_portal/sensors/templatetags/sensors.py | 3 +- sensor_portal/sensors/views.py | 68 +++++++++++++++---- sensor_portal/templates/base.html | 5 ++ 7 files changed, 98 insertions(+), 16 deletions(-) create mode 100644 sensor_portal/sensors/serializers.py diff --git a/.gitignore b/.gitignore index 143efba..b12d27c 100644 --- a/.gitignore +++ b/.gitignore @@ -61,7 +61,7 @@ node_modules/ # User-uploaded media sensor_portal/media/ - +src/ staticfiles/ diff --git a/config/settings/common.py b/config/settings/common.py index 9de19d2..3167a42 100644 --- a/config/settings/common.py +++ b/config/settings/common.py @@ -29,7 +29,7 @@ 'django.contrib.staticfiles', # Useful template tags: - # 'django.contrib.humanize', + 'django.contrib.humanize', # Admin 'django.contrib.admin', @@ -41,6 +41,7 @@ 'allauth.socialaccount', # registration # 'user_sessions', # Better session managment 'rest_framework', + 'geoposition' ) # Apps specific for this project go here. @@ -275,4 +276,4 @@ GEOPOSITION_GOOGLE_MAPS_API_KEY = 'AIzaSyDE5DzzKxdwrLutlDBh1YB56UnsVAJ0ulM' -VERSION = '0.0.1' +VERSION = '0.0.2' diff --git a/config/urls.py b/config/urls.py index 1ca62d6..ac82216 100644 --- a/config/urls.py +++ b/config/urls.py @@ -11,6 +11,15 @@ from django.views.defaults import page_not_found, server_error from django.http import HttpResponseServerError +from sensor_portal.sensors.views import SensorViewSet, MetricViewSet, ReadingViewSet +from rest_framework import routers + +admin.site.site_header = 'Sensor Panel Admin' + +router = routers.DefaultRouter() +router.register(r'sensors', SensorViewSet) +router.register(r'metrics', MetricViewSet) +router.register(r'readings', ReadingViewSet) urlpatterns = [ url(r'^$', TemplateView.as_view(template_name='pages/home.html'), name='home'), @@ -25,6 +34,7 @@ # Your stuff: custom urls includes go here + url(r'^api/v1/', include(router.urls, namespace='v1')), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) diff --git a/sensor_portal/sensors/serializers.py b/sensor_portal/sensors/serializers.py new file mode 100644 index 0000000..10e4fce --- /dev/null +++ b/sensor_portal/sensors/serializers.py @@ -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') diff --git a/sensor_portal/sensors/templatetags/sensors.py b/sensor_portal/sensors/templatetags/sensors.py index b48e908..b903306 100644 --- a/sensor_portal/sensors/templatetags/sensors.py +++ b/sensor_portal/sensors/templatetags/sensors.py @@ -11,5 +11,6 @@ def error_reporting(scheme=None): from raven.contrib.django.models import client dsn = client.get_public_dsn(scheme) if dsn: - return 'Raven.config(' + dsn + ').install()' + html = 'Raven.config({}).install();'.format(dsn) + return safe_html(html) return 'console.warn(\'DEBUG = True\')' diff --git a/sensor_portal/sensors/views.py b/sensor_portal/sensors/views.py index 0e12add..19fcf1a 100755 --- a/sensor_portal/sensors/views.py +++ b/sensor_portal/sensors/views.py @@ -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') diff --git a/sensor_portal/templates/base.html b/sensor_portal/templates/base.html index bcbd94c..0da68eb 100644 --- a/sensor_portal/templates/base.html +++ b/sensor_portal/templates/base.html @@ -48,6 +48,11 @@ + {% if request.user.is_staff %} + + {% endif %}