Skip to content

Commit d07405c

Browse files
committed
implemented recipe listing API
1 parent d9494dc commit d07405c

3 files changed

Lines changed: 44 additions & 3 deletions

File tree

app/app/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@
2828
SpectacularSwaggerView.as_view(url_name='api-schema'),
2929
name='api-docs',
3030
),
31-
path('api/user/', include('user.urls'))
31+
path('api/user/', include('user.urls')),
32+
path('api/recipe/', include('recipe.urls')),
3233
]

app/recipe/urls.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
URLs mapping for the recipe app
3+
"""
4+
from django.urls import (
5+
path,
6+
include,
7+
)
8+
9+
from rest_framework.routers import DefaultRouter
10+
11+
from recipe import views
12+
13+
14+
router = DefaultRouter()
15+
router.register('recipes', views.RecipeViewSet)
16+
17+
app_name = 'recipe'
18+
19+
urlpatterns = [
20+
path('', include(router.urls)),
21+
]

app/recipe/views.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1-
from django.shortcuts import render # noqa
1+
"""
2+
Views for the recipe APIs
3+
"""
4+
from rest_framework import viewsets
5+
from rest_framework.authentication import TokenAuthentication
6+
from rest_framework.permissions import IsAuthenticated
7+
8+
from core.models import Recipe
9+
from recipe import serializers
10+
11+
12+
class RecipeViewSet(viewsets.ModelViewSet):
13+
"""View for manage recipe APIs"""
14+
serializer_class = serializers.RecipeSerializer
15+
queryset = Recipe.objects.all()
16+
authentication_classes = [TokenAuthentication]
17+
permission_classes = [IsAuthenticated]
18+
19+
def get_queryset(self):
20+
"""Retrieve recipes for authenticated user"""
21+
return self.queryset.filter(user=self.request.user).order_by('-id')
222

3-
# Create your views here.

0 commit comments

Comments
 (0)