2015-09-09 02:12:36 -06:00
|
|
|
from django.http import Http404
|
2017-08-12 09:25:53 -06:00
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
from django.utils.timezone import now
|
2015-02-14 14:47:31 -07:00
|
|
|
|
2017-08-12 09:25:53 -06:00
|
|
|
from rest_framework import viewsets, mixins, status
|
2015-09-09 02:12:36 -06:00
|
|
|
from rest_framework.response import Response
|
2014-10-15 13:20:46 -06:00
|
|
|
|
2015-06-21 18:44:25 -06:00
|
|
|
from network.api.perms import StationOwnerCanEditPermission
|
2015-05-06 02:55:54 -06:00
|
|
|
from network.api import serializers, filters
|
2015-07-23 12:59:40 -06:00
|
|
|
from network.base.models import Data, Station
|
2014-10-15 13:20:46 -06:00
|
|
|
|
|
|
|
|
2015-05-06 02:55:54 -06:00
|
|
|
class DataView(viewsets.ModelViewSet, mixins.UpdateModelMixin):
|
2014-10-15 13:20:46 -06:00
|
|
|
queryset = Data.objects.all()
|
|
|
|
serializer_class = serializers.DataSerializer
|
2016-04-25 08:54:38 -06:00
|
|
|
filter_class = filters.DataViewFilter
|
2014-12-13 10:49:15 -07:00
|
|
|
permission_classes = [
|
|
|
|
StationOwnerCanEditPermission
|
|
|
|
]
|
2015-02-14 07:15:26 -07:00
|
|
|
|
2017-08-12 09:25:53 -06:00
|
|
|
def update(self, request, *args, **kwargs):
|
|
|
|
if request.data.get('demoddata'):
|
|
|
|
instance = self.get_object()
|
|
|
|
instance.demoddata.create(payload_demod=request.data.get('demoddata'))
|
|
|
|
|
|
|
|
super(DataView, self).update(request, *args, **kwargs)
|
|
|
|
return Response(status=status.HTTP_200_OK)
|
|
|
|
|
2015-02-14 12:58:11 -07:00
|
|
|
|
|
|
|
class JobView(viewsets.ReadOnlyModelViewSet):
|
|
|
|
queryset = Data.objects.filter(payload='')
|
|
|
|
serializer_class = serializers.JobSerializer
|
2015-05-06 02:55:54 -06:00
|
|
|
filter_class = filters.DataViewFilter
|
2015-02-14 12:58:11 -07:00
|
|
|
filter_fields = ('ground_station')
|
2015-02-14 14:47:31 -07:00
|
|
|
|
|
|
|
def get_queryset(self):
|
2015-06-21 18:44:25 -06:00
|
|
|
queryset = self.queryset.filter(start__gte=now())
|
2016-01-22 10:47:27 -07:00
|
|
|
gs_id = self.request.query_params.get('ground_station', None)
|
2015-06-21 18:44:25 -06:00
|
|
|
if gs_id and self.request.user.is_authenticated():
|
|
|
|
gs = get_object_or_404(Station, id=gs_id)
|
|
|
|
if gs.owner == self.request.user:
|
|
|
|
gs.last_seen = now()
|
|
|
|
gs.save()
|
|
|
|
return queryset
|
2015-09-09 02:12:36 -06:00
|
|
|
|
|
|
|
|
|
|
|
class SettingsView(viewsets.ReadOnlyModelViewSet):
|
|
|
|
queryset = Station.objects.all()
|
|
|
|
lookup_field = 'uuid'
|
|
|
|
|
|
|
|
def list(self, request):
|
|
|
|
raise Http404()
|
|
|
|
|
|
|
|
def retrieve(self, request, queryset=queryset, uuid=None):
|
|
|
|
station = get_object_or_404(queryset, uuid=uuid)
|
|
|
|
serializer = serializers.SettingsSerializer(station)
|
|
|
|
return Response(serializer.data)
|