def post_message(self, request, pk=None): """ Pk is the pk of the Thread to which the message belongs. """ # we get the thread and check for permission thread = Thread.objects.get(id=pk) self.check_object_permissions(request, thread) # we get the body body = compat_get_request_data(self.request).get('body') # we create the message # Message.objects.save() could return an Exception try: message = Message(sender=request.rest_messaging_participant, thread=thread, body=body) message.save() serializer = SimpleMessageSerializer(message) return Response(serializer.data, status=status.HTTP_201_CREATED) except Exception: return Response(status=status.HTTP_412_PRECONDITION_FAILED)
def test_check_callback_and_save(self): # we ensure a user may by default not send more than 50 messages a day Message.objects.filter(sender=self.participant1).count() r = 100 for i in range(r): try: m = Message(sender=self.participant1, thread=self.thread1, body="hi") m.save() except Exception: pass # we have more than 50 messages (more in setUp) self.assertEqual(50, Message.objects.filter(sender=self.participant1).count()) # the limit is only in the last 24 hours Message.objects.filter(sender=self.participant1).update(sent_at=now() - timedelta(days=1, seconds=1)) last = Message.objects.filter(sender=self.participant1).latest('id') new = Message.objects.create(sender=self.participant1, thread=self.thread1, body="hi") self.assertEqual(new.id, last.id + 1)
def test_check_callback_and_save(self): # we ensure a user may by default not send more than 50 messages a day Message.objects.filter(sender=self.participant1).count() r = 100 for i in range(r): try: m = Message(sender=self.participant1, thread=self.thread1, body="hi") m.save() except Exception: pass # we have more than 50 messages (more in setUp) self.assertEqual( 50, Message.objects.filter(sender=self.participant1).count()) # the limit is only in the last 24 hours Message.objects.filter(sender=self.participant1).update( sent_at=now() - timedelta(days=1, seconds=1)) last = Message.objects.filter(sender=self.participant1).latest('id') new = Message.objects.create(sender=self.participant1, thread=self.thread1, body="hi") self.assertEqual(new.id, last.id + 1)