Exemple #1
0
def send_message(request):

    # вынести в форму
    Member = apps.get_model('chats', 'Member')
    Chat = apps.get_model('chats', 'Chat')

    form = MessageForm(request.POST)

    if form.is_valid():
        message = form.save()
        msg = {
            'id': message.id,
            'chat_id': message.chat.id,
            'user_id': message.user.id,
            'content': message.content,
            'added_at': message.added_at
        }

        chat = Chat.objects.filter(id=message.chat.id).first()

        if chat:
            chat.last_message = message.content
            chat.save()

        members = list(
            Member.objects.filter(chat_id=message.chat.id).exclude(
                user_id=message.user.id))
        for member in members:
            member.new_messages += 1
            member.save()

        return JsonResponse({'message': msg})

    return JsonResponse({'errors': form.errors}, status=400)
def send_message(request, user_id):
    form = MessageForm(json.loads(request.body), user_id=user_id)
    if form.is_valid():
        message = form.save()
        headers = {
            'Content-type': 'application/json',
            'Authorization': 'apikey ' + settings.CENTRIFUGE_API
        }
        msg = {
            'time': myconverter(message.added_at),
            'message': message.content,
            'user': message.user.username,
            'type': 'text'
        }
        command = {
            "method": "publish",
            "params": {
                "channel": "chat:" + str(message.chat.id),
                "data": {
                    "message": msg,
                }
            }
        }
        data = json.dumps(command)
        requests.post(settings.CENTRIFUGE_ADDRESS, data=data, headers=headers)
        return JsonResponse('OK', safe=False)
    else:
        return JsonResponse({'errors': form.errors}, status=400)
def upload_file(request, chat_id=None):
    if chat_id is None:
        return JsonResponse({'Error': 'ID is None'}, status=400)
    user = request.user
    im_file = request.POST.get('image')
    image_key = request.POST.get('image_key')
    chat = Chat.objects.filter(id=chat_id).first()
    if chat is None:
        return JsonResponse({'Error': 'No such Chat {}'.format(chat_id)},
                            status=404)
    mime = magic.from_file(im_file, mime=True)
    form = MessageForm(request.POST)
    if form.is_valid():
        message = form.save()
        message.user_id = user.id
        message.chat_id = chat.id
        message.image = ImageFile(open(im_file, 'rb'))
        message.image_mime_type = mime
        message.image_key = image_key
        message.save()
        chat.last_message = 'Image'
        chat.save()
        return JsonResponse({
            'Message': 'Messahe has been added',
        })
    else:
        return JsonResponse({'Error': form.errors}, status=400)
def send_message(request):
    form = MessageForm(request.POST)
    if form.is_valid():
        message = form.save(commit=False)
        message.added_at = timezone.now()
        message.user = User.objects.filter(id=request.POST['user']).first()
        message.chat = Chat.objects.filter(id=request.POST['chat']).first()
        message.save()
        return JsonResponse({'msg': 'message saved'})
    return JsonResponse({'errors': form.errors}, status=400)
Exemple #5
0
def send_message(request):
    Member = apps.get_model('chats', 'Member')
    Chat = apps.get_model('chats', 'Chat')
    Message = apps.get_model('chats', 'Message')

    form = MessageForm(request.POST)

    if form.is_valid():
        #message = form.save()

        message = Message.objects.create(
            chat_id=form.cleaned_data['chat'].id,
            user_id=request.user.id,
            content=form.cleaned_data['content']
        )  #, added_at=form.cleaned_data['added_at'])

        msg = {
            'id': message.id,
            'chat_id': message.chat.id,
            'user_id': message.user.id,
            'content': message.content
        }  # , 'added_at': message.added_at}

        chat = Chat.objects.filter(id=message.chat.id).first()

        if chat:
            chat.last_message = message.content
            chat.save()

        members = list(
            Member.objects.filter(chat_id=message.chat.id).exclude(
                user_id=message.user.id))
        for member in members:
            member.new_messages += 1
            member.save()

        # msg_ws = {'id': message.id, 'chat_id': message.chat.id, 'user_id': message.user.id, 'content': message.content , 'added_at': message.added_at}

        requests.post(
            'http://localhost:8080/api',
            json={
                "method": "publish",
                "params": {
                    "channel": "chat" + str(message.chat.id),
                    "data": {
                        'message': msg
                    }
                }
            },
            headers={'Authorization': 'apikey ' + settings.CENTRIFUGE_API})
        return JsonResponse({"result": {'message': msg}})

    return JsonResponse({'errors': form.errors}, status=400)
def send_message_with_html(request):
    if request.method == 'POST':
        form = MessageForm(request.POST)
        if form.is_valid():
            message = form.save(commit=False)
            message.added_at = timezone.now()
            message.user = User.objects.filter(id=1).first()
            message.chat = Chat.objects.filter(id=1).first()
            message.save()
            #message = Message.objects.create(
            #    content = form.content,
            #    user = form.user,
            #    chat = form.chat,
            #   added_at = timezone.now())
            return JsonResponse({'msg': 'message saved'})
        return JsonResponse({'errors': form.errors}, status=400)
    else:
        form = MessageForm()
    return render(request, 'send_message_with_html.html', {'form': form})