from rest_framework import serializers

from foodsaving.conversations.models import Conversation, ConversationMessage


class ConversationSerializer(serializers.ModelSerializer):
    class Meta:
        model = Conversation
        fields = [
            'id',
            'participants',
            'created_at'
        ]

    def retrieve(self, validated_data):
        user = self.context['request'].user
        return ConversationMessage.objects.create(author=user, **validated_data)


class ConversationMessageSerializer(serializers.ModelSerializer):
    class Meta:
        model = ConversationMessage
        fields = [
            'id',
            'author',
            'content',
            'conversation',
            'created_at'
        ]


class CreateConversationMessageSerializer(serializers.ModelSerializer):
    class Meta:
        model = ConversationMessage
        fields = [
            'id',
            'author',
            'content',
            'conversation'
        ]
        extra_kwargs = {
            'author': {
                'read_only': True
            }
        }

    def create(self, validated_data):
        user = self.context['request'].user
        return ConversationMessage.objects.create(author=user, **validated_data)

from rest_framework import serializers

from foodsaving.conversations.models import Conversation, ConversationMessage


class ConversationSerializer(serializers.ModelSerializer):
    class Meta:
        model = Conversation
        fields = [
            'id',
            'participants',
            'created_at'
        ]

    def retrieve(self, validated_data):
        user = self.context['request'].user
        return ConversationMessage.objects.create(author=user, **validated_data)


class ConversationMessageSerializer(serializers.ModelSerializer):
    class Meta:
        model = ConversationMessage
        fields = [
            'id',
            'author',
            'content',
            'conversation',
            'created_at'
        ]


class CreateConversationMessageSerializer(serializers.ModelSerializer):
    class Meta:
        model = ConversationMessage
        fields = [
            'id',
            'author',
            'content',
            'conversation'
        ]
        extra_kwargs = {
            'author': {
                'read_only': True
            }
        }

    def validate_conversation(self, conversation):
        if self.context['request'].user not in conversation.participants.all():
            raise serializers.ValidationError("You are not in this conversation")
        return conversation

    def create(self, validated_data):
        user = self.context['request'].user
        return ConversationMessage.objects.create(author=user, **validated_data)

Validate user is in conversation on create message
Validate user is in conversation on create message

  from rest_framework import serializers
  
  from foodsaving.conversations.models import Conversation, ConversationMessage
  
  
  class ConversationSerializer(serializers.ModelSerializer):
      class Meta:
          model = Conversation
          fields = [
              'id',
              'participants',
              'created_at'
          ]
  
      def retrieve(self, validated_data):
          user = self.context['request'].user
          return ConversationMessage.objects.create(author=user, **validated_data)
  
  
  class ConversationMessageSerializer(serializers.ModelSerializer):
      class Meta:
          model = ConversationMessage
          fields = [
              'id',
              'author',
              'content',
              'conversation',
              'created_at'
          ]
  
  
  class CreateConversationMessageSerializer(serializers.ModelSerializer):
      class Meta:
          model = ConversationMessage
          fields = [
              'id',
              'author',
              'content',
              'conversation'
          ]
          extra_kwargs = {
              'author': {
                  'read_only': True
              }
          }
  
+     def validate_conversation(self, conversation):
+         if self.context['request'].user not in conversation.participants.all():
+             raise serializers.ValidationError("You are not in this conversation")
+         return conversation
+ 
      def create(self, validated_data):
          user = self.context['request'].user
          return ConversationMessage.objects.create(author=user, **validated_data)
  
Validate user is in conversation on create message
## Code Before:
from rest_framework import serializers

from foodsaving.conversations.models import Conversation, ConversationMessage


class ConversationSerializer(serializers.ModelSerializer):
    class Meta:
        model = Conversation
        fields = [
            'id',
            'participants',
            'created_at'
        ]

    def retrieve(self, validated_data):
        user = self.context['request'].user
        return ConversationMessage.objects.create(author=user, **validated_data)


class ConversationMessageSerializer(serializers.ModelSerializer):
    class Meta:
        model = ConversationMessage
        fields = [
            'id',
            'author',
            'content',
            'conversation',
            'created_at'
        ]


class CreateConversationMessageSerializer(serializers.ModelSerializer):
    class Meta:
        model = ConversationMessage
        fields = [
            'id',
            'author',
            'content',
            'conversation'
        ]
        extra_kwargs = {
            'author': {
                'read_only': True
            }
        }

    def create(self, validated_data):
        user = self.context['request'].user
        return ConversationMessage.objects.create(author=user, **validated_data)

## Instruction:
Validate user is in conversation on create message
## Code After:
from rest_framework import serializers

from foodsaving.conversations.models import Conversation, ConversationMessage


class ConversationSerializer(serializers.ModelSerializer):
    class Meta:
        model = Conversation
        fields = [
            'id',
            'participants',
            'created_at'
        ]

    def retrieve(self, validated_data):
        user = self.context['request'].user
        return ConversationMessage.objects.create(author=user, **validated_data)


class ConversationMessageSerializer(serializers.ModelSerializer):
    class Meta:
        model = ConversationMessage
        fields = [
            'id',
            'author',
            'content',
            'conversation',
            'created_at'
        ]


class CreateConversationMessageSerializer(serializers.ModelSerializer):
    class Meta:
        model = ConversationMessage
        fields = [
            'id',
            'author',
            'content',
            'conversation'
        ]
        extra_kwargs = {
            'author': {
                'read_only': True
            }
        }

    def validate_conversation(self, conversation):
        if self.context['request'].user not in conversation.participants.all():
            raise serializers.ValidationError("You are not in this conversation")
        return conversation

    def create(self, validated_data):
        user = self.context['request'].user
        return ConversationMessage.objects.create(author=user, **validated_data)
