예제 #1
0
파일: views.py 프로젝트: CalevGrad/restAPI
    def create(self, request, *args, **kwargs):
        """
        parameters: 'recipientID', 'textFirstMessage'
        """
        for parameter in ['recipientID', 'textFirstMessage']:
            if parameter not in request.data:
                print(3333)
                return Response(status=status.HTTP_400_BAD_REQUEST)

        recipientID = request.data['recipientID']
        textFirstMessage = request.data['textFirstMessage']

        querysetDialogsUser = list()
        querysetDialogsRecipient = list()

        #exist dialog?
        for item in DialogOwners.objects.filter(owner=request.user):
            querysetDialogsUser.append(item.dialog)
        for item in DialogOwners.objects.filter(owner=User.objects.get(id=recipientID)):
            querysetDialogsRecipient.append(item.dialog)
        
        #if dialog exist
        for item in querysetDialogsUser:
            if querysetDialogsRecipient.count(item) != 0:
                return Response(status=status.HTTP_400_BAD_REQUEST)

        dialog = Dialog()
        dialog.save()

        message = Message()
        message.owner = request.user
        message.ownerDialog = dialog
        message.text = textFirstMessage
        message.date = timezone.now()
        message.save()

        dialog.textLastMessage = textFirstMessage
        dialog.dateLastMessage = message.date
        dialog.idLastMessage = message.id

        dialog.save()

        dialogOwner1 = DialogOwners()
        dialogOwner2 = DialogOwners()

        dialogOwner1.owner = request.user
        dialogOwner1.dialog = dialog
        dialogOwner2.owner = User.objects.get(id=recipientID)
        dialogOwner2.dialog = dialog

        dialogOwner1.save()
        dialogOwner2.save()

        #data = [{ 'id':dialog.id }]
        return Response(status=status.HTTP_201_CREATED)
예제 #2
0
    def create(self, validated_data):
        text = validated_data.get('text', None)
        dialog = validated_data.get('dialog', None)

        if text is None or text == '':
            raise serializers.ValidationError('Отсутствуют необходимые параметры!')

        owners = dialog.owners.all()

        if self.user not in owners:
            raise PermissionDenied

        message = Message()
        message.owner = self.user
        message.dialog = dialog
        message.text = text
        message.save()

        dialog.last_message = message
        dialog.save()

        send_event('new_message', owners, MessageSerializer(message).data)

        return message
예제 #3
0
파일: views.py 프로젝트: CalevGrad/restAPI
    def create(self, request, *args, **kwargs):
        """
        parameters POST: 'dialogID', 'text'
        """
        for parameter in ['dialogID', 'text']:
            if parameter not in request.data:
                return Response(status=status.HTTP_400_BAD_REQUEST)

        dialog = Dialog.objects.get(pk=request.data['dialogID'])
        message = Message()
        message.owner = request.user
        message.ownerDialog = dialog
        message.text = request.data['text']
        message.date = timezone.now()
        message.save()

        if dialog.dateLastMessage < message.date:
            dialog.dateLastMessage = message.date
            dialog.textLastMessage = message.text
            dialog.idLastMessage = message.id
            dialog.save()

        #data = [{ 'id':message.id }, { 'date':message.date }]
        return Response(status=status.HTTP_201_CREATED)