Complete primitive backend

This commit is contained in:
Alexander Laevens
2022-11-22 16:16:34 -07:00
commit 35fc396050
29 changed files with 840 additions and 0 deletions

View File

View File

@@ -0,0 +1,43 @@
from django.contrib import admin
from django.contrib.contenttypes.admin import GenericTabularInline
from users.models import User
import nested_admin
from api.models import *
# Register your models here.
class IngredientInline(nested_admin.NestedGenericTabularInline):
model = Ingredient
extra = 0
class UserInline(nested_admin.NestedTabularInline):
model = User
fields = ("username",)
readonly_fields = ("username",)
extra = 0
def has_add_permission(self, request, obj=None):
return False
def has_delete_permission(self, request, obj=None):
return False
class RecipeInline(nested_admin.NestedTabularInline):
model = Recipe
inlines = (IngredientInline,)
extra = 0
@admin.register(Recipe)
class RecipeAdmin(nested_admin.NestedModelAdmin):
list_display = ("name",)
inlines = (IngredientInline,)
@admin.register(Homegroup)
class HomegroupAdmin(nested_admin.NestedModelAdmin):
list_display = ("id", "name")
inlines = (UserInline, RecipeInline, IngredientInline)
@admin.register(List)
class ListAdmin(nested_admin.NestedModelAdmin):
list_display = ("id",)
inlines = (RecipeInline, IngredientInline)

View File

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

View File

@@ -0,0 +1,47 @@
# Generated by Django 4.1.3 on 2022-11-22 20:42
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
('contenttypes', '0002_remove_content_type_name'),
]
operations = [
migrations.CreateModel(
name='Homegroup',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
],
),
migrations.CreateModel(
name='List',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
],
),
migrations.CreateModel(
name='Recipe',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=50)),
('homegroup', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='recipes', to='api.homegroup')),
('list', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='recipes', to='api.list')),
],
),
migrations.CreateModel(
name='Ingredient',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('object_id', models.PositiveBigIntegerField()),
('name', models.CharField(max_length=50)),
('in_stock', models.BooleanField(default=False)),
('content_type', models.ForeignKey(limit_choices_to=models.Q(models.Q(('app_label', 'api'), ('model', 'recipe')), models.Q(('app_label', 'api'), ('model', 'list')), _connector='OR'), on_delete=django.db.models.deletion.CASCADE, to='contenttypes.contenttype')),
],
),
]

View File

@@ -0,0 +1,19 @@
# Generated by Django 4.1.3 on 2022-11-22 21:43
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('api', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='homegroup',
name='name',
field=models.CharField(default="Alex's Kitchen", max_length=50),
preserve_default=False,
),
]

View File

@@ -0,0 +1,33 @@
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
# Create your models here.
class Ingredient(models.Model):
limit = models.Q(app_label="api", model="recipe") | models.Q(app_label="api", model="list")
content_type = models.ForeignKey(ContentType, limit_choices_to=limit, on_delete=models.CASCADE)
object_id = models.PositiveBigIntegerField()
content_object = GenericForeignKey()
name = models.CharField(max_length=50)
in_stock = models.BooleanField(default=False)
class List(models.Model):
# Foreign Key Recipe -> List [as recipes]
extra_ingredients = GenericRelation(Ingredient, related_query_name="extra_ingredients")
class Homegroup(models.Model):
# Foreign Key Recipe -> Homegroup [as recipes]
# Foreign Key User -> Homegroup [as users]
name = models.CharField(max_length=50)
class Recipe(models.Model):
name = models.CharField(max_length=50)
homegroup = models.ForeignKey(Homegroup, related_name="recipes", on_delete=models.CASCADE)
list = models.ForeignKey(List, related_name="recipes", on_delete=models.SET_NULL, blank=True, null=True)
ingredients = GenericRelation(Ingredient, related_query_name="ingredients")

View File

@@ -0,0 +1,24 @@
from rest_framework import serializers
from api.models import *
class RecipeSerializer(serializers.ModelSerializer):
ingredients = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
class Meta:
model = Recipe
fields = ["id", "name", "ingredients"]
read_only_fields = ["id"]
class IngredientSerializer(serializers.ModelSerializer):
class Meta:
model = Ingredient
fields = ["id", "name", "in_stock"]
read_only_fields = ["id"]
class HomegroupSerializer(serializers.ModelSerializer):
users = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
recipes = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
class Meta:
model = Homegroup
fields = ["id", "recipes", "users"]

View File

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

View File

@@ -0,0 +1,12 @@
from django.urls import include, path
from rest_framework import routers
from api import views
router = routers.DefaultRouter()
router.register(r'recipes', views.RecipeView)
router.register(r'ingredients', views.IngredientView)
router.register(r'homegroups', views.HomegroupView)
urlpatterns = [
path('', include(router.urls))
]

View File

@@ -0,0 +1,16 @@
from rest_framework import viewsets
from api.serializers import *
from api.models import *
class RecipeView(viewsets.ModelViewSet):
serializer_class = RecipeSerializer
queryset = Recipe.objects.all()
class IngredientView(viewsets.ModelViewSet):
serializer_class = IngredientSerializer
queryset = Ingredient.objects.all()
class HomegroupView(viewsets.ModelViewSet):
serializer_class = HomegroupSerializer
queryset = Homegroup.objects.all()