Пример #1
0
    def post(self, message_id):
        try:
            message = Message.objects(id=message_id).get()
            message.was_seen = True
            message.save()
            data = json.loads(self.request.body)
            body = data['body']

            response_message = Message(
                body=body,
                to_user=message.from_user,
                from_user=message.to_user,
                is_response=True,
                is_private=True,
                chat=message.chat,
                original_body=message.body
            )
            response_message.save()
            self.set_status(201)
            self.write(response_message.to_representation())


        except Message.DoesNotExist:
            self.clear()
            self.set_status(404)
            self.finish({'error': 'not found'})
Пример #2
0
    def post(self):
        try:
            data = json.loads(self.request.body)
            body = data['body']
            name = data['chat_id']
            username = data['username']
            chat = Chat.objects(slug_name=name).get()
            user = User.objects(username=username).get()
            self.set_status(201)

            message = Message(
                from_user=user,
                chat=chat,
                created_at=datetime.now(),
                body=body,
                is_private=self.is_private
            )

            users = [user for user in chat.users]
            if len(users) > 1:
                to_user = users[random.randrange(0, len(users))]
                while to_user.id == user.id:
                    to_user = users[random.randrange(0, len(users))]
                message.to_user = to_user

            message.save()

            self.write(message.to_representation())

        except (Chat.DoesNotExist, User.DoesNotExist):
            self.clear()
            self.set_status(404)
            self.finish({'error': 'not found'})