Skip to content

Commit

Permalink
Added Basic API and Improved Error Logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Collins committed Oct 9, 2016
1 parent 1490c23 commit 884655b
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ node_modules/
# User-uploaded media
sensor_portal/media/


src/


staticfiles/
Expand Down
5 changes: 3 additions & 2 deletions config/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
'django.contrib.staticfiles',

# Useful template tags:
# 'django.contrib.humanize',
'django.contrib.humanize',

# Admin
'django.contrib.admin',
Expand All @@ -41,6 +41,7 @@
'allauth.socialaccount', # registration
# 'user_sessions', # Better session managment
'rest_framework',
'geoposition'
)

# Apps specific for this project go here.
Expand Down Expand Up @@ -275,4 +276,4 @@

GEOPOSITION_GOOGLE_MAPS_API_KEY = 'AIzaSyDE5DzzKxdwrLutlDBh1YB56UnsVAJ0ulM'

VERSION = '0.0.1'
VERSION = '0.0.2'
10 changes: 10 additions & 0 deletions config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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)

Expand Down
21 changes: 21 additions & 0 deletions sensor_portal/sensors/serializers.py
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')
3 changes: 2 additions & 1 deletion sensor_portal/sensors/templatetags/sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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\')'
68 changes: 56 additions & 12 deletions sensor_portal/sensors/views.py
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')
5 changes: 5 additions & 0 deletions sensor_portal/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
<li class="nav-item">
<a class="nav-link" href="{% url 'users:detail' request.user.username %}">{% trans "My Profile" %}</a>
</li>
{% if request.user.is_staff %}
<li class="nav-item">
<a class="nav-link" href="{% url 'admin:index' %}">{% trans "Admin" %}</a>
</li>
{% endif %}
<li class="nav-item">
<a class="nav-link" href="{% url 'account_logout' %}">{% trans "Sign Out" %}</a>
</li>
Expand Down

0 comments on commit 884655b

Please sign in to comment.