cqtu/api/views.py

92 lines
3.0 KiB
Python
Raw Normal View History

2020-02-02 01:48:10 +01:00
from rest_framework.permissions import IsAuthenticated, IsAdminUser
2020-03-17 20:24:16 +01:00
from rest_framework import viewsets
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
from contest.models import Contest, Band, Frequency, EntryCategory, Reference, QSO, ShadowCall, User
2020-02-02 01:48:10 +01:00
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
class UserProfileViewSet(viewsets.ModelViewSet):
"""
Resource to view user-profiles, currently restricted to the current user's profile.
Use `me/` to get the profile of the currently logged in user.
"""
2020-02-02 02:10:33 +01:00
permission_classes = [IsAuthenticated]
serializer_class = UserSerializer
def get_queryset(self):
return User.objects.filter(id=self.request.user.id)
def get_object(self):
lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
if self.kwargs.get(lookup_url_kwarg) == "me":
obj = self.request.user
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 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']