diff --git a/app/recipe/views.py b/app/recipe/views.py index 5517aed..445bb23 100644 --- a/app/recipe/views.py +++ b/app/recipe/views.py @@ -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] @@ -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')