from rest_framework.permissions import IsAuthenticated, IsAdminUser from rest_framework import viewsets from .serializers import ContestSerializer, BandSerializer, FrequencySerializer, EntryCategorySerializer, \ ReferenceSerializer, QSOSerializer, ShadowCallSerializer, UserSerializer from contest.models import Contest, Band, Frequency, EntryCategory, Reference, QSO, ShadowCall, User class ContestViewSet(viewsets.ReadOnlyModelViewSet): """ Resource to list and view all available contests. Use `current/` to get the current Contest. """ queryset = Contest.objects.all() serializer_class = ContestSerializer 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 class BandViewSet(viewsets.ReadOnlyModelViewSet): queryset = Band.objects.all() serializer_class = BandSerializer filterset_fields = ['name', 'contest'] class FrequencyViewSet(viewsets.ReadOnlyModelViewSet): queryset = Frequency.objects.all() serializer_class = FrequencySerializer filterset_fields = ['band', 'channel'] class EntryCategoryViewSet(viewsets.ReadOnlyModelViewSet): queryset = EntryCategory.objects.all() serializer_class = EntryCategorySerializer filterset_fields = ['name'] class ReferenceViewSet(viewsets.ReadOnlyModelViewSet): permission_classes = [IsAdminUser] queryset = Reference.objects.all() serializer_class = ReferenceSerializer filterset_fields = ['name'] class QSOViewSet(viewsets.ModelViewSet): permission_classes = [IsAuthenticated] serializer_class = QSOSerializer filterset_fields = ['time', 'ownNo', 'band', 'call', 'refStr'] def get_queryset(self): return QSO.objects.filter(owner=self.request.user) def perform_create(self, serializer): return serializer.save(owner=self.request.user) 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. """ 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 class ShadowCallViewSet(viewsets.ReadOnlyModelViewSet): permission_classes = [IsAdminUser] queryset = ShadowCall.objects.all() serializer_class = ShadowCallSerializer filterset_fields = ['username', 'ref']