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'})
def get(self, name): try: chat = Chat.objects(slug_name=name).get() last_date = self.get_argument('last_date', None) if last_date: last_date = datetime.strptime(last_date, '%Y-%m-%dT%H:%M:%S.%f') messages = Message.objects(chat=chat, is_private=self.is_private, created_at__gt=last_date).order_by('created_at') else: messages = Message.objects(chat=chat, is_private=self.is_private).order_by('-created_at').limit(10) messages = [message for message in messages] messages.reverse() self.write({'results': [message.to_representation() for message in messages]}) except Chat.DoesNotExist: self.clear() self.set_status(404) self.finish({'error': 'not found'})
def clean(): for message in Message.objects(): message.delete() for user in User.objects(): user.delete() for chat in Chat.objects(): chat.delete()
def post(self, message_id): try: message = Message.objects(id=message_id).get() message.was_seen = True message.save() self.write(message.to_representation()) except Message.DoesNotExist: self.clear() self.set_status(404) self.finish({'error': 'not found'})