cqtu/api/views.py

85 lines
2.8 KiB
Python
Raw Normal View History

2020-02-02 02:10:33 +01:00
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import viewsets, generics, filters
2020-02-02 01:48:10 +01:00
from rest_framework.permissions import IsAuthenticated, IsAdminUser
2020-02-02 02:10:33 +01:00
from rest_framework.response import Response
2020-02-02 01:48:10 +01:00
from .serializers import ContestSerializer, BandSerializer, FrequencySerializer, EntryCategorySerializer, \
2020-02-02 02:10:33 +01:00
ReferenceSerializer, QSOSerializer, ShadowCallSerializer, UserSerializer
2020-02-02 01:48:10 +01:00
from contest.models import Contest, Band, Frequency, EntryCategory, Reference, QSO, ShadowCall
class ContestViewSet(viewsets.ReadOnlyModelViewSet):
2020-02-02 02:10:33 +01:00
"""
Resource to list and view all available contests. Use `current/` to get the current Contest.
"""
2020-02-02 01:48:10 +01:00
queryset = Contest.objects.all()
serializer_class = ContestSerializer
2020-02-02 02:10:33 +01:00
filterset_fields = ['shortName']
def get_object(self):
lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
if self.kwargs.get(lookup_url_kwarg) == "current":
obj = Contest.get_current_contest()
self.check_object_permissions(self.request, obj)
else:
obj = super(ContestViewSet, self).get_object()
return obj
2020-02-02 01:48:10 +01:00
class BandViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Band.objects.all()
serializer_class = BandSerializer
2020-02-02 02:10:33 +01:00
filterset_fields = ['name', 'contest']
2020-02-02 01:48:10 +01:00
class FrequencyViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Frequency.objects.all()
serializer_class = FrequencySerializer
2020-02-02 02:10:33 +01:00
filterset_fields = ['band', 'channel']
2020-02-02 01:48:10 +01:00
class EntryCategoryViewSet(viewsets.ReadOnlyModelViewSet):
queryset = EntryCategory.objects.all()
serializer_class = EntryCategorySerializer
2020-02-02 02:10:33 +01:00
filterset_fields = ['name']
2020-02-02 01:48:10 +01:00
class ReferenceViewSet(viewsets.ReadOnlyModelViewSet):
permission_classes = [IsAdminUser]
queryset = Reference.objects.all()
serializer_class = ReferenceSerializer
2020-02-02 02:10:33 +01:00
filterset_fields = ['name']
2020-02-02 01:48:10 +01:00
2020-02-02 02:10:33 +01:00
class QSOViewSet(viewsets.ModelViewSet):
2020-02-02 01:48:10 +01:00
permission_classes = [IsAuthenticated]
serializer_class = QSOSerializer
2020-02-02 02:10:33 +01:00
filterset_fields = ['time', 'ownNo', 'band', 'call', 'refStr']
2020-02-02 01:48:10 +01:00
def get_queryset(self):
return QSO.objects.filter(owner=self.request.user)
2020-02-02 02:10:33 +01:00
def perform_create(self, serializer):
return serializer.save(owner=self.request.user)
2020-02-02 01:48:10 +01:00
2020-02-02 02:10:33 +01:00
class UserProfileViewSet(generics.UpdateAPIView, viewsets.GenericViewSet):
permission_classes = [IsAuthenticated]
serializer_class = UserSerializer
def list(self, request, format=None):
user = request.user
serializer = UserSerializer(user)
return Response(serializer.data)
def get_queryset(self):
return self.request.user
2020-02-02 01:48:10 +01:00
class ShadowCallViewSet(viewsets.ReadOnlyModelViewSet):
permission_classes = [IsAdminUser]
queryset = ShadowCall.objects.all()
serializer_class = ShadowCallSerializer
2020-02-02 02:10:33 +01:00
filterset_fields = ['username', 'ref']