[A]add recipe

This commit is contained in:
Ching 2021-08-24 18:09:00 +08:00
parent d0c1b65632
commit 558065a8f1
13 changed files with 109 additions and 1 deletions

View File

@ -1,4 +1,5 @@
Django Django
djangorestframework djangorestframework
markdown # Markdown support for the browsable API. markdown # Markdown support for the browsable API.
django-filter django-filter
pywebio

View File

@ -45,6 +45,7 @@ INSTALLED_APPS = [
# user apps # user apps
'timer', 'timer',
'recipe',
] ]
MIDDLEWARE = [ MIDDLEWARE = [

View File

@ -30,4 +30,5 @@ urlpatterns = [
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')), path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
path('api-auth-token/', views.obtain_auth_token, name='api-auth-token'), path('api-auth-token/', views.obtain_auth_token, name='api-auth-token'),
url('timer/', include('timer.urls')), url('timer/', include('timer.urls')),
url('recipe/', include('recipe.urls')),
] ]

0
recipe/__init__.py Normal file
View File

3
recipe/admin.py Normal file
View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
recipe/apps.py Normal file
View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class RecipeConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'recipe'

View File

@ -0,0 +1,25 @@
# Generated by Django 3.2.6 on 2021-08-24 07:31
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Recipe',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=128)),
('recipe_type', models.CharField(max_length=32)),
('note', models.TextField(null=True)),
('rate', models.IntegerField(default=0)),
('difficulty', models.IntegerField(default=0)),
],
),
]

View File

9
recipe/models.py Normal file
View File

@ -0,0 +1,9 @@
from django.db import models
# Create your models here.
class Recipe(models.Model):
name = models.CharField(max_length=128)
recipe_type = models.CharField(max_length=32)
note = models.TextField(null=True)
rate = models.IntegerField(default=0)
difficulty = models.IntegerField(default=0)

9
recipe/serializers.py Normal file
View File

@ -0,0 +1,9 @@
from django.contrib.auth.models import User, Group
from rest_framework import serializers
import recipe.models
class RecipeSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = recipe.models.Recipe
fields = '__all__'

3
recipe/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

13
recipe/urls.py Normal file
View File

@ -0,0 +1,13 @@
from django.conf.urls import include, url
# from django.core.urlresolvers import reverse
from django.urls import path
from rest_framework import routers
from recipe import views
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
url(r'^recipe/(?P<pk>\d+)$', views.RecipeAPI.as_view(), name='recipe-detail'),
url(r'^recipe/$', views.RecipeListAPI.as_view()),
]

37
recipe/views.py Normal file
View File

@ -0,0 +1,37 @@
from django.shortcuts import render
# Create your views here.
import django.views.generic
from django.http import JsonResponse
from django.urls import reverse
from rest_framework import authentication, permissions, status, viewsets
import rest_framework.generics
from rest_framework.response import Response
from rest_framework.views import APIView
import recipe.models
import recipe.serializers
class RecipeAPI(rest_framework.generics.RetrieveUpdateAPIView,
rest_framework.generics.ListAPIView):
authentication_classes = (authentication.TokenAuthentication,
authentication.SessionAuthentication,
authentication.BasicAuthentication)
permission_classes = (permissions.IsAuthenticated,)
queryset = recipe.models.Recipe.objects.all()
serializer_class = recipe.serializers.RecipeSerializer
class RecipeListAPI(rest_framework.generics.ListAPIView,
rest_framework.generics.CreateAPIView):
authentication_classes = (authentication.TokenAuthentication,
authentication.SessionAuthentication,
authentication.BasicAuthentication)
permission_classes = (permissions.IsAuthenticated,)
queryset = recipe.models.Recipe.objects.all()
serializer_class = recipe.serializers.RecipeSerializer