Пример #1
0
    def update(self, instance, validated_data):
        instance.user = validated_data.get('user', instance.user)
        instance.save()

        if 'ingredients' in validated_data:
            # Delete any quantified ingredients not included in the request.
            quantified_ingredient_ids = [item.get('id') for item in validated_data['ingredients']]
            for quantified_ingredient in instance.ingredients.all():
                if quantified_ingredient.id not in quantified_ingredient_ids:
                    quantified_ingredient.delete()

            # Create or update quantified ingredient instances that are in the request.
            for item in validated_data['ingredients']:
                if item.get('units') is not None:
                    units = Unit(**item['units'])
                    units.save()
                else:
                    units = None
                name = Ingredient(**item['name'])
                name.save()
                quantified_ingredient = QuantifiedIngredient(id=item.get('id'), qty=item.get('qty'), name=name, is_checked=item.get('is_checked', QuantifiedIngredient._meta.get_field('is_checked').get_default()),
                                                             units=units, grocery_list=instance, original_recipe=item.get('original_recipe'))
                quantified_ingredient.save()

        return instance
Пример #2
0
    def update(self, instance, validated_data):
        instance.name = validated_data.get('name', instance.name)
        instance.description = validated_data.get('description', instance.description)
        instance.photo = validated_data.get('photo', instance.photo)
        instance.is_public = validated_data.get('is_public', instance.is_public)
        instance.author = validated_data.get('author', instance.author)
        instance.tags = validated_data.get('tags', instance.tags)
        instance.favorite_by = validated_data.get('favorite_by', instance.favorite_by)
        instance.servings = validated_data.get('servings', instance.servings)
        instance.difficulty = validated_data.get('difficulty', instance.difficulty)
        instance.ratings = validated_data.get('ratings', instance.ratings)
        instance.save()

        if 'preparation_steps' in validated_data:
            # Delete any preparation steps not included in the request.
            preparation_step_ids = [item.get('id') for item in validated_data['preparation_steps']]
            for preparation_step in instance.preparation_steps.all():
                if preparation_step.id not in preparation_step_ids:
                    preparation_step.delete()

            # Create or update preparation step instances that are in the request.
            for item in validated_data['preparation_steps']:
                preparation_step = PreparationStep(id=item.get('id'), text=item['text'], step_number=item['step_number'], recipe=instance)
                preparation_step.save()

        if 'ingredients' in validated_data:
            # Delete any quantified ingredients not included in the request.
            quantified_ingredient_ids = [item.get('id') for item in validated_data['ingredients']]
            for quantified_ingredient in instance.ingredients.all():
                if quantified_ingredient.id not in quantified_ingredient_ids:
                    quantified_ingredient.delete()

            # Create or update quantified ingredient instances that are in the request.
            for item in validated_data['ingredients']:
                units = Unit(**item['units'])
                units.save()
                name = Ingredient(**item['name'])
                name.save()
                quantified_ingredient = QuantifiedIngredient(id=item.get('id'), qty=item['qty'], name=name,
                                                             units=units, recipe=instance, original_recipe=instance)
                quantified_ingredient.save()

        return instance