HHHMHA/recipe-app-api

View on GitHub
app/recipe/views.py

Summary

Maintainability
A
0 mins
Test Coverage
from rest_framework import viewsets, mixins
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import IsAuthenticated
from .serializers import TagSerializer, IngredientSerializer, RecipeSerializer
from core.models import Tag, Ingredient, Recipe


class BaseRecipeAttrViewSet(viewsets.GenericViewSet,
                            mixins.ListModelMixin,
                            mixins.CreateModelMixin):
    """Base class for a user owned recipe attributes"""

    authentication_classes = (TokenAuthentication,)
    permission_classes = (IsAuthenticated,)

    def get_queryset(self):
        """Return objects for the current authenticated user only"""

        qs = super(BaseRecipeAttrViewSet, self).get_queryset()
        return qs.filter(user=self.request.user).order_by('-name')

    def perform_create(self, serializer):
        """Create a new object"""

        serializer.save(user=self.request.user)


class TagViewSet(BaseRecipeAttrViewSet):
    """Manage tags in the database"""

    serializer_class = TagSerializer
    queryset = Tag.objects.all()


class IngredientViewSet(BaseRecipeAttrViewSet):
    """Manage ingredients in the database"""

    serializer_class = IngredientSerializer
    queryset = Ingredient.objects.all()


class RecipeViewSet(viewsets.ModelViewSet):
    """Manage Recipe in the database"""

    serializer_class = RecipeSerializer
    queryset = Recipe.objects.all()
    authentication_classes = (TokenAuthentication,)
    permission_classes = (IsAuthenticated,)

    # Must override because the old one order by name
    def get_queryset(self):
        """Retrieve the recipes for the authenticated user"""

        qs = super(RecipeViewSet, self).get_queryset()
        return qs.filter(user=self.request.user)