diff --git a/develop_requirements.txt b/develop_requirements.txt index e67b3bb..c251ebe 100644 --- a/develop_requirements.txt +++ b/develop_requirements.txt @@ -1,4 +1,5 @@ Django djangorestframework markdown # Markdown support for the browsable API. -django-filter +django-filter +pywebio diff --git a/dsite/settings_default.py b/dsite/settings_default.py index 3232664..1213a2f 100644 --- a/dsite/settings_default.py +++ b/dsite/settings_default.py @@ -45,6 +45,7 @@ INSTALLED_APPS = [ # user apps 'timer', + 'recipe', ] MIDDLEWARE = [ diff --git a/dsite/urls.py b/dsite/urls.py index 0dfa4f8..87236df 100644 --- a/dsite/urls.py +++ b/dsite/urls.py @@ -30,4 +30,5 @@ urlpatterns = [ path('api-auth/', include('rest_framework.urls', namespace='rest_framework')), path('api-auth-token/', views.obtain_auth_token, name='api-auth-token'), url('timer/', include('timer.urls')), + url('recipe/', include('recipe.urls')), ] diff --git a/recipe/__init__.py b/recipe/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/recipe/admin.py b/recipe/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/recipe/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/recipe/apps.py b/recipe/apps.py new file mode 100644 index 0000000..9133199 --- /dev/null +++ b/recipe/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class RecipeConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'recipe' diff --git a/recipe/migrations/0001_initial.py b/recipe/migrations/0001_initial.py new file mode 100644 index 0000000..4b5deac --- /dev/null +++ b/recipe/migrations/0001_initial.py @@ -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)), + ], + ), + ] diff --git a/recipe/migrations/__init__.py b/recipe/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/recipe/models.py b/recipe/models.py new file mode 100644 index 0000000..70c06ed --- /dev/null +++ b/recipe/models.py @@ -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) diff --git a/recipe/serializers.py b/recipe/serializers.py new file mode 100644 index 0000000..ffd887e --- /dev/null +++ b/recipe/serializers.py @@ -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__' diff --git a/recipe/tests.py b/recipe/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/recipe/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/recipe/urls.py b/recipe/urls.py new file mode 100644 index 0000000..eb15e99 --- /dev/null +++ b/recipe/urls.py @@ -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\d+)$', views.RecipeAPI.as_view(), name='recipe-detail'), + url(r'^recipe/$', views.RecipeListAPI.as_view()), +] diff --git a/recipe/views.py b/recipe/views.py new file mode 100644 index 0000000..2427777 --- /dev/null +++ b/recipe/views.py @@ -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 +