Skip to content

Commit

Permalink
adding hooks for recipe group creation when a recipe is created
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanNoelk committed Nov 15, 2018
1 parent fdb772e commit 28ceb06
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 5 deletions.
59 changes: 57 additions & 2 deletions v1/recipe/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
from rest_framework.fields import SerializerMethodField

from v1.recipe.models import Recipe, SubRecipe
from v1.recipe_groups.models import Tag
from v1.recipe_groups.models import Tag, Course, Cuisine
from v1.ingredient.serializers import IngredientGroupSerializer
from v1.recipe_groups.serializers import TagSerializer
from v1.recipe_groups.serializers import TagSerializer, CourseSerializer, CuisineSerializer
from v1.ingredient.models import IngredientGroup, Ingredient
from v1.recipe.mixins import FieldLimiter
from v1.rating.average_rating import average_rating
Expand Down Expand Up @@ -102,6 +102,8 @@ class RecipeSerializer(FieldLimiter, serializers.ModelSerializer):
pub_date = serializers.DateTimeField(format="%Y-%m-%d", read_only=True)
update_date = serializers.DateTimeField(format="%Y-%m-%d", read_only=True)
username = serializers.ReadOnlyField(source='author.username')
course = CourseSerializer()
cuisine = CuisineSerializer()

def get_subrecipes(self, obj):
try:
Expand All @@ -123,6 +125,33 @@ def update(self, instance, validated_data):
# Pop tags, and ingredients
ingredient_data = validated_data.pop('ingredient_groups', None)
tag_data = validated_data.pop('tags', None)
course = validated_data.pop('course', None)
cuisine = validated_data.pop('cuisine', None)
print(course)
print(cuisine)

# If the course is a string.
# Create a new course and replace it with a string
if course.get('id'):
validated_data['course'] = Course.objects.get(id=course.get('id'))
elif course.get('title'):
validated_data['course'] = Course.objects.create(
author=self.context['request'].user,
title=course.get('title')
).save()
#TODO: on update check if there are any of the old course left.
# if not delete them

# If the cuisine is a string.
# Create a new cuisine and replace it with a string
if cuisine.get('id'):
validated_data['cuisine'] = Cuisine.objects.get(id=course.get('id'))
elif cuisine.get('title'):
validated_data['cuisine'] = Cuisine.objects.create(
author=self.context['request'].user,
title=course.get('title')
).save()

# ManytoMany fields in django rest don't work very well, so we are getting the data directly fron teh context
subrecipe_data = None
if 'request' in self.context:
Expand Down Expand Up @@ -188,6 +217,32 @@ def create(self, validated_data):
# Pop tags, and ingredients
ingredient_data = validated_data.pop('ingredient_groups', None)
tag_data = validated_data.pop('tags', None)
course = validated_data.pop('course', None)
cuisine = validated_data.pop('cuisine', None)
validated_data.pop('author')

# If the course is a string.
# Create a new course and replace it with a string
if course.get('id'):
validated_data['course'] = Course.objects.get(id=course.get('id'))
elif course.get('title'):
validated_data['course'] = Course.objects.create(
author=self.context['request'].user,
title=course.get('title')
).save()
#TODO: on update check if there are any of the old course left.
# if not delete them

# If the cuisine is a string.
# Create a new cuisine and replace it with a string
if cuisine.get('id'):
validated_data['cuisine'] = Cuisine.objects.get(id=course.get('id'))
elif cuisine.get('title'):
validated_data['cuisine'] = Cuisine.objects.create(
author=self.context['request'].user,
title=course.get('title')
).save()

# ManytoMany fields in django rest don't work very well, so we are getting the data directly fron teh context
subrecipe_data = None
if 'request' in self.context:
Expand Down
32 changes: 31 additions & 1 deletion v1/recipe_groups/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,37 @@
from .models import Cuisine, Course, Tag


class CourseSerializer(serializers.ModelSerializer):
id = serializers.IntegerField()
author = serializers.HiddenField(
default=serializers.CurrentUserDefault()
)

class Meta:
model = Course
fields = (
'id',
'author',
'title',
)


class CuisineSerializer(serializers.ModelSerializer):
id = serializers.IntegerField()
author = serializers.HiddenField(
default=serializers.CurrentUserDefault()
)

class Meta:
model = Cuisine
fields = (
'id',
'author',
'title',
)


class AggCuisineSerializer(serializers.ModelSerializer):
""" Standard `rest_framework` ModelSerializer """
total = serializers.IntegerField(read_only=True)

Expand All @@ -14,7 +44,7 @@ class Meta:
fields = '__all__'


class CourseSerializer(serializers.ModelSerializer):
class AggCourseSerializer(serializers.ModelSerializer):
""" Standard `rest_framework` ModelSerializer """
total = serializers.IntegerField(read_only=True)

Expand Down
4 changes: 2 additions & 2 deletions v1/recipe_groups/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class CuisineCountViewSet(viewsets.ModelViewSet):
Uses `title` as the PK for any lookups.
"""
serializer_class = serializers.CuisineSerializer
serializer_class = serializers.AggCuisineSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,
IsOwnerOrReadOnly)
lookup_field = 'slug'
Expand Down Expand Up @@ -97,7 +97,7 @@ class CourseCountViewSet(viewsets.ModelViewSet):
Uses `title` as the PK for any lookups.
"""
serializer_class = serializers.CourseSerializer
serializer_class = serializers.AggCourseSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,
IsOwnerOrReadOnly)
lookup_field = 'slug'
Expand Down

0 comments on commit 28ceb06

Please sign in to comment.