Skip to content

Commit

Permalink
refactor recipe views [expect to pass]
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcongliu committed Jan 18, 2024
1 parent 0632279 commit 3bd9ef7
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions app/recipe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,11 @@ def perform_create(self, serializer):
serializer.save(user=self.request.user)


class TagViewSet(mixins.DestroyModelMixin,
mixins.UpdateModelMixin,
mixins.ListModelMixin,
viewsets.GenericViewSet):
"""Manage tags in the database."""
serializer_class = serializers.TagSerializer
queryset = Tag.objects.all()
class BaseRecipeAttrViewSet(mixins.DestroyModelMixin,
mixins.UpdateModelMixin,
mixins.ListModelMixin,
viewsets.GenericViewSet):
"""Base viewset for recipe attributes."""
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticated]

Expand All @@ -54,16 +52,13 @@ def get_queryset(self):
return self.queryset.filter(user=self.request.user).order_by('-name')


class IngredientViewSet(mixins.DestroyModelMixin,
mixins.UpdateModelMixin,
mixins.ListModelMixin,
viewsets.GenericViewSet):
class TagViewSet(BaseRecipeAttrViewSet):
"""Manage tags in the database."""
serializer_class = serializers.TagSerializer
queryset = Tag.objects.all()


class IngredientViewSet(BaseRecipeAttrViewSet):
"""Manage ingredients in the database."""
serializer_class = serializers.IngredientSerializer
queryset = Ingredient.objects.all()
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticated]

def get_queryset(self):
"""Filter queryset to authenticated user."""
return self.queryset.filter(user=self.request.user).order_by('-name')

0 comments on commit 3bd9ef7

Please sign in to comment.