def new_message(request, template): """Send a new private message.""" to = request.GET.get('to') if to: try: User.objects.get(username=to) except User.DoesNotExist: contrib_messages.add_message( request, contrib_messages.ERROR, _('Invalid username provided. Enter a new username below.')) return HttpResponseRedirect(reverse('messages.new')) form = MessageForm(request.POST or None, initial={'to': to}) if (request.method == 'POST' and form.is_valid() and not is_ratelimited(request, increment=True, rate='50/d', ip=False, keys=user_or_ip('private-message-day'))): send_message(form.cleaned_data['to'], form.cleaned_data['message'], request.user) if form.cleaned_data['in_reply_to']: irt = form.cleaned_data['in_reply_to'] try: m = InboxMessage.objects.get(pk=irt, to=request.user) m.update(replied=True) except InboxMessage.DoesNotExist: pass contrib_messages.add_message(request, contrib_messages.SUCCESS, _('Your message was sent!')) return HttpResponseRedirect(reverse('messages.inbox')) return render(request, template, {'form': form})
def new_message(request, template): """Send a new private message.""" to = request.GET.get("to") if to: try: User.objects.get(username=to) except User.DoesNotExist: contrib_messages.add_message( request, contrib_messages.ERROR, _("Invalid username provided. Enter a new username below.") ) return HttpResponseRedirect(reverse("messages.new")) form = MessageForm(request.POST or None, initial={"to": to}) if ( request.method == "POST" and form.is_valid() and not is_ratelimited(request, increment=True, rate="50/d", ip=False, keys=user_or_ip("private-message-day")) ): send_message(form.cleaned_data["to"], form.cleaned_data["message"], request.user) if form.cleaned_data["in_reply_to"]: irt = form.cleaned_data["in_reply_to"] try: m = InboxMessage.objects.get(pk=irt, to=request.user) m.update(replied=True) except InboxMessage.DoesNotExist: pass contrib_messages.add_message(request, contrib_messages.SUCCESS, _("Your message was sent!")) return HttpResponseRedirect(reverse("messages.inbox")) return render(request, template, {"form": form})
def test_send_message(self): to = [user(save=True), user(save=True)] sender = user(save=True) msg_text = "hi there!" send_message(to=to, text=msg_text, sender=sender) msgs_in = InboxMessage.objects.all() msgs_out = OutboxMessage.objects.all() eq_(1, msgs_out.count()) msg_out = msgs_out[0] eq_(sender, msg_out.sender) eq_(msg_text, msg_out.message) for u in msg_out.to.all(): assert u in to eq_(2, msgs_in.count()) for message in msgs_in: eq_(sender, message.sender) assert message.to in to eq_(msg_text, message.message)