from django.shortcuts import get_object_or_404
from rest_framework import viewsets, permissions  # , serializers
from .models import Stat, Activity
from .permissions import IsAPIUser
from .serializers import ActivitySerializer, ActivityListSerializer, StatSerializer

# Create your views here.


# class UserViewSet(viewsets.ModelViewSet):
#     permission_classes = (permissions.IsAuthenticated,
#                           IsAPIUser)
#
#     def list(self, request, *args, **kwargs):
#         return []


class ActivityViewSet(viewsets.ModelViewSet):
    queryset = Activity.objects.all()
    serializer_class = ActivitySerializer

    # def get_queryset(self):
    #     return self.request.user.activity_set.all()

    def get_serializer_class(self):
        if self.action == 'list':
            return ActivitySerializer
        else:
            return ActivityListSerializer


class StatViewSet(viewsets.ModelViewSet):
    serializer_class = StatSerializer

    def get_queryset(self):
        activity = get_object_or_404(Activity, pk=self.kwargs['activity_pk'])
        return Stat.objects.all().filter(
            # user=self.request.user,
            activity=activity)

    def get_serializer_context(self):
        context = super().get_serializer_context().copy()
        activity = get_object_or_404(Activity, pk=self.kwargs['activity_pk'])
        context['activity'] = activity
        return context

    # def perform_create(self, serializer):
    #     serializers.save(user=self.request.user)

from django.shortcuts import get_object_or_404
from rest_framework import viewsets, mixins, permissions  # , serializers
from .models import Stat, Activity
# from .permissions import IsAPIUser
from .serializers import ActivitySerializer, ActivityListSerializer, StatSerializer

# Create your views here.


# class UserViewSet(viewsets.GenericViewSet, mixins.CreateModelMixin):
#     permission_classes = (permissions.IsAuthenticated,
#                           IsAPIUser)


class ActivityViewSet(viewsets.ModelViewSet):
    queryset = Activity.objects.all()
    serializer_class = ActivitySerializer

    # def get_queryset(self):
    #     return self.request.user.activity_set.all()

    def get_serializer_class(self):
        if self.action == 'list':
            return ActivitySerializer
        else:
            return ActivityListSerializer


class StatViewSet(viewsets.GenericViewSet, mixins.CreateModelMixin,
                  mixins.UpdateModelMixin, mixins.DestroyModelMixin):
    serializer_class = StatSerializer

    def get_queryset(self):
        activity = get_object_or_404(Activity, pk=self.kwargs['activity_pk'])
        return Stat.objects.all().filter(
            # user=self.request.user,
            activity=activity)

    def get_serializer_context(self):
        context = super().get_serializer_context().copy()
        activity = get_object_or_404(Activity, pk=self.kwargs['activity_pk'])
        context['activity'] = activity
        return context

    # def perform_create(self, serializer):
    #     serializers.save(user=self.request.user)

Update StatViewSet to generic, add necessary mixins
Update StatViewSet to generic, add necessary mixins

  from django.shortcuts import get_object_or_404
- from rest_framework import viewsets, permissions  # , serializers
+ from rest_framework import viewsets, mixins, permissions  # , serializers
  from .models import Stat, Activity
- from .permissions import IsAPIUser
+ # from .permissions import IsAPIUser
  from .serializers import ActivitySerializer, ActivityListSerializer, StatSerializer
  
  # Create your views here.
  
  
- # class UserViewSet(viewsets.ModelViewSet):
+ # class UserViewSet(viewsets.GenericViewSet, mixins.CreateModelMixin):
  #     permission_classes = (permissions.IsAuthenticated,
  #                           IsAPIUser)
- #
- #     def list(self, request, *args, **kwargs):
- #         return []
  
  
  class ActivityViewSet(viewsets.ModelViewSet):
      queryset = Activity.objects.all()
      serializer_class = ActivitySerializer
  
      # def get_queryset(self):
      #     return self.request.user.activity_set.all()
  
      def get_serializer_class(self):
          if self.action == 'list':
              return ActivitySerializer
          else:
              return ActivityListSerializer
  
  
- class StatViewSet(viewsets.ModelViewSet):
+ class StatViewSet(viewsets.GenericViewSet, mixins.CreateModelMixin,
+                   mixins.UpdateModelMixin, mixins.DestroyModelMixin):
      serializer_class = StatSerializer
  
      def get_queryset(self):
          activity = get_object_or_404(Activity, pk=self.kwargs['activity_pk'])
          return Stat.objects.all().filter(
              # user=self.request.user,
              activity=activity)
  
      def get_serializer_context(self):
          context = super().get_serializer_context().copy()
          activity = get_object_or_404(Activity, pk=self.kwargs['activity_pk'])
          context['activity'] = activity
          return context
  
      # def perform_create(self, serializer):
      #     serializers.save(user=self.request.user)
  
Update StatViewSet to generic, add necessary mixins
## Code Before:
from django.shortcuts import get_object_or_404
from rest_framework import viewsets, permissions  # , serializers
from .models import Stat, Activity
from .permissions import IsAPIUser
from .serializers import ActivitySerializer, ActivityListSerializer, StatSerializer

# Create your views here.


# class UserViewSet(viewsets.ModelViewSet):
#     permission_classes = (permissions.IsAuthenticated,
#                           IsAPIUser)
#
#     def list(self, request, *args, **kwargs):
#         return []


class ActivityViewSet(viewsets.ModelViewSet):
    queryset = Activity.objects.all()
    serializer_class = ActivitySerializer

    # def get_queryset(self):
    #     return self.request.user.activity_set.all()

    def get_serializer_class(self):
        if self.action == 'list':
            return ActivitySerializer
        else:
            return ActivityListSerializer


class StatViewSet(viewsets.ModelViewSet):
    serializer_class = StatSerializer

    def get_queryset(self):
        activity = get_object_or_404(Activity, pk=self.kwargs['activity_pk'])
        return Stat.objects.all().filter(
            # user=self.request.user,
            activity=activity)

    def get_serializer_context(self):
        context = super().get_serializer_context().copy()
        activity = get_object_or_404(Activity, pk=self.kwargs['activity_pk'])
        context['activity'] = activity
        return context

    # def perform_create(self, serializer):
    #     serializers.save(user=self.request.user)

## Instruction:
Update StatViewSet to generic, add necessary mixins
## Code After:
from django.shortcuts import get_object_or_404
from rest_framework import viewsets, mixins, permissions  # , serializers
from .models import Stat, Activity
# from .permissions import IsAPIUser
from .serializers import ActivitySerializer, ActivityListSerializer, StatSerializer

# Create your views here.


# class UserViewSet(viewsets.GenericViewSet, mixins.CreateModelMixin):
#     permission_classes = (permissions.IsAuthenticated,
#                           IsAPIUser)


class ActivityViewSet(viewsets.ModelViewSet):
    queryset = Activity.objects.all()
    serializer_class = ActivitySerializer

    # def get_queryset(self):
    #     return self.request.user.activity_set.all()

    def get_serializer_class(self):
        if self.action == 'list':
            return ActivitySerializer
        else:
            return ActivityListSerializer


class StatViewSet(viewsets.GenericViewSet, mixins.CreateModelMixin,
                  mixins.UpdateModelMixin, mixins.DestroyModelMixin):
    serializer_class = StatSerializer

    def get_queryset(self):
        activity = get_object_or_404(Activity, pk=self.kwargs['activity_pk'])
        return Stat.objects.all().filter(
            # user=self.request.user,
            activity=activity)

    def get_serializer_context(self):
        context = super().get_serializer_context().copy()
        activity = get_object_or_404(Activity, pk=self.kwargs['activity_pk'])
        context['activity'] = activity
        return context

    # def perform_create(self, serializer):
    #     serializers.save(user=self.request.user)
