Skip to content

Commit

Permalink
Implement tag and ingredient filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
pavlo-myskov committed Nov 14, 2023
1 parent eca3517 commit 566d028
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion app/recipe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ def upload_image(self, request, pk=None):
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


@extend_schema_view(
list=extend_schema(
parameters=[
OpenApiParameter(
'assigned_only',
OpenApiTypes.INT, enum=[0, 1],
description='Filter by items assigned to recipes.',
)
]
)
)
class BaserecipeAttrViewSet(mixins.DestroyModelMixin,
mixins.UpdateModelMixin,
mixins.ListModelMixin,
Expand All @@ -124,7 +135,15 @@ class BaserecipeAttrViewSet(mixins.DestroyModelMixin,

def get_queryset(self):
"""Filter queryset to authenticated user."""
return self.queryset.filter(user=self.request.user).order_by('-name')
assigned_only = bool(
int(self.request.query_params.get('assigned_only', 0))
)
queryset = self.queryset
if assigned_only:
queryset = queryset.filter(recipe__isnull=False)
return queryset.filter(
user=self.request.user
).order_by('-name').distinct()


class TagViewSet(BaserecipeAttrViewSet):
Expand Down

0 comments on commit 566d028

Please sign in to comment.