food to tree (model, api, serializer)
This commit is contained in:
@ -289,34 +289,32 @@ class SupermarketSerializer(UniqueFieldsMixin, SpacedModelSerializer):
|
||||
class FoodSerializer(UniqueFieldsMixin, WritableNestedModelSerializer):
|
||||
supermarket_category = SupermarketCategorySerializer(allow_null=True, required=False)
|
||||
image = serializers.SerializerMethodField('get_image')
|
||||
#numrecipe = serializers.SerializerMethodField('count_recipes')
|
||||
numrecipe = serializers.SerializerMethodField('count_recipes')
|
||||
|
||||
# TODO check if it is a recipe and get that image first
|
||||
def get_image(self, obj):
|
||||
if obj.recipe:
|
||||
recipes = Recipe.objects.filter(id=obj.recipe).exclude(image__isnull=True).exclude(image__exact='')
|
||||
if len(recipes) == 0:
|
||||
return recipes.image.url
|
||||
# if food is not also a recipe, look for recipe images that use the food
|
||||
recipes = Recipe.objects.filter(steps__ingredients__food=obj).exclude(image__isnull=True).exclude(image__exact='')
|
||||
if len(recipes) == 0:
|
||||
recipes = Recipe.objects.filter(keywords__in=obj.get_tree()).exclude(image__isnull=True).exclude(image__exact='') # if no recipes found - check whole tree
|
||||
# if len(recipes) != 0:
|
||||
# return random.choice(recipes).image.url
|
||||
# else:
|
||||
# return None
|
||||
return None
|
||||
|
||||
# def count_recipes(self, obj):
|
||||
# return obj.recipe_set.all().count()
|
||||
# if no recipes found - check whole tree
|
||||
if len(recipes) == 0:
|
||||
recipes = Recipe.objects.filter(steps__ingredients__food__in=obj.get_tree()).exclude(image__isnull=True).exclude(image__exact='')
|
||||
|
||||
if len(recipes) != 0:
|
||||
return random.choice(recipes).image.url
|
||||
else:
|
||||
return None
|
||||
|
||||
def count_recipes(self, obj):
|
||||
return Recipe.objects.filter(steps__ingredients__food=obj).count()
|
||||
|
||||
def create(self, validated_data):
|
||||
validated_data['name'] = validated_data['name'].strip()
|
||||
validated_data['space'] = self.context['request'].space
|
||||
supermarket = validated_data.pop('supermarket_category')
|
||||
obj, created = Food.objects.get_or_create(**validated_data)
|
||||
if supermarket:
|
||||
obj.supermarket_category, created = SupermarketCategory.objects.get_or_create(name=supermarket['name'], space=self.context['request'].space)
|
||||
obj.save()
|
||||
return obj
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
@ -325,7 +323,8 @@ class FoodSerializer(UniqueFieldsMixin, WritableNestedModelSerializer):
|
||||
|
||||
class Meta:
|
||||
model = Food
|
||||
fields = ('id', 'name', 'recipe', 'ignore_shopping', 'supermarket_category', 'image')
|
||||
fields = ('id', 'name', 'recipe', 'ignore_shopping', 'supermarket_category', 'image', 'parent', 'numchild', 'numrecipe')
|
||||
read_only_fields = ('id', 'numchild', 'parent', 'image')
|
||||
|
||||
|
||||
class IngredientSerializer(WritableNestedModelSerializer):
|
||||
|
Reference in New Issue
Block a user