-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1aba889
commit 6e7bcdb
Showing
5 changed files
with
68 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,4 +26,5 @@ | |
name='api-docs' | ||
), | ||
path('api/user/', include('user.urls')), | ||
path('api/recipe/', include('recipe.urls')), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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',) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |