Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
    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)