Exemple #1
0
 def post(self, request, *args, **kwargs):
     print(settings.EMAIL_HOST_USER)
     print(settings.EMAIL_HOST_PASSWORD)
     serializer = ContactSerializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     self.perform_create(serializer)
     headers = self.get_success_headers(serializer.data)
     email_to_customer.delay(self.request.user.username,
                             request.data['email'])
     email_to_admin.delay(request.data['email'], request.data['content'])
     return response.Response(serializer.data,
                              status=status.HTTP_201_CREATED,
                              headers=headers)
    def create(self, request, *args, **kwargs):
        """
        adds operation success boolean for client
        """
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)

        try:
            conversation = self.perform_create(serializer)
        except ConversationError as e:
            return Response({
                'success': False,
                'error': e.code
            },
                            status=status.HTTP_400_BAD_REQUEST)

        if conversation:
            serialized_conversation = serializer.data
            contacts_serializer = ContactSerializer(
                instance=conversation.participants, many=True)
            serialized_conversation['participants'] = contacts_serializer.data

            headers = self.get_success_headers(serializer.data)

            contact_serializer = ContactSerializer(
                instance=request.user.contact)

            return Response(
                {
                    'success': True,
                    'conversation': serialized_conversation,
                    "authenticated_contact": contact_serializer.data
                },
                status=status.HTTP_201_CREATED,
                headers=headers)
        else:
            return Response(
                {
                    'success': False,
                    'errors': {
                        'non_field_errors': [
                            'you cannot start a conversation with a non-friend contact '
                        ]
                    },
                },
                status=status.HTTP_400_BAD_REQUEST)
class OrderSerializer(serializers.ModelSerializer):
    ordered_items = OrderItemSerializer(read_only=True, many=True)
    total_price = serializers.IntegerField()
    total_items_count = serializers.IntegerField()
    contact = ContactSerializer(read_only=True)

    class Meta:
        model = Order
        fields = ('id', 'status', 'total_items_count', 'total_price',
                  'contact', 'ordered_items')
        read_only_fields = ('id', )
    def retrieve(self, request, *args, **kwargs):
        """
        adds operation success boolean for client
        """
        response = super().retrieve(request, *args, **kwargs)

        conversation = response.data

        contacts = Contact.objects.filter(conversations__id=conversation['id'])
        serializer = ContactSerializer(contacts, many=True)
        conversation['participants'] = serializer.data

        return Response({
            'success': True,
            "conversation": conversation
        },
                        status=status.HTTP_200_OK)
Exemple #5
0
    async def add_conversation(self, conversation_id):
        group_name = f'{self.connected_user.id}_friends'
        conversation = await get_conversation_or_error(
            conversation_id=conversation_id, user=self.connected_user)
        conversation_serializer = ConversationSerializer(instance=conversation)

        contacts = conversation.participants.all()
        contacts_serializer = ContactSerializer(instance=contacts, many=True)

        conversation_object = conversation_serializer.data
        conversation_object['participants'] = contacts_serializer.data

        await self.channel_layer.group_send(
            group_name, {
                'type': 'conversation_added',
                'creator': self.connected_user.id,
                'conversation': conversation_object,
            })
Exemple #6
0
    async def conversation_added(self, event):

        user_is_participant = False

        for participant in event['conversation']['participants']:
            if participant['user'] == self.connected_user.id and participant[
                    'user'] is not event['creator']:
                user_is_participant = True

        authenticated_contact = ContactSerializer(
            instance=self.connected_user.contact).data

        if user_is_participant:
            await self.send_json({
                'command': 'CONVERSATION',
                'conversation': event['conversation'],
                'authenticated_user': self.connected_user.id,
                'authenticated_contact': authenticated_contact,
                'creator': event['creator'],
            })
class MessageSerializer(serializers.ModelSerializer):
    sender = ContactSerializer()
    time_sent = serializers.TimeField(
        read_only=True,
        required=False,
        format="%I: %M %p",
    )
    attachments = AttachmentsSerializer(required=False,
                                        read_only=True,
                                        many=True)

    class Meta:
        model = Message
        fields = '__all__'

    def validate(self, attrs):
        if not attrs.get('sender') in attrs.get(
                'conversation').participants.all():
            raise serializers.ValidationError(
                'cannot send to a conversation that is not joined')

        return attrs