Skip to content

Commit

Permalink
recipe listing api implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian333Dev committed Aug 6, 2023
1 parent 1aba889 commit 6e7bcdb
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@
name='api-docs'
),
path('api/user/', include('user.urls')),
path('api/recipe/', include('recipe.urls')),
]
15 changes: 15 additions & 0 deletions app/recipe/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Serializers for recipe API

from rest_framework import serializers

from core.models import Recipe


class RecipeSerializer(serializers.ModelSerializer):
"""Serializer for recipe objects"""
# Meta class is a configuration for the serializer
class Meta:
model = Recipe
fields = ('id', 'title', 'time_minutes', 'price', 'link')
# Make sure that the id is read only
read_only_fields = ('id',)
2 changes: 1 addition & 1 deletion app/recipe/test_recipe_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def test_retrieve_recipes(self):

self.assertEqual(res.status_code, status.HTTP_200_OK)
self.assertEqual(res.data, serializer.data)

def test_recipes_limited_to_user(self):
"""Test retrieving recipes for user"""
other_user = get_user_model().objects.create_user(
Expand Down
20 changes: 20 additions & 0 deletions app/recipe/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# URL mapping for recipe app

from django.urls import path, include

from rest_framework.routers import DefaultRouter

from recipe import views

# Create a router object
router = DefaultRouter()
# Register the recipe viewset with the router
router.register('recipes', views.RecipeViewSet)

# Define the app name
app_name = 'recipe'

urlpatterns = [
# Register the router with the urlpatterns
path('', include(router.urls))
]
33 changes: 31 additions & 2 deletions app/recipe/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
from django.shortcuts import render
# Views for recipe API

# Create your views here.
from rest_framework import viewsets
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import IsAuthenticated

from core.models import Recipe
from recipe import serializers


class RecipeViewSet(viewsets.ModelViewSet):
"""Manage recipes in the database"""
serializer_class = serializers.RecipeSerializer
# Queryset is used to retrieve objects from the database
queryset = Recipe.objects.all()
# Add authentication and permission classes
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated,)

# Override the get_queryset function to return objects for the
# authenticated user only
def get_queryset(self):
"""Return objects for the current authenticated user only"""
# Return objects for the current authenticated user only
return self.queryset.filter(user=self.request.user).order_by('-id')

# Override the perform_create function to add the user to the recipe
# object
def perform_create(self, serializer):
"""Create a new recipe"""
# Create a new recipe
serializer.save(user=self.request.user)

0 comments on commit 6e7bcdb

Please sign in to comment.